forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.js
More file actions
232 lines (204 loc) · 4.65 KB
/
Copy pathmap.js
File metadata and controls
232 lines (204 loc) · 4.65 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { getSafeProperty, isSafeProperty, setSafeProperty } from './customs.js'
import { isMap, isObject } from './is.js'
/**
* A map facade on a bare object.
*
* The small number of methods needed to implement a scope,
* forwarding on to the SafeProperty functions. Over time, the codebase
* will stop using this method, as all objects will be Maps, rather than
* more security prone objects.
*/
export class ObjectWrappingMap {
constructor (object) {
this.wrappedObject = object
this[Symbol.iterator] = this.entries
}
keys () {
return Object.keys(this.wrappedObject)
.filter(key => this.has(key))
.values()
}
get (key) {
return getSafeProperty(this.wrappedObject, key)
}
set (key, value) {
setSafeProperty(this.wrappedObject, key, value)
return this
}
has (key) {
return isSafeProperty(this.wrappedObject, key) && key in this.wrappedObject
}
entries () {
return mapIterator(this.keys(), key => [key, this.get(key)])
}
forEach (callback) {
for (const key of this.keys()) {
callback(this.get(key), key, this)
}
}
delete (key) {
if (isSafeProperty(this.wrappedObject, key)) {
delete this.wrappedObject[key]
}
}
clear () {
for (const key of this.keys()) {
this.delete(key)
}
}
get size () {
return Object.keys(this.wrappedObject).length
}
}
/**
* Create a map with two partitions: a and b.
* The set with bKeys determines which keys/values are read/written to map b,
* all other values are read/written to map a
*
* For example:
*
* const a = new Map()
* const b = new Map()
* const p = new PartitionedMap(a, b, new Set(['x', 'y']))
*
* In this case, values `x` and `y` are read/written to map `b`,
* all other values are read/written to map `a`.
*/
export class PartitionedMap {
/**
* @param {Map} a
* @param {Map} b
* @param {Set} bKeys
*/
constructor (a, b, bKeys) {
this.a = a
this.b = b
this.bKeys = bKeys
this[Symbol.iterator] = this.entries
}
get (key) {
return this.bKeys.has(key)
? this.b.get(key)
: this.a.get(key)
}
set (key, value) {
if (this.bKeys.has(key)) {
this.b.set(key, value)
} else {
this.a.set(key, value)
}
return this
}
has (key) {
return this.b.has(key) || this.a.has(key)
}
keys () {
return new Set([
...this.a.keys(),
...this.b.keys()
])[Symbol.iterator]()
}
entries () {
return mapIterator(this.keys(), key => [key, this.get(key)])
}
forEach (callback) {
for (const key of this.keys()) {
callback(this.get(key), key, this)
}
}
delete (key) {
return this.bKeys.has(key)
? this.b.delete(key)
: this.a.delete(key)
}
clear () {
this.a.clear()
this.b.clear()
}
get size () {
return [...this.keys()].length
}
}
/**
* Create a new iterator that maps over the provided iterator, applying a mapping function to each item
*/
function mapIterator (it, callback) {
return {
next: () => {
const n = it.next()
return (n.done)
? n
: {
value: callback(n.value),
done: false
}
}
}
}
/**
* Creates an empty map, or whatever your platform's polyfill is.
*
* @returns an empty Map or Map like object.
*/
export function createEmptyMap () {
return new Map()
}
/**
* Creates a Map from the given object.
*
* @param { Map | { [key: string]: unknown } | undefined } mapOrObject
* @returns
*/
export function createMap (mapOrObject) {
if (!mapOrObject) {
return createEmptyMap()
}
if (isMap(mapOrObject)) {
return mapOrObject
}
if (isObject(mapOrObject)) {
return new ObjectWrappingMap(mapOrObject)
}
throw new Error('createMap can create maps from objects or Maps')
}
/**
* Unwraps a map into an object.
*
* @param {Map} map
* @returns { [key: string]: unknown }
*/
export function toObject (map) {
if (map instanceof ObjectWrappingMap) {
return map.wrappedObject
}
const object = {}
for (const key of map.keys()) {
const value = map.get(key)
setSafeProperty(object, key, value)
}
return object
}
/**
* Copies the contents of key-value pairs from each `objects` in to `map`.
*
* Object is `objects` can be a `Map` or object.
*
* This is the `Map` analog to `Object.assign`.
*/
export function assign (map, ...objects) {
for (const args of objects) {
if (!args) {
continue
}
if (isMap(args)) {
for (const key of args.keys()) {
map.set(key, args.get(key))
}
} else if (isObject(args)) {
for (const key of Object.keys(args)) {
map.set(key, args[key])
}
}
}
return map
}