codekingpro/portable-devtools
114k
1// package children are represented with a Map object, but many file systems
2// are case-insensitive and unicode-normalizing, so we need to treat
3// node.children.get('FOO') and node.children.get('foo') as the same thing.
4
5module.exports = class CIMap extends Map {
6 #keys = new Map()
7
8 constructor (items = []) {
9 super()
10 for (const [key, val] of items) {
11 this.set(key, val)
12 }
13 }
14
15 #normKey (key) {
16 if (typeof key !== 'string') {
17 return key
18 }
19 return key.normalize('NFKD').toLowerCase()
20 }
21
22 get (key) {
23 const normKey = this.#normKey(key)
24 return this.#keys.has(normKey) ? super.get(this.#keys.get(normKey))
25 : undefined
26 }
27
28 set (key, val) {
29 const normKey = this.#normKey(key)
30 if (this.#keys.has(normKey)) {
31 super.delete(this.#keys.get(normKey))
32 }
33 this.#keys.set(normKey, key)
34 return super.set(key, val)
35 }
36
37 delete (key) {
38 const normKey = this.#normKey(key)
39 if (this.#keys.has(normKey)) {
40 const prevKey = this.#keys.get(normKey)
41 this.#keys.delete(normKey)
42 return super.delete(prevKey)
43 }
44 }
45
46 has (key) {
47 const normKey = this.#normKey(key)
48 return this.#keys.has(normKey) && super.has(this.#keys.get(normKey))
49 }
50}
51 