import { BaseEntity } from '../infrastructure/Entity/BaseEntity' ; import { IScript } from './IScript' ; import { ICategory } from './ICategory' ; export class Category extends BaseEntity < number > implements ICategory { private allSubScripts : ReadonlyArray < IScript > = undefined ; constructor ( id , public readonly name , public readonly documentationUrls , public readonly subCategories ? , public readonly scripts ? ) { super ( id ) ; validateCategory ( this ) ; } public includes ( script ) { return this . getAllScriptsRecursively ( ) . some ( ( childScript ) => childScript . id === script . id ) ; } public getAllScriptsRecursively ( ) { if ( ! this . allSubScripts ) { this . allSubScripts = parseScriptsRecursively ( this ) ; } return this . allSubScripts ; } } function parseScriptsRecursively ( category ) { return [ ... category . scripts , ... category . subCategories . flatMap ( ( c ) => c . getAllScriptsRecursively ( ) ) , ] ; } function validateCategory ( category ) { if ( ! category . name ) { throw new Error ( 'undefined or empty name' ) ; } if ( ( ! category . subCategories || category . subCategories . length === 0 ) && ( ! category . scripts || category . scripts . length === 0 ) ) { throw new Error ( 'A category must have at least one sub-category or script' ) ; } }