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,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC;QAElD,MAAM,CAAC;YACN,OAAO,EAAE;gBACR,IAAI,CAAC,OAAO,GAAG,cAAkB,CAAC,CAAC;gBACnC,IAAI,CAAC,GAAG,CAAC,CAAC;gBACV,EAAE,CAAC,CAAC,OAAO,IAAI,KAAK,CAAC,CAAC,CAAC;oBACtB,OAAO,CAAC,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;wBAC7C,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;oBACtB,CAAC;gBACF,CAAC;gBACD,IAAI,GAAG,KAAK,GAAG,OAAO,GAAG,KAAK,GAAG,IAAI,CAAC;YACvC,CAAC;SACD,CAAC;IACH,CAAC;CACD","sourcesContent":["import { Handle } from './interfaces';\n\n/**\n * An entry in a MatchRegistry. Each Entry contains a test to determine whether the Entry is applicable, and a value for\n * the entry.\n */\ninterface Entry<T> {\n\treadonly test: Test | null;\n\treadonly value: T | null;\n}\n\n/**\n * A registry of values tagged with matchers.\n */\nexport default class MatchRegistry<T> {\n\tprotected _defaultValue: T | undefined;\n\tprivate readonly _entries: Entry<T>[] | null;\n\n\t/**\n\t * Construct a new MatchRegistry, optionally containing a given default value.\n\t */\n\tconstructor(defaultValue?: T) {\n\t\tthis._defaultValue = defaultValue;\n\t\tthis._entries = [];\n\t}\n\n\t/**\n\t * Return the first entry in this registry that matches the given arguments. If no entry matches and the registry\n\t * was created with a default value, that value will be returned. Otherwise, an exception is thrown.\n\t *\n\t * @param ...args Arguments that will be used to select a matching value.\n\t * @returns the matching value, or a default value if one exists.\n\t */\n\tmatch(...args: any[]): T {\n\t\tconst entries = this._entries ? this._entries.slice(0) : [];\n\t\tlet entry: Entry<T>;\n\n\t\tfor (let i = 0; (entry = entries[i]); ++i) {\n\t\t\tif (entry.value && entry.test && entry.test.apply(null, args)) {\n\t\t\t\treturn entry.value;\n\t\t\t}\n\t\t}\n\n\t\tif (this._defaultValue !== undefined) {\n\t\t\treturn this._defaultValue;\n\t\t}\n\n\t\tthrow new Error('No match found');\n\t}\n\n\t/**\n\t * Register a test + value pair with this registry.\n\t *\n\t * @param test The test that will be used to determine if the registered value matches a set of arguments.\n\t * @param value A value being registered.\n\t * @param first If true, the newly registered test and value will be the first entry in the registry.\n\t */\n\tregister(test: Test | null, value: T | null, first?: boolean): Handle {\n\t\tlet entries = this._entries;\n\t\tlet entry: Entry<T> | null = {\n\t\t\ttest: test,\n\t\t\tvalue: value\n\t\t};\n\n\t\t(<any>entries)[first ? 'unshift' : 'push'](entry);\n\n\t\treturn {\n\t\t\tdestroy: function(this: Handle) {\n\t\t\t\tthis.destroy = function(): void {};\n\t\t\t\tlet i = 0;\n\t\t\t\tif (entries && entry) {\n\t\t\t\t\twhile ((i = entries.indexOf(entry, i)) > -1) {\n\t\t\t\t\t\tentries.splice(i, 1);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\ttest = value = entries = entry = null;\n\t\t\t}\n\t\t};\n\t}\n}\n\n/**\n * The interface that a test function must implement.\n */\nexport interface Test {\n\t(...args: any[]): boolean | null;\n}\n"]}