-
Notifications
You must be signed in to change notification settings - Fork 8
/
Chainable.js
447 lines (419 loc) · 11.7 KB
/
Chainable.js
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
const Iterator = require('./deps/symbols/iterator')
const Instance = require('./deps/symbols/instance')
const Primitive = require('./deps/symbols/primitive')
const isPrototypeOf = require('./deps/is/prototypeOf')
const isMap = require('./deps/is/map')
const isSet = require('./deps/is/set')
const isUndefined = require('./deps/is/undefined')
const isFunction = require('./deps/is/function')
const isString = require('./deps/is/string')
const isFalse = require('./deps/is/false')
const ObjectKeys = require('./deps/util/keys')
const ObjectDefine = require('./deps/define')
const ignored = require('./deps/ignored')
const ENV_DEVELOPMENT = require('./deps/env/dev')
const shouldClear = (key, property) =>
!ignored(key) &&
(isMap(property) || isSet(property) || (property && property.store))
const ComposeChainable = Target => {
/* istanbul ignore next: dev */
if (ENV_DEVELOPMENT) {
if (!Target || !Target.prototype) {
console.log({Target})
throw new TypeError('did not have a super class / target base')
}
}
/**
* @desc Trait class that can inherit any class passed into compose, extended by ChainedMap & ChainedSet
*
* @member Chainable
* @class Chainable
* @category Chainable
* @type {Chainable}
*
* @prop {Chainable | any} parent
* @prop {string} className
*
* {@link https://github.com/iluwatar/java-design-patterns/tree/master/chain chain-pattern}
* @see {@link chain-pattern}
*
* @see ChainedMap
* @see ChainedSet
*
* @tests Chainable
* @types Chainable
*/
class Chainable extends Target {
/**
* @since 0.0.1
* @memberOf Chainable
*
* @param {Chainable | any | ParentType} parent ParentType
* @constructor
*
* @example
*
* class ChainedMap extends Chainable {}
* const map = new ChainedMap()
* map.className
* //=> ChainedMap
*
*/
constructor(parent) {
super()
if (parent) this.parent = parent
this.className = this.constructor.name
}
/**
* @desc Iterator for looping values in the store
*
* @memberOf Chainable
* @since 0.5.0
*
* @type {generator}
* @return {Object} {value: undefined | any, done: true | false}
*
* @NOTE assigned to a variable so buble ignores it
* @see https://github.com/sindresorhus/quick-lru/blob/master/index.js
* @see https://stackoverflow.com/questions/36976832/what-is-the-meaning-of-symbol-iterator-in-this-context
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator
* @tests iteration
* @see this.store
*
* @example
*
* const chain = new Chain().set('eh', 1)
* for (var [key, val] of chain) console.log({[key]: val})
* //=> {eh: 1}
*
* @example
*
* *[Symbol.iterator](): void { for (const item of this.store) yield item }
*
* @example
*
* const {ChainedSet} = require('chain-able')
* const set = new ChainedSet()
* set.add('eh')
*
* for (const arr of set) {
* const [key, val] = arr
*
* key
* //=> 0
*
* val
* //=> 'eh'
*
* arr.length
* //=> 2
* }
*
*/
[Iterator]() {
const values = this.values()
const size = this.store.size
const entries = this.entries ? this.entries() : 0
const keys = entries === 0 ? new Array(size) : ObjectKeys(entries)
return {
i: 0,
next() {
let i = this.i
let key = i
const val = values[i]
if (entries) key = keys[i]
// done - no more values, or iteration reached size
if ((isUndefined(key) && isUndefined(val)) || size <= i) {
return {value: undefined, done: true}
}
this.i++
// return
return {value: [key, val], done: false}
},
}
}
/**
* @desc for ending nested chains
* @since 0.4.0
* @memberOf Chainable
*
* @return {Chainable | any}
*
* @see Chainable.parent
* @see FactoryChain
*
* @example
*
* const parent = 'eh'
* const child = newChain(parent)
* child.end()
* //=> 'eh'
*
*/
end() {
return this.parent
}
/**
* @desc when the condition is true,
* trueBrancher is called,
* else, falseBrancher is called
*
* @memberOf Chainable
* @since 4.0.0 <- added string-as-has(condition)
* @since 2.0.0
*
* @param {boolean | string} condition when string, checks this.get
* @param {Function} [trueBrancher=Function] called when true
* @param {Function} [falseBrancher=Function] called when false
* @return {Chainable} @chainable
*
* @example
*
*
* const prod = process.env.NODE_ENV === 'production'
* chains.when(prod, c => c.set('prod', true), c => c.set('prod', false))
*
*
*/
when(condition, trueBrancher, falseBrancher) {
if (condition) {
if (isFunction(trueBrancher)) {
if (isString(condition)) {
if (this.get(condition)) {
trueBrancher(this)
}
}
else {
trueBrancher(this)
}
}
}
else if (isFunction(falseBrancher)) {
falseBrancher(this)
}
return this
}
/**
* @desc clears the map,
* goes through this properties,
* calls .clear if they are instanceof Chainable or Map
*
* @memberOf Chainable
* @since 4.0.0 (moved only to Chainable, added option to clear this keys)
* @since 0.4.0 (in ChainedMap)
* @since 0.3.0 (in Chainable)
*
* @param {boolean | undefined} [clearPropertiesThatAreChainLike=true] checks properties on the object, if they are `chain-like`, clears them as well
* @return {Chainable} @chainable
*
* @see https://github.com/fliphub/flipchain/issues/2
* @see ChainedSet
* @see ChainedMap
*
* @example
*
* const chain = new Chain()
* chain.set('eh', 1)
* chain.entries()
* //=> {eh: 1}
* chain.clear()
* chain.entries()
* //=> {}
*
*/
clear(clearPropertiesThatAreChainLike) {
this.store.clear()
if (isFalse(clearPropertiesThatAreChainLike)) return this
const keys = ObjectKeys(this)
for (let k = 0; k < keys.length; k++) {
const key = keys[k]
const property = this[key]
if (shouldClear(key, property)) this[key].clear()
}
return this
}
/**
* @desc calls .delete on this.store.map
* @since 0.3.0
* @memberOf Chainable
*
* @param {Primitive} key on a Map: key referencing the value. on a Set: the index
* @return {Chainable}
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/has
* @see ChainedSet
* @see ChainedMap
*
* @example
*
* const chain = new Chain()
* chain.set('eh', 1)
* chain.get('eh')
* // => 1
* chain.delete('eh', 1)
* chain.get('eh')
* // => undefined
*
*/
delete(key) {
this.store.delete(key)
return this
}
/**
* @since 0.3.0
* @memberOf Chainable
*
* @param {any} keyOrValue key when Map, value when Set
* @return {boolean}
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has
*
* @example
*
* const chain = new Chain()
* chain.set('eh', 1).has('eh')
* //=> true
* chain.has('canada')
* //=> false
*
*/
has(keyOrValue) {
return this.store.has(keyOrValue)
}
/**
* @desc spreads the entries from ChainedMap.store.values
* allocates a new array, adds the values from the iterator
*
* @memberOf Chainable
* @since 0.4.0
*
* @return {Array<any>} toArr(this.store.values())
*
* @NOTE look at Chainable.constructor to ensure not to use `new Array...`
* @NOTE moved from ChainedMap and ChainedSet to Chainable @2.0.2
* @NOTE this was [...] & Array.from(this.store.values())
*
* {@link https://kangax.github.io/compat-table/es6/#test-Array_static_methods compat-array-static-methods}
* {@link https://stackoverflow.com/questions/20069828/how-to-convert-set-to-array set-to-array}
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/values mozilla-map-values}
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/values mozilla-set-values}
*
* @see {@link mozilla-map-values}
* @see {@link mozilla-set-values}
* @see {@link compat-array-static-methods}
* @see {@link set-to-array}
*
*
* @example
*
* const chain = new Chain()
* chain.set('eh', 1)
* chain.values()
* //=> [1]
*
*/
values() {
const allocated = new Array(this.store.size)
let i = 0
this.store.forEach(v => (allocated[i++] = v))
return allocated
// const size = this.store.size
// const allocated = new Array(size)
// // .forEach((value, index) => {
//
// const values = this.store.values()
//
// for (let index = 0; index < size; index++) {
// // const value = values[index]
// const value = values.next().value
// // console.log({value, index, values})
// allocated[index] = value
// }
//
// return allocated
}
/**
* @see http://2ality.com/2015/09/well-known-symbols-es6.html#default-tostring-tags
* @since 1.0.2
*
* @memberOf Chainable
*
* @param {string} hint enum[default, string, number]
* @return {Primitive}
*
* @example
*
* const chain = new Chain()
* chain.toNumber = () => 1
* +chain;
* //=> 1
* chain + 1
* //=>
*
* @example
*
* const chain = new Chain()
* chain.toString = () => 'eh'
* chain + ''
* //=> 'eh'
*
*/
[Primitive](hint) {
/* prettier-ignore */
/**
* hint === 'number'
* `s`tring is 115
* `n`umber is 110
* 110 & 4 = 1
* 115 & 4 = 0
*
* if (hint === 'string' && this.toJSON) return this.toJSON()
* else if (hint === 'number' && this.toNumber) return this.toNumber()
*/
if (hint === 'number' && this.toNumber) return this.toNumber()
// hint === 'string'
if (this.toJSON) return this.toJSON()
// hint === 'default'
return this.toString()
}
}
const ChainPrototype = Chainable.prototype
/**
* @memberOf Chainable
* @name length
* @method length
* @readonly
* @since 0.5.0
* @example for (var i = 0; i < chain.length; i++)
* @see ChainedMap.store
* @return {number}
*/
ObjectDefine(ChainPrototype, 'length', {
enumerable: false,
get() {
return this.store.size
},
})
ObjectDefine(ChainPrototype, Instance, {
enumerable: false,
value: instance =>
instance && (isPrototypeOf(ChainPrototype, instance) || instance.store),
})
return Chainable
}
const c = ComposeChainable(class {})
/**
* @since 3.0.0
* @func
* @example
*
* class Target {}
* const TargetChain = Chainable.compose(Target)
* const chain = new TargetChain()
* chain instanceof Target
* //=> true
*
*/
c.compose = ComposeChainable
module.exports = c