-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
dict.ts
79 lines (59 loc) · 1.65 KB
/
dict.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
namespace $ {
/** @deprecated */
export let $mol_dict_key = $mol_key
/**
* Dictionary with extended keys support
*/
export class $mol_dict< Key , Value > extends Map< Key , Value > {
get( key : Key ) {
return super.get( $mol_key( key ) as any )
}
has( key : Key ) {
return super.has( $mol_key( key ) as any )
}
set( key : Key , value : Value ) {
return super.set( $mol_key( key ) as any , value )
}
delete( key : Key ) {
return super.delete( $mol_key( key ) as any )
}
forEach( back : ( value : Value , key : Key , dict : Map< Key , Value > ) => void , context? : any ) {
return super.forEach( ( val , key , dict )=> {
if( typeof key === 'string' ) key = JSON.parse( key )
return back.call( this , val , key , dict )
} , context )
}
keys() {
const iterator = super.keys()
return {
[Symbol.iterator]() {
return this
},
next() {
const iteration = iterator.next()
if( iteration.done ) return iteration
iteration.value = JSON.parse( iteration.value as any as string )
return iteration
}
} as MapIterator<Key>
}
entries() {
const iterator = super.entries()
return {
[Symbol.iterator]() {
return this
},
next() {
const iteration = iterator.next()
if( iteration.done ) return iteration
iteration.value = [ JSON.parse( iteration.value[0] as any as string ), iteration.value[1] ]
// iteration.value[0] = JSON.parse( iteration.value[0] as any as string )
return iteration
}
} as MapIterator<[Key, Value]>
}
[Symbol.iterator]() {
return this.entries()
}
}
}