import { ANSI_ALPHABET, DIGITS, SPECSYMBOLS, INTERACTORS, MODIFIERS } from '../constants'; import { Subject } from 'rxjs'; import { ForbiddenInput } from './forbidden-input'; export class KeySet { public keyset: Set = new Set(); public forbiddenInput = new Subject(); constructor(...rest: string[]){ this.replenish(...rest); } /* --- Public Methods --- */ public all(...exclusions: string[]): KeySet { this._replenishKeys([].concat(ANSI_ALPHABET,DIGITS,SPECSYMBOLS,INTERACTORS,MODIFIERS), exclusions) return this; } public alphabet(...exclusions: string[]): KeySet{ this._replenishKeys(ANSI_ALPHABET, exclusions); return this; } public digits(...exclusions: string[]): KeySet{ this._replenishKeys(DIGITS, exclusions); return this; } public specSymbols(...exclusions: string[]): KeySet { this._replenishKeys(SPECSYMBOLS, exclusions); return this; } public interactors(...exclusions: string[]): KeySet { this._replenishKeys(INTERACTORS, exclusions); return this; } public modifiers(...exclusions: string[]): KeySet { this._replenishKeys(MODIFIERS, exclusions); return this; } public removeAll(...exclusions: string[]): KeySet{ this.keyset.clear(); if(exclusions && exclusions.length){ exclusions.forEach(exc => { this.keyset.add(exc); }) } return this; } public removeAlphabet(...exclusions: string[]): KeySet{ this._reduceKeys(ANSI_ALPHABET, exclusions); return this; } public removeDigits(...exclusions: string[]): KeySet{ this._reduceKeys(DIGITS, exclusions); return this; } public removeSpecSymbols(...exclusions: string[]): KeySet{ this._reduceKeys(SPECSYMBOLS, exclusions); return this; } public removeInteractors(...exclusions: string[]): KeySet{ this._reduceKeys(INTERACTORS, exclusions); return this; } public removeModifiers(...exclusions: string[]): KeySet{ this._reduceKeys(MODIFIERS, exclusions); return this; } public replenish(...keys: string[]): KeySet{ this._replenishKeys(keys); return this; } public reduce(...keys: string[]): KeySet{ this._reduceKeys(keys); return this; } public has(key: string): boolean { return this.keyset.has(key) } public get size(): number { return this.keyset.size } static getKeyFamily(key: string): string { if(ANSI_ALPHABET.includes(key)){ return 'alphabet' }else if(DIGITS.includes(key)){ return 'digits' }else if(SPECSYMBOLS.includes(key)){ return 'specsymbols' }else if(INTERACTORS.includes(key)){ return 'interactors' }else if(MODIFIERS.includes(key)){ return 'modifiers' }else{ return null; } } /* --- Private Implementations --- */ private _replenishKeys(keys: string[], exclusions?: string[]){ keys = this._exclude(keys,exclusions); keys.forEach(key => this.keyset.add(key)); }; private _reduceKeys(keys: string[], exclusions?: string[]){ keys = this._exclude(keys,exclusions); keys.forEach(key => this.keyset.delete(key)); }; private _exclude(srcKeys: string[], exclusions: string[]): string[] { if(exclusions && exclusions.length){ return srcKeys.filter(key => !exclusions.includes(key)); }else{ return srcKeys; } } }