• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5const _keys = Symbol('keys')
6const _normKey = Symbol('normKey')
7const normalize = s => s.normalize('NFKD').toLowerCase()
8const OGMap = Map
9module.exports = class Map extends OGMap {
10  constructor (items = []) {
11    super()
12    this[_keys] = new OGMap()
13    for (const [key, val] of items) {
14      this.set(key, val)
15    }
16  }
17
18  [_normKey] (key) {
19    return typeof key === 'string' ? normalize(key) : key
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