import { IApplication } from './IApplication' ; import { ICategoryCollection } from './ICategoryCollection' ; import { IProjectInformation } from './IProjectInformation' ; import { OperatingSystem } from './OperatingSystem' ; export class Application implements IApplication { constructor ( public info , public collections ) { validateInformation ( info ) ; validateCollections ( collections ) ; } public getSupportedOsList ( ) { return this . collections . map ( ( collection ) => collection . os ) ; } public getCollection ( operatingSystem ) { return this . collections . find ( ( collection ) => collection . os === operatingSystem ) ; } } function validateInformation ( info ) { if ( ! info ) { throw new Error ( 'undefined project information' ) ; } } function validateCollections ( collections ) { if ( ! collections ) { throw new Error ( 'undefined collections' ) ; } if ( collections . length === 0 ) { throw new Error ( 'no collection in the list' ) ; } if ( collections . filter ( ( c ) => ! c ) . length > 0 ) { throw new Error ( 'undefined collection in the list' ) ; } const osList = collections . map ( ( c ) => c . os ) ; const duplicates = getDuplicates ( osList ) ; if ( duplicates . length > 0 ) { throw new Error ( `multiple collections with same os: ${ duplicates . map ( ( os ) => OperatingSystem [ os ] . toLowerCase ( ) ) . join ( '", "' ) }` ) ; } } function getDuplicates ( list ) { return list . filter ( ( os , index ) => list . indexOf ( os ) !== index ) ; }