import { unique } from '../utils/array-utils' ; export class IndexedItems { readonly items : string [ ] ; constructor ( items ) { this . items = unique ( items ) ; } includes ( item ) { return this . items . includes ( item ) ; } getIndex ( item ) { const idx = this . items . indexOf ( item ) ; if ( idx === - 1 ) { throw new Error ( `Item '${ item }' not found.` ) ; } return idx ; } getItem ( idx ) { if ( ! this . isValidIndex ( idx ) ) { throw new Error ( `Invalid index '${ idx }'.` ) ; } return this . items [ idx ] ; } maskUnknownItems ( sequence , unknownItem ) { return sequence . map ( item => this . items . includes ( item ) ? item : unknownItem ) ; } private isValidIndex ( idx ) { return 0 <= idx && idx < this . length ; } get length ( ) : number { return this . items . length ; } }