UNPKG

1.78 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.AliasedMap = exports.CaseInsensitiveProxyHandler = exports.createCaseInsensitiveObject = void 0;
4function createCaseInsensitiveObject() {
5 return new Proxy({}, exports.CaseInsensitiveProxyHandler);
6}
7exports.createCaseInsensitiveObject = createCaseInsensitiveObject;
8exports.CaseInsensitiveProxyHandler = {
9 has: (obj, prop) => {
10 return conformPropertyKey(prop) in obj;
11 },
12 get: (obj, prop) => {
13 return obj[conformPropertyKey(prop)];
14 },
15 set: (obj, prop, value) => {
16 obj[conformPropertyKey(prop)] = value;
17 return true;
18 },
19 deleteProperty: (obj, prop) => {
20 return delete obj[conformPropertyKey(prop)];
21 },
22};
23const conformPropertyKey = (prop) => typeof prop === 'string' ? prop.toLowerCase() : prop;
24class AliasedMap extends Map {
25 getAliases() {
26 const aliasmap = new Map();
27 // TODO: waiting for https://github.com/Microsoft/TypeScript/issues/18562
28 const aliases = [...this.entries()].filter(([, v]) => typeof v === 'string' || typeof v === 'symbol');
29 aliases.forEach(([alias, cmd]) => {
30 const cmdaliases = aliasmap.get(cmd) || [];
31 cmdaliases.push(alias);
32 aliasmap.set(cmd, cmdaliases);
33 });
34 return aliasmap;
35 }
36 resolveAlias(key) {
37 const r = this.get(key);
38 if (typeof r !== 'string' && typeof r !== 'symbol') {
39 return r;
40 }
41 return this.resolveAlias(r);
42 }
43 keysWithoutAliases() {
44 return [...this.entries()]
45 .filter((entry) => typeof entry[1] !== 'string' && typeof entry[1] !== 'symbol')
46 .map(([k, v]) => k);
47 }
48}
49exports.AliasedMap = AliasedMap;