'use strict' ; /** Parses student email, name or both  */ export class StudentNameParser { parse ( input ) { return input . split ( ';' ) . map ( name => name . trim ( ) ) . filter ( name => name . length ) . map ( name => this . parseName ( name ) ) . filter ( student => ! ! student ) . map ( student => student ! ) ; } validateClassroomNames ( input ) { const invalidNames = input . split ( ';' ) . map ( name => name . trim ( ) ) . filter ( name => name . length > 0 ) . filter ( name => ! this . parseName ( name ) ) ; if ( invalidNames . length ) { return 'Invalid names: ' + invalidNames . join ( ', ' ) ; } else { return null ; } } EMAIL_PATTERN = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ; NAME_PATTERN = /^[\w \-\.]+$/ ; NAME_AND_EMAIL_PATTERN = /(^[\w \-\.]+)\s*(<(.+)>)?$/ ; parseName ( name ) { this . EMAIL_PATTERN . lastIndex = 0 ; this . NAME_PATTERN . lastIndex = 0 ; this . NAME_AND_EMAIL_PATTERN . lastIndex = 0 ; let match ; if ( match = name . match ( this . EMAIL_PATTERN ) ) { return new StudentName ( match [ 0 ] , match [ 0 ] ) ; } else if ( match = name . match ( this . NAME_PATTERN ) ) { return new StudentName ( match [ 0 ] ) ; } else if ( match = name . match ( this . NAME_AND_EMAIL_PATTERN ) ) { const emailPart = match [ 3 ] . trim ( ) ; this . EMAIL_PATTERN . lastIndex = 0 ; if ( emailPart . match ( this . EMAIL_PATTERN ) === null ) { return null ; } return new StudentName ( match [ 1 ] . trim ( ) , match [ 3 ] . trim ( ) ) ; } return null ; } } export class StudentName { constructor ( public readonly name , public readonly email ? ) { } getEffectiveName ( ) { return this . name ; } /**
     * Returns sanitized student name, so a folder may be created from it.
     */ getEffectivePath ( ) { return this . name . replace ( ' ' , '_' ) . replace ( '@' , '_at_' ) ; } }