@radio-garden/ditojs-utils 2.85.0-0.5067ad799
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/package.json +43 -0
- package/src/__snapshots__/index.test.js.snap +88 -0
- package/src/array/flatten.js +14 -0
- package/src/array/flattten.test.js +29 -0
- package/src/array/index.js +2 -0
- package/src/array/shuffle.js +11 -0
- package/src/array/shuffle.test.js +25 -0
- package/src/base/base.js +118 -0
- package/src/base/base.test.js +590 -0
- package/src/base/index.js +1 -0
- package/src/class/index.js +1 -0
- package/src/class/mixin.js +29 -0
- package/src/class/mixin.test.js +70 -0
- package/src/dataPath/getEntriesAtDataPath.js +67 -0
- package/src/dataPath/getEntriesAtDataPath.test.js +204 -0
- package/src/dataPath/getValueAtDataPath.js +45 -0
- package/src/dataPath/getValueAtDataPath.test.js +140 -0
- package/src/dataPath/index.js +6 -0
- package/src/dataPath/normalizeDataPath.js +27 -0
- package/src/dataPath/normalizeDataPath.test.js +36 -0
- package/src/dataPath/parseDataPath.js +16 -0
- package/src/dataPath/parseDataPath.test.js +67 -0
- package/src/dataPath/setDataPathEntries.js +8 -0
- package/src/dataPath/setDataPathEntries.test.js +36 -0
- package/src/dataPath/setValueAtDataPath.js +13 -0
- package/src/dataPath/setValueAtDataPath.test.js +34 -0
- package/src/function/deprecate.js +9 -0
- package/src/function/index.js +4 -0
- package/src/function/toAsync.js +9 -0
- package/src/function/toAsync.test.js +31 -0
- package/src/function/toCallback.js +11 -0
- package/src/function/toCallback.test.js +29 -0
- package/src/function/toPromiseCallback.js +9 -0
- package/src/function/toPromiseCallback.test.js +24 -0
- package/src/html/escapeHtml.js +11 -0
- package/src/html/escapeHtml.test.js +19 -0
- package/src/html/index.js +2 -0
- package/src/html/stripHtml.js +23 -0
- package/src/html/stripHtml.test.js +37 -0
- package/src/index.js +10 -0
- package/src/index.test.js +7 -0
- package/src/object/asCallback.js +9 -0
- package/src/object/clone.js +75 -0
- package/src/object/clone.test.js +131 -0
- package/src/object/equals.js +36 -0
- package/src/object/equals.test.js +269 -0
- package/src/object/groupBy.js +15 -0
- package/src/object/groupBy.test.js +70 -0
- package/src/object/index.js +8 -0
- package/src/object/mapKeys.js +7 -0
- package/src/object/mapKeys.test.js +16 -0
- package/src/object/mapValues.js +9 -0
- package/src/object/mapValues.test.js +38 -0
- package/src/object/mergeDeeply.js +47 -0
- package/src/object/mergeDeeply.test.js +152 -0
- package/src/object/pick.js +11 -0
- package/src/object/pick.test.js +23 -0
- package/src/object/pickBy.js +11 -0
- package/src/object/pickBy.test.js +48 -0
- package/src/promise/index.js +2 -0
- package/src/promise/mapConcurrently.js +33 -0
- package/src/promise/mapSequentially.js +9 -0
- package/src/string/camelize.js +14 -0
- package/src/string/camelize.test.js +37 -0
- package/src/string/capitalize.js +7 -0
- package/src/string/capitalize.test.js +33 -0
- package/src/string/decamelize.js +27 -0
- package/src/string/decamelize.test.js +83 -0
- package/src/string/deindent.js +69 -0
- package/src/string/deindent.test.js +181 -0
- package/src/string/escapeRegexp.js +3 -0
- package/src/string/format.js +109 -0
- package/src/string/format.test.js +196 -0
- package/src/string/formatDate.js +13 -0
- package/src/string/formatDate.test.js +28 -0
- package/src/string/getCommonPrefix.js +35 -0
- package/src/string/getCommonPrefix.test.js +23 -0
- package/src/string/index.js +15 -0
- package/src/string/isAbsoluteUrl.js +7 -0
- package/src/string/isAbsoluteUrl.test.js +15 -0
- package/src/string/isCreditCard.js +21 -0
- package/src/string/isCreditCard.test.js +50 -0
- package/src/string/isDomain.js +9 -0
- package/src/string/isDomain.test.js +15 -0
- package/src/string/isEmail.js +6 -0
- package/src/string/isEmail.test.js +37 -0
- package/src/string/isHostname.js +8 -0
- package/src/string/isHostname.test.js +12 -0
- package/src/string/isUrl.js +23 -0
- package/src/string/isUrl.test.js +1595 -0
- package/src/string/labelize.js +17 -0
- package/src/string/labelize.test.js +39 -0
- package/src/timer/debounce.js +34 -0
- package/src/timer/debounce.test.js +101 -0
- package/src/timer/debounceAsync.js +60 -0
- package/src/timer/debounceAsync.test.js +143 -0
- package/src/timer/index.js +2 -0
- package/types/index.d.ts +939 -0
- package/types/tests/base.test-d.ts +172 -0
- package/types/tests/datapath.test-d.ts +75 -0
- package/types/tests/function.test-d.ts +137 -0
- package/types/tests/object.test-d.ts +190 -0
- package/types/tests/promise.test-d.ts +66 -0
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isPlainObject,
|
|
3
|
+
isPlainArray,
|
|
4
|
+
isObject,
|
|
5
|
+
isArray,
|
|
6
|
+
isModule,
|
|
7
|
+
isFunction,
|
|
8
|
+
isString,
|
|
9
|
+
isBoolean,
|
|
10
|
+
isNumber,
|
|
11
|
+
isDate,
|
|
12
|
+
isRegExp,
|
|
13
|
+
isPromise,
|
|
14
|
+
isAsync,
|
|
15
|
+
isInteger,
|
|
16
|
+
isArrayLike,
|
|
17
|
+
isEmpty,
|
|
18
|
+
asObject,
|
|
19
|
+
asArray,
|
|
20
|
+
asFunction
|
|
21
|
+
} from './base.js'
|
|
22
|
+
|
|
23
|
+
import * as module from './index.js'
|
|
24
|
+
|
|
25
|
+
const object = { a: 1 }
|
|
26
|
+
const array = [1, 2, 3]
|
|
27
|
+
const arrayLike = { 0: 1, 1: 2, 2: 3, length: 3 }
|
|
28
|
+
const string = 'a'
|
|
29
|
+
const number = 1
|
|
30
|
+
const boolean = true
|
|
31
|
+
const date = new Date()
|
|
32
|
+
const regexp = /x/
|
|
33
|
+
const symbol = Symbol('a')
|
|
34
|
+
const instance = new (class {})()
|
|
35
|
+
const promise = new Promise(() => {})
|
|
36
|
+
const func = function () {}
|
|
37
|
+
const lambda = () => {}
|
|
38
|
+
const asyncFunc = async function () {}
|
|
39
|
+
const asyncLambda = async function () {}
|
|
40
|
+
|
|
41
|
+
describe('isPlainObject()', () => {
|
|
42
|
+
describe.each([
|
|
43
|
+
[null, false],
|
|
44
|
+
[module, false],
|
|
45
|
+
[object, true],
|
|
46
|
+
[array, false],
|
|
47
|
+
[arrayLike, true],
|
|
48
|
+
[string, false],
|
|
49
|
+
[number, false],
|
|
50
|
+
[boolean, false],
|
|
51
|
+
[date, false],
|
|
52
|
+
[regexp, false],
|
|
53
|
+
[symbol, false],
|
|
54
|
+
[instance, false],
|
|
55
|
+
[promise, false],
|
|
56
|
+
[func, false],
|
|
57
|
+
[lambda, false],
|
|
58
|
+
[asyncFunc, false],
|
|
59
|
+
[asyncLambda, false]
|
|
60
|
+
])(
|
|
61
|
+
'isPlainObject(%o)',
|
|
62
|
+
(value, expected) => {
|
|
63
|
+
it(`returns ${expected}`, () => {
|
|
64
|
+
expect(isPlainObject(value)).toBe(expected)
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
describe('isPlainArray()', () => {
|
|
71
|
+
describe.each([
|
|
72
|
+
[null, false],
|
|
73
|
+
[module, false],
|
|
74
|
+
[object, false],
|
|
75
|
+
[array, true],
|
|
76
|
+
[arrayLike, false],
|
|
77
|
+
[string, false],
|
|
78
|
+
[number, false],
|
|
79
|
+
[boolean, false],
|
|
80
|
+
[date, false],
|
|
81
|
+
[regexp, false],
|
|
82
|
+
[symbol, false],
|
|
83
|
+
[instance, false],
|
|
84
|
+
[promise, false],
|
|
85
|
+
[func, false],
|
|
86
|
+
[lambda, false],
|
|
87
|
+
[asyncFunc, false],
|
|
88
|
+
[asyncLambda, false]
|
|
89
|
+
])(
|
|
90
|
+
'isPlainArray(%o)',
|
|
91
|
+
(value, expected) => {
|
|
92
|
+
it(`returns ${expected}`, () => {
|
|
93
|
+
expect(isPlainArray(value)).toBe(expected)
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
describe('isObject()', () => {
|
|
100
|
+
describe.each([
|
|
101
|
+
[null, false],
|
|
102
|
+
[module, true],
|
|
103
|
+
[object, true],
|
|
104
|
+
[array, false],
|
|
105
|
+
[arrayLike, true],
|
|
106
|
+
[string, false],
|
|
107
|
+
[number, false],
|
|
108
|
+
[boolean, false],
|
|
109
|
+
[Object(number), true],
|
|
110
|
+
[Object(string), true],
|
|
111
|
+
[Object(boolean), true],
|
|
112
|
+
[date, true],
|
|
113
|
+
[regexp, true],
|
|
114
|
+
[symbol, false],
|
|
115
|
+
[instance, true],
|
|
116
|
+
[promise, true],
|
|
117
|
+
[func, false],
|
|
118
|
+
[lambda, false],
|
|
119
|
+
[asyncFunc, false],
|
|
120
|
+
[asyncLambda, false]
|
|
121
|
+
])(
|
|
122
|
+
'isObject(%o)',
|
|
123
|
+
(value, expected) => {
|
|
124
|
+
it(`returns ${expected}`, () => {
|
|
125
|
+
expect(isObject(value)).toBe(expected)
|
|
126
|
+
})
|
|
127
|
+
}
|
|
128
|
+
)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
describe('isArray()', () => {
|
|
132
|
+
describe.each([
|
|
133
|
+
[null, false],
|
|
134
|
+
[module, false],
|
|
135
|
+
[object, false],
|
|
136
|
+
[array, true],
|
|
137
|
+
[arrayLike, false],
|
|
138
|
+
[string, false],
|
|
139
|
+
[number, false],
|
|
140
|
+
[boolean, false],
|
|
141
|
+
[date, false],
|
|
142
|
+
[regexp, false],
|
|
143
|
+
[symbol, false],
|
|
144
|
+
[instance, false],
|
|
145
|
+
[promise, false],
|
|
146
|
+
[func, false],
|
|
147
|
+
[lambda, false],
|
|
148
|
+
[asyncFunc, false],
|
|
149
|
+
[asyncLambda, false]
|
|
150
|
+
])(
|
|
151
|
+
'isArray(%o)',
|
|
152
|
+
(value, expected) => {
|
|
153
|
+
it(`returns ${expected}`, () => {
|
|
154
|
+
expect(isArray(value)).toBe(expected)
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
)
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
describe('isModule()', () => {
|
|
161
|
+
describe.each([
|
|
162
|
+
[null, false],
|
|
163
|
+
[module, true],
|
|
164
|
+
[object, false],
|
|
165
|
+
[array, false],
|
|
166
|
+
[arrayLike, false],
|
|
167
|
+
[string, false],
|
|
168
|
+
[number, false],
|
|
169
|
+
[boolean, false],
|
|
170
|
+
[date, false],
|
|
171
|
+
[regexp, false],
|
|
172
|
+
[symbol, false],
|
|
173
|
+
[instance, false],
|
|
174
|
+
[promise, false],
|
|
175
|
+
[func, false],
|
|
176
|
+
[lambda, false],
|
|
177
|
+
[asyncFunc, false],
|
|
178
|
+
[asyncLambda, false]
|
|
179
|
+
])(
|
|
180
|
+
'isModule(%o)',
|
|
181
|
+
(value, expected) => {
|
|
182
|
+
it(`returns ${expected}`, () => {
|
|
183
|
+
expect(isModule(value)).toBe(expected)
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
)
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
describe('isFunction()', () => {
|
|
190
|
+
describe.each([
|
|
191
|
+
[null, false],
|
|
192
|
+
[module, false],
|
|
193
|
+
[object, false],
|
|
194
|
+
[array, false],
|
|
195
|
+
[arrayLike, false],
|
|
196
|
+
[string, false],
|
|
197
|
+
[number, false],
|
|
198
|
+
[boolean, false],
|
|
199
|
+
[date, false],
|
|
200
|
+
[regexp, false],
|
|
201
|
+
[symbol, false],
|
|
202
|
+
[instance, false],
|
|
203
|
+
[promise, false],
|
|
204
|
+
[func, true],
|
|
205
|
+
[lambda, true],
|
|
206
|
+
[asyncFunc, true],
|
|
207
|
+
[asyncLambda, true]
|
|
208
|
+
])(
|
|
209
|
+
'isFunction(%o)',
|
|
210
|
+
(value, expected) => {
|
|
211
|
+
it(`returns ${expected}`, () => {
|
|
212
|
+
expect(isFunction(value)).toBe(expected)
|
|
213
|
+
})
|
|
214
|
+
}
|
|
215
|
+
)
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
describe('isNumber()', () => {
|
|
219
|
+
describe.each([
|
|
220
|
+
[null, false],
|
|
221
|
+
[module, false],
|
|
222
|
+
[object, false],
|
|
223
|
+
[array, false],
|
|
224
|
+
[arrayLike, false],
|
|
225
|
+
[string, false],
|
|
226
|
+
[number, true],
|
|
227
|
+
[Object(number), true],
|
|
228
|
+
[boolean, false],
|
|
229
|
+
[date, false],
|
|
230
|
+
[regexp, false],
|
|
231
|
+
[symbol, false],
|
|
232
|
+
[instance, false],
|
|
233
|
+
[promise, false],
|
|
234
|
+
[func, false],
|
|
235
|
+
[lambda, false],
|
|
236
|
+
[asyncFunc, false],
|
|
237
|
+
[asyncLambda, false]
|
|
238
|
+
])(
|
|
239
|
+
'isNumber(%o)',
|
|
240
|
+
(value, expected) => {
|
|
241
|
+
it(`returns ${expected}`, () => {
|
|
242
|
+
expect(isNumber(value)).toBe(expected)
|
|
243
|
+
})
|
|
244
|
+
}
|
|
245
|
+
)
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
describe('isInteger()', () => {
|
|
249
|
+
describe.each([
|
|
250
|
+
[null, false],
|
|
251
|
+
[module, false],
|
|
252
|
+
[object, false],
|
|
253
|
+
[array, false],
|
|
254
|
+
[arrayLike, false],
|
|
255
|
+
[string, false],
|
|
256
|
+
[number, true],
|
|
257
|
+
[boolean, false],
|
|
258
|
+
[date, false],
|
|
259
|
+
[regexp, false],
|
|
260
|
+
[symbol, false],
|
|
261
|
+
[instance, false],
|
|
262
|
+
[promise, false],
|
|
263
|
+
[func, false],
|
|
264
|
+
[lambda, false],
|
|
265
|
+
[asyncFunc, false],
|
|
266
|
+
[asyncLambda, false]
|
|
267
|
+
])(
|
|
268
|
+
'isInteger(%o)',
|
|
269
|
+
(value, expected) => {
|
|
270
|
+
it(`returns ${expected}`, () => {
|
|
271
|
+
expect(isInteger(value)).toBe(expected)
|
|
272
|
+
})
|
|
273
|
+
}
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
describe.each([
|
|
277
|
+
13,
|
|
278
|
+
123,
|
|
279
|
+
0,
|
|
280
|
+
-0,
|
|
281
|
+
1,
|
|
282
|
+
-1,
|
|
283
|
+
+1
|
|
284
|
+
])(
|
|
285
|
+
'isInteger(%o)',
|
|
286
|
+
str => {
|
|
287
|
+
it('returns true', () => {
|
|
288
|
+
expect(isInteger(str)).toBe(true)
|
|
289
|
+
})
|
|
290
|
+
}
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
describe.each([
|
|
294
|
+
-0.00000000001,
|
|
295
|
+
123.123
|
|
296
|
+
])(
|
|
297
|
+
'isInteger(%o)',
|
|
298
|
+
str => {
|
|
299
|
+
it('returns false', () => {
|
|
300
|
+
expect(isInteger(str)).toBe(false)
|
|
301
|
+
})
|
|
302
|
+
}
|
|
303
|
+
)
|
|
304
|
+
})
|
|
305
|
+
|
|
306
|
+
describe('isString()', () => {
|
|
307
|
+
describe.each([
|
|
308
|
+
[null, false],
|
|
309
|
+
[object, false],
|
|
310
|
+
[array, false],
|
|
311
|
+
[arrayLike, false],
|
|
312
|
+
[string, true],
|
|
313
|
+
[Object(string), true],
|
|
314
|
+
[number, false],
|
|
315
|
+
[boolean, false],
|
|
316
|
+
[date, false],
|
|
317
|
+
[regexp, false],
|
|
318
|
+
[symbol, false],
|
|
319
|
+
[instance, false],
|
|
320
|
+
[promise, false],
|
|
321
|
+
[func, false],
|
|
322
|
+
[lambda, false],
|
|
323
|
+
[asyncFunc, false],
|
|
324
|
+
[asyncLambda, false]
|
|
325
|
+
])(
|
|
326
|
+
'isString(%o)',
|
|
327
|
+
(value, expected) => {
|
|
328
|
+
it(`returns ${expected}`, () => {
|
|
329
|
+
expect(isString(value)).toBe(expected)
|
|
330
|
+
})
|
|
331
|
+
}
|
|
332
|
+
)
|
|
333
|
+
})
|
|
334
|
+
|
|
335
|
+
describe('isBoolean()', () => {
|
|
336
|
+
describe.each([
|
|
337
|
+
[null, false],
|
|
338
|
+
[object, false],
|
|
339
|
+
[array, false],
|
|
340
|
+
[arrayLike, false],
|
|
341
|
+
[string, false],
|
|
342
|
+
[number, false],
|
|
343
|
+
[boolean, true],
|
|
344
|
+
[Object(boolean), true],
|
|
345
|
+
[date, false],
|
|
346
|
+
[regexp, false],
|
|
347
|
+
[symbol, false],
|
|
348
|
+
[instance, false],
|
|
349
|
+
[promise, false],
|
|
350
|
+
[func, false],
|
|
351
|
+
[lambda, false],
|
|
352
|
+
[asyncFunc, false],
|
|
353
|
+
[asyncLambda, false]
|
|
354
|
+
])(
|
|
355
|
+
'isBoolean(%o)',
|
|
356
|
+
(value, expected) => {
|
|
357
|
+
it(`returns ${expected}`, () => {
|
|
358
|
+
expect(isBoolean(value)).toBe(expected)
|
|
359
|
+
})
|
|
360
|
+
}
|
|
361
|
+
)
|
|
362
|
+
})
|
|
363
|
+
|
|
364
|
+
describe('isDate()', () => {
|
|
365
|
+
describe.each([
|
|
366
|
+
[null, false],
|
|
367
|
+
[object, false],
|
|
368
|
+
[array, false],
|
|
369
|
+
[arrayLike, false],
|
|
370
|
+
[string, false],
|
|
371
|
+
[number, false],
|
|
372
|
+
[boolean, false],
|
|
373
|
+
[date, true],
|
|
374
|
+
[regexp, false],
|
|
375
|
+
[symbol, false],
|
|
376
|
+
[instance, false],
|
|
377
|
+
[promise, false],
|
|
378
|
+
[func, false],
|
|
379
|
+
[lambda, false],
|
|
380
|
+
[asyncFunc, false],
|
|
381
|
+
[asyncLambda, false]
|
|
382
|
+
])(
|
|
383
|
+
'isDate(%o)',
|
|
384
|
+
(value, expected) => {
|
|
385
|
+
it(`returns ${expected}`, () => {
|
|
386
|
+
expect(isDate(value)).toBe(expected)
|
|
387
|
+
})
|
|
388
|
+
}
|
|
389
|
+
)
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
describe('isRegExp()', () => {
|
|
393
|
+
describe.each([
|
|
394
|
+
[null, false],
|
|
395
|
+
[object, false],
|
|
396
|
+
[array, false],
|
|
397
|
+
[arrayLike, false],
|
|
398
|
+
[string, false],
|
|
399
|
+
[number, false],
|
|
400
|
+
[boolean, false],
|
|
401
|
+
[date, false],
|
|
402
|
+
[regexp, true],
|
|
403
|
+
[symbol, false],
|
|
404
|
+
[instance, false],
|
|
405
|
+
[promise, false],
|
|
406
|
+
[func, false],
|
|
407
|
+
[lambda, false],
|
|
408
|
+
[asyncFunc, false],
|
|
409
|
+
[asyncLambda, false]
|
|
410
|
+
])(
|
|
411
|
+
'isRegExp(%o)',
|
|
412
|
+
(value, expected) => {
|
|
413
|
+
it(`returns ${expected}`, () => {
|
|
414
|
+
expect(isRegExp(value)).toBe(expected)
|
|
415
|
+
})
|
|
416
|
+
}
|
|
417
|
+
)
|
|
418
|
+
})
|
|
419
|
+
|
|
420
|
+
describe('isPromise()', () => {
|
|
421
|
+
const thenable = {
|
|
422
|
+
then() {}
|
|
423
|
+
}
|
|
424
|
+
const thenableCatchable = {
|
|
425
|
+
then() {},
|
|
426
|
+
catch() {}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
describe.each([
|
|
430
|
+
[null, false],
|
|
431
|
+
[object, false],
|
|
432
|
+
[array, false],
|
|
433
|
+
[arrayLike, false],
|
|
434
|
+
[string, false],
|
|
435
|
+
[number, false],
|
|
436
|
+
[boolean, false],
|
|
437
|
+
[date, false],
|
|
438
|
+
[regexp, false],
|
|
439
|
+
[symbol, false],
|
|
440
|
+
[instance, false],
|
|
441
|
+
[promise, true],
|
|
442
|
+
[thenable, false],
|
|
443
|
+
[thenableCatchable, true],
|
|
444
|
+
[func, false],
|
|
445
|
+
[lambda, false],
|
|
446
|
+
[asyncFunc, false],
|
|
447
|
+
[asyncLambda, false]
|
|
448
|
+
])(
|
|
449
|
+
'isPromise(%o)',
|
|
450
|
+
(value, expected) => {
|
|
451
|
+
it(`returns ${expected}`, () => {
|
|
452
|
+
expect(isPromise(value)).toBe(expected)
|
|
453
|
+
})
|
|
454
|
+
}
|
|
455
|
+
)
|
|
456
|
+
})
|
|
457
|
+
|
|
458
|
+
describe('isAsync()', () => {
|
|
459
|
+
describe.each([
|
|
460
|
+
[null, false],
|
|
461
|
+
[object, false],
|
|
462
|
+
[array, false],
|
|
463
|
+
[arrayLike, false],
|
|
464
|
+
[string, false],
|
|
465
|
+
[number, false],
|
|
466
|
+
[boolean, false],
|
|
467
|
+
[date, false],
|
|
468
|
+
[regexp, false],
|
|
469
|
+
[symbol, false],
|
|
470
|
+
[instance, false],
|
|
471
|
+
[promise, false],
|
|
472
|
+
[func, false],
|
|
473
|
+
[lambda, false],
|
|
474
|
+
[asyncFunc, true],
|
|
475
|
+
[asyncLambda, true]
|
|
476
|
+
])(
|
|
477
|
+
'isAsync(%o)',
|
|
478
|
+
(value, expected) => {
|
|
479
|
+
it(`returns ${expected}`, () => {
|
|
480
|
+
expect(isAsync(value)).toBe(expected)
|
|
481
|
+
})
|
|
482
|
+
}
|
|
483
|
+
)
|
|
484
|
+
})
|
|
485
|
+
|
|
486
|
+
describe('isArrayLike()', () => {
|
|
487
|
+
describe.each([
|
|
488
|
+
[null, false],
|
|
489
|
+
[object, false],
|
|
490
|
+
[array, true],
|
|
491
|
+
[arrayLike, true],
|
|
492
|
+
[string, true],
|
|
493
|
+
[number, false],
|
|
494
|
+
[boolean, false],
|
|
495
|
+
[date, false],
|
|
496
|
+
[regexp, false],
|
|
497
|
+
[symbol, false],
|
|
498
|
+
[instance, false],
|
|
499
|
+
[promise, false],
|
|
500
|
+
[func, false],
|
|
501
|
+
[lambda, false],
|
|
502
|
+
[asyncFunc, false],
|
|
503
|
+
[asyncLambda, false]
|
|
504
|
+
])(
|
|
505
|
+
'isArrayLike(%o)',
|
|
506
|
+
(value, expected) => {
|
|
507
|
+
it(`returns ${expected}`, () => {
|
|
508
|
+
expect(isArrayLike(value)).toBe(expected)
|
|
509
|
+
})
|
|
510
|
+
}
|
|
511
|
+
)
|
|
512
|
+
})
|
|
513
|
+
|
|
514
|
+
describe('isEmpty()', () => {
|
|
515
|
+
describe.each([
|
|
516
|
+
[null, true],
|
|
517
|
+
[object, false],
|
|
518
|
+
[{}, true],
|
|
519
|
+
[array, false],
|
|
520
|
+
[[], true],
|
|
521
|
+
[string, false],
|
|
522
|
+
['', true]
|
|
523
|
+
])(
|
|
524
|
+
'isEmpty(%o)',
|
|
525
|
+
(value, expected) => {
|
|
526
|
+
it(`returns ${expected}`, () => {
|
|
527
|
+
expect(isEmpty(value)).toBe(expected)
|
|
528
|
+
})
|
|
529
|
+
}
|
|
530
|
+
)
|
|
531
|
+
})
|
|
532
|
+
|
|
533
|
+
describe('asObject()', () => {
|
|
534
|
+
it('returns received objects', () => {
|
|
535
|
+
expect(asObject(object)).toBe(object)
|
|
536
|
+
})
|
|
537
|
+
|
|
538
|
+
it('converts a number to an object', () => {
|
|
539
|
+
const actual = asObject(number)
|
|
540
|
+
expect(actual).toBeInstanceOf(Object)
|
|
541
|
+
expect(actual.valueOf()).toBe(number)
|
|
542
|
+
})
|
|
543
|
+
|
|
544
|
+
it('converts a string to an object', () => {
|
|
545
|
+
const actual = asObject(string)
|
|
546
|
+
expect(actual).toBeInstanceOf(Object)
|
|
547
|
+
expect(actual.valueOf()).toBe(string)
|
|
548
|
+
})
|
|
549
|
+
|
|
550
|
+
it('converts a boolean to an object', () => {
|
|
551
|
+
const actual = asObject(boolean)
|
|
552
|
+
expect(actual).toBeInstanceOf(Object)
|
|
553
|
+
expect(actual.valueOf()).toBe(boolean)
|
|
554
|
+
})
|
|
555
|
+
|
|
556
|
+
it('does not convert null or undefined', () => {
|
|
557
|
+
expect(asObject(null)).toBe(null)
|
|
558
|
+
expect(asObject(undefined)).toBe(undefined)
|
|
559
|
+
})
|
|
560
|
+
})
|
|
561
|
+
|
|
562
|
+
describe('asArray()', () => {
|
|
563
|
+
it('returns received objects', () => {
|
|
564
|
+
expect(asArray(array)).toBe(array)
|
|
565
|
+
})
|
|
566
|
+
|
|
567
|
+
it('converts a primitive value to an array', () => {
|
|
568
|
+
expect(asArray(number)).toStrictEqual([number])
|
|
569
|
+
})
|
|
570
|
+
|
|
571
|
+
it('converts `null` to an array containing `null`', () => {
|
|
572
|
+
expect(asArray(null)).toStrictEqual([null])
|
|
573
|
+
})
|
|
574
|
+
|
|
575
|
+
it('converts `undefined` to an empty array', () => {
|
|
576
|
+
expect(asArray(undefined)).toStrictEqual([])
|
|
577
|
+
})
|
|
578
|
+
})
|
|
579
|
+
|
|
580
|
+
describe('asFunction()', () => {
|
|
581
|
+
it('returns received functions', () => {
|
|
582
|
+
expect(asFunction(func)).toBe(func)
|
|
583
|
+
})
|
|
584
|
+
|
|
585
|
+
it('converts a value to a function returning this value', () => {
|
|
586
|
+
const actual = asFunction(number)
|
|
587
|
+
expect(actual).toBeInstanceOf(Function)
|
|
588
|
+
expect(actual()).toEqual(number)
|
|
589
|
+
})
|
|
590
|
+
})
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './base.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './mixin.js'
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// `mixinsMap` is a weak-map that links classes to the mixin functions that
|
|
2
|
+
// produced them.
|
|
3
|
+
const mixinsMap = new WeakMap()
|
|
4
|
+
|
|
5
|
+
export function mixin(mixinFunction) {
|
|
6
|
+
return function (targetClass) {
|
|
7
|
+
// Prevent multiple application of the same mixins in nested inheritance
|
|
8
|
+
// through keeping track of what was already applied in weak-maps:
|
|
9
|
+
if (targetClass && !hasMixin(targetClass, mixinFunction)) {
|
|
10
|
+
const mixinClass = mixinFunction(targetClass)
|
|
11
|
+
mixinsMap.set(mixinClass, mixinFunction)
|
|
12
|
+
return mixinClass
|
|
13
|
+
}
|
|
14
|
+
return targetClass
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function hasMixin(targetClass, mixinFunction) {
|
|
19
|
+
// Walk up the inheritance chain and check on every level if this mixin was
|
|
20
|
+
// already applied.
|
|
21
|
+
let currentClass = targetClass
|
|
22
|
+
while (currentClass) {
|
|
23
|
+
if (mixinsMap.get(currentClass) === mixinFunction) {
|
|
24
|
+
return true
|
|
25
|
+
}
|
|
26
|
+
currentClass = Object.getPrototypeOf(currentClass)
|
|
27
|
+
}
|
|
28
|
+
return false
|
|
29
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { mixin } from './mixin.js'
|
|
2
|
+
|
|
3
|
+
describe('mixin()', () => {
|
|
4
|
+
class SomeClass {
|
|
5
|
+
function() {}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const Something1Mixin = mixin(
|
|
9
|
+
Class =>
|
|
10
|
+
class extends Class {
|
|
11
|
+
function1() {}
|
|
12
|
+
}
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
const Something2Mixin = mixin(
|
|
16
|
+
Class =>
|
|
17
|
+
class extends Class {
|
|
18
|
+
function2() {}
|
|
19
|
+
}
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
it('should apply mixin to class', () => {
|
|
23
|
+
const NewClass = Something1Mixin(SomeClass)
|
|
24
|
+
const instance = new NewClass()
|
|
25
|
+
expect(instance.function).toBeInstanceOf(Function)
|
|
26
|
+
expect(instance.function1).toBeInstanceOf(Function)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('should apply multiple mixins to class', () => {
|
|
30
|
+
const NewClass = Something2Mixin(Something1Mixin(SomeClass))
|
|
31
|
+
const instance = new NewClass()
|
|
32
|
+
expect(instance.function).toBeInstanceOf(Function)
|
|
33
|
+
expect(instance.function1).toBeInstanceOf(Function)
|
|
34
|
+
expect(instance.function2).toBeInstanceOf(Function)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('should not apply mixin more than once', () => {
|
|
38
|
+
const NewClass1 = Something1Mixin(SomeClass)
|
|
39
|
+
const NewClass2 = Something1Mixin(NewClass1)
|
|
40
|
+
expect(NewClass2).toBe(NewClass1)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('should not affect the base class', () => {
|
|
44
|
+
const NewClass1 = Something1Mixin(SomeClass)
|
|
45
|
+
const NewClass2 = Something2Mixin(SomeClass)
|
|
46
|
+
|
|
47
|
+
const instance = new SomeClass()
|
|
48
|
+
const instance1 = new NewClass1()
|
|
49
|
+
const instance2 = new NewClass2()
|
|
50
|
+
|
|
51
|
+
expect(instance.function).toBeInstanceOf(Function)
|
|
52
|
+
expect(instance.function1).toBeUndefined()
|
|
53
|
+
expect(instance.function2).toBeUndefined()
|
|
54
|
+
|
|
55
|
+
expect(instance1.function).toBeInstanceOf(Function)
|
|
56
|
+
expect(instance1.function1).toBeInstanceOf(Function)
|
|
57
|
+
expect(instance1.function2).toBeUndefined()
|
|
58
|
+
|
|
59
|
+
expect(instance2.function).toBeInstanceOf(Function)
|
|
60
|
+
expect(instance2.function1).toBeUndefined()
|
|
61
|
+
expect(instance2.function2).toBeInstanceOf(Function)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it(`should not apply mixin more than once, even if it was applied higher up`, () => {
|
|
65
|
+
const NewClass1 = Something1Mixin(SomeClass)
|
|
66
|
+
class NewClass2 extends NewClass1 {}
|
|
67
|
+
const NewClass3 = Something1Mixin(NewClass2)
|
|
68
|
+
expect(NewClass3).toBe(NewClass2)
|
|
69
|
+
})
|
|
70
|
+
})
|