-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
526 lines (455 loc) · 11.3 KB
/
helpers.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/**
* @desc
* Create new object based on old object, using Object.assign
* single layer of data copying.
*/
export function newObj(obj) {
return Object.assign({}, obj)
}
/**
* @desc
* Converts an array like structure (like a NodeList) into an array.
* Also handy for shallow array copying
*/
export function newArr(arrLike) {
return Array.prototype.slice.call(arrLike, 0)
}
/**
* @desc
* pure function to create an object
* with reversed key value pairs
*
* @input <object>
* ex
* {
* age: 42
* }
*
* @output <object>
* ex
* {
* 42: age
* }
*/
export function revObj(obj) {
const rev = {}
for(let k in obj) {
const v = obj[k]
rev[v] = k
}
return rev
}
/**
* @desc
* convert an array to enum object
*
* @input <array>
*
*/
export function arr2enum(arr) {
const enumObj = {}
arr.forEach((key, index)=>{enumObj[key] = index})
return enumObj
}
//
// Converts values into simpler version (intended for complex array and object structures)
// with filter options to remove certain keys.
//
//
// Use cases:
// - sanitizing objects so they return something when called JSON.stringify on them
// - cleaning so you only get the data from an object
//
//
// @Args
// - typelist to disallow certain types (based on getType fn)
// ['function', 'Object', 'circular-reference']
// - valList to disallow certain values
// [null, '']
// - valArr used in recursive calls to self to remove circular references
// [ObjectPointer]
//
//
// The script turns circular references into the string: <circular-reference>
// To remove the object keys as well:
// put 'circular-reference' inside of typeList argument
//
//
// @example
// input
// objDeepClone(
// {
// key1: function(){},
// key2: null,
// hello: 'world',
// list: []
// },
// ['function', 'Array'],
// [null]
// )
//
// output
// {hello: "world"}
//
// reason:
// typeList: ['function', 'Array'] Filters all the functions and empty arrays
// valList: [null] Filters all nulls
//
//
// Notes
// - you may have to call the function multiple times,
// if you want to remove certain things (see comment above objTodata ())
//
export function objDeepClone(input, typeList, keyList, valList, valArr) {
typeList = typeList || []
keyList = keyList || []
valList = valList || []
valArr = valArr || []
const type = getType(input)
if(
typeList.indexOf(type) !== -1 ||
valList.indexOf(input) !== -1
) {
return undefined
}
const noCircular = typeList.indexOf('circular-reference') !== -1
// remove circular referencing
if(type === 'arraylike' || type === 'objectlike') {
if(valArr.indexOf(input) !== -1) {
if(noCircular) {
return undefined
} else {
return '<circular-reference>'
}
}
valArr.push(input)
}
if(type == 'null') { return null } else
if(type == 'arraylike') {
const arr = []
for(let i = 0, len = input.length; i < len; i++) {
const val = input[i]
if(
typeList.indexOf(getType(val)) === -1 &&
valList.indexOf(val) === -1
) {
let valid = true
if(noCircular && valArr.indexOf(val) !== -1) {
valid = false
}
if(valid) {
arr.push(objDeepClone(val, typeList, keyList, valList, valArr))
}
}
}
return arr
} else
if(type == 'objectlike') {
const obj = {}
for(let k in input) {
// go to next index if k was found inside of keyList
if(keyList.indexOf(k) != -1) {
continue
}
// this can mess up.
// Some values aren't always accessible.
let v
try {
v = input[k]
}
catch (e) {
v = 'error-inaccessible'
}
if(
typeList.indexOf(getType(v)) === -1 &&
valList.indexOf(v) === -1
) {
let valid = true
if(noCircular && valArr.indexOf(v) !== -1) {
valid = false
}
if(valid) {
obj[k] = objDeepClone(v, typeList, keyList, valList, valArr)
}
}
}
return obj
} else {
return input
}
}
// @todo write more tests
export const objDeepCloneTests = (function(){
const tests = [
{
input: {key2: 24, 'string based key': 50},
exout: {key2: 24, 'string based key': 50},
opts: {noReference: true}
}
]
// self referencing test
{
const val1 = {k: 2}
val1.s = val1
const test = {
input: val1,
exout: {k: 2, s: "<circular-reference>"},
}
tests.push(test)
}
return tests
})()
// A simple approximation of getting the type of a value in JS
//
// can return:
//
// From typeof:
// - "undefined"
// - "boolean"
// - "number"
// - "string"
// - "symbol"
// - "function"
//
// From custom:
// - "null"
// - "arraylike"
// - "objectlike"
//
// from Object.prototype.toString.call
// Here is a small list of examples:
// - new date() = "Date"
// - [] = "Array" (empty array)
// - {} = "Object" (empty object)
// - /[0-9A-Z]/ = "RegExp"
//
export function getType(input) {
var simpleType = typeof input
if(simpleType !== 'object') { return simpleType }
if(input == null) { return 'null' }
// Test for length (by ducktyping) and checking hasOwnProperty for last index
if(
input.length !== undefined &&
input.length > 0 &&
input.hasOwnProperty(input.length - 1)
) { return 'arraylike' }
// test with for in loop
for(var k in input) { return 'objectlike' }
return Object.prototype.toString.call(input).slice(8,-1)
}
export const getTypeTests = [
{
input: 23,
exout: 'number'
},
{
input: 'Complex',
exout: 'string',
},
{
input: [1,2,3],
exout: 'arraylike',
},
{
input: true,
exout: 'boolean',
},
{
input: !1,
exout: 'boolean',
},
{
input: [],
exout: 'Array',
},
{
input: {key: 24, length: 4},
exout: 'objectlike',
},
{
input: {key: 'hello', key2: 'World'},
exout: 'objectlike',
},
{
input: {},
exout: 'Object',
},
{
input: null,
exout: 'null',
},
{
input: new Date(),
exout: 'Date',
},
{
input: /[0-9A-Z]/,
exout: 'RegExp',
},
{
input: function noop(){},
exout: 'function',
},
{
input: Symbol(30),
exout: 'symbol',
},
]
//
// I wrote this for a part in the possible solution for a bug, but it didn't fix it
//
// removes key from an object structure, removes it from arrays and objects and all the sub structures
//
export function recursiveRemoveKey(input, key) {
const type = getType(input)
console.warn('rRk', input, key, type)
if(type == 'arraylike' || type == 'objectlike' || type == 'Array' || type == 'Object') {
if(input[key] != undefined) {
delete input[key]
}
}
if(type == 'arraylike') {
for(let i = 0; i < input.length; i++) {
const item = input[i]
console.log(item)
recursiveRemoveKey(item, key)
}
} else
if(type == 'objectlike') {
for(let k in input) {
const item = input[k]
// console.log('t', item, k)
recursiveRemoveKey(item, key)
}
}
}
//
// Create a unique id, based on time and random number
//
export function createUid() {
return '' + Date.now() + Math.random()
}
/**
* @desc
* checks whether a number is between min and max,
* including min and max, so if called like
* (num, 100, 200): it would return true if num was
* 100 or 200, including all values in between
*/
export function isNumberBetween(num, min, max) {
return num >= min && num <= max
}
/**
* @desc
* Tests whether or not a is equal to b.
*
* This was created for testing library, to check whether or not output is the same as expected output.
* so comparing things like NaN === NaN should return true.
* and objects containing the same keys with the same values.
*
* differences to a === b
* - equals(NaN,NaN) > true
* - equals([{b:[1,2,3],k:24}],[{k:24,b:[1,2,3]}]) > true
*/
export function equals(a, b, {noReference = false} = {}) {
// NaN check
if(a !== a) {
if(b !== b) {
return true
}
}
// do opts based checks
if(noReference && a === b) {
return false
}
// Array compare
if(getType(a) === 'arraylike' && getType(b) === 'arraylike') {
if(a.length !== b.length) {
return false
}
for(let i = 0; i < a.length; i++) {
if(!equals(a[i], b[i])) {
return false
}
}
return true
}
// Object compare
if(getType(a) === 'objectlike' && getType(b) === 'objectlike') {
for(let key in a) {
if(!equals(a[key], b[key])) {
return false
}
}
return true
}
return a === b
}
export const equalsTests = (function(){
const tests = [
{
input: [0, 0],
exout: true
},
{
input: [NaN, NaN],
exout: true
},
{
input: [NaN, 0],
exout: false
},
{
input: [null, undefined],
exout: false
},
{
input: [0, ""],
exout: false
},
{
input: [0, "0"],
exout: false
},
{
input: [1, true],
exout: false
},
{
input: [[1,2,3,4,5], [1,2,3,4,5]],
exout: true
},
{
input: [{key: 24}, {key: 24}],
exout: true
},
{
input: [{b: [1,2,3], key: 24}, {key: 24, b: [1,2,3]}],
exout: true
},
{
input: [[1,{hello: 'World'},2], [1,{'hello': 'World'},2]],
exout: true
},
]
{
const item1 = {key: 20, key2: 40}
const test = {
input: [item1, item1, {noReference: true}],
exout: false
}
tests.push(test)
}
return tests
})()
/**
* encode obj to get parameters queries
*/
export function encodeToQuery(obj) {
let queryStr = '?'
for(let k in obj) {
const v = obj[k]
queryStr += encodeURIComponent(k) + '=' + encodeURIComponent(v) + '&'
}
return queryStr.slice(0, -1)
}