-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
1332 lines (1251 loc) · 36.2 KB
/
utils.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { v1 as uuidV1 } from 'uuid'
/*
* List of optional node-modules and the functions used by them:
* Module Name : Function Name
* ------------------------------------------------------
* @polkadot/util-crypto: isAddress, generateHash
* escapeStringRegexp : escapeStringRegexp, searchRanked
* form-data : objToFormData
* web3-utils : isAddress, isETHAddress
*/
export const EMAIL_REGEX = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,9}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i)
export const HEX_REGEX = /^0x[0-9a-f]+$/i
export const HASH_REGEX = /^0x[0-9a-f]{64}$/i
// doesn't work well on URLs with ports!!! Matches emails too!
export const URL_REGEX = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/g
// default icons used in Message component
// ToDo: move to `reactjs/components/Message`
export const icons = {
basic: '',
error: 'exclamation circle',
loading: { name: 'circle notched', loading: true },
info: 'info',
success: 'check circle outline',
warning: 'lightning'
}
/**
* @name clearClutter
* @summary clears clutter from strings
*
* @param {String} x
*
* @returns {String}
*/
export const clearClutter = x => x.split('\n')
.map(y => y.trim())
.filter(Boolean)
.join(' ')
/**
* @name copyToClipboard
* @summary copies text to browser clipboard. Not compatible with NodeJS.
*
* @param {String} str
*/
export const copyToClipboard = str => {
try {
window.navigator.clipboard.writeText(url)
} catch (e) {
const el = document.createElement('textarea')
el.value = str
el.setAttribute('readonly', '')
el.style.position = 'absolute'
el.style.left = '-9999px'
document.body.appendChild(el)
el.select()
document.execCommand('copy')
document.body.removeChild(el)
}
}
export const downloadFile = (content, fileName, contentType) => {
const a = document.createElement('a')
const file = new Blob([content], { type: contentType })
a.href = URL.createObjectURL(file)
a.download = fileName
a.click()
}
export const escapeStringRegexp = (str) => {
const fn = require('escape-string-regexp')
return fn(str)
}
/**
* @name fallbackIfFails
* @summary a simple try-catch wrapper for invoking functions to catch errors.
* Ensures a value is always returned by avoiding any unexpected errors.
*
* @param {*|Function|Promise} func
* @param {Array|Function} args arguments to be supplied to `func` fuction
* @param {*|Function} fallbackValue alternative value
*
* @returns {*|Promise} if func is a promise the return a promise
*/
export const fallbackIfFails = (func, args = [], fallbackValue = null) => {
let result
try {
if (!isFn(func)) {
result = func
throw 0
}
result = func(
...isFn(args)
? args()
: args
)
if (!isPromise(result)) return result
} catch (_) { }
const getAltVal = () => isFn(fallbackValue)
? fallbackValue(result)
: fallbackValue
return isPromise(result)
? result.catch(getAltVal)
: result !== undefined && !isError(result)
? result
: getAltVal()
}
/**
* @name generateHash
* @summary generate hash using supplied data
*
* @param {String} seed data to generate hash of
* @param {String} algo Supported algorithms: blake2 (default), keccak
* @param {Number} bitLength Default: 256
*/
export const generateHash = (seed = uuidV1(), algo = 'blake2', bitLength = 256) => {
const { blake2AsHex, keccakAsHex } = require('@polkadot/util-crypto')
seed = isUint8Arr(seed)
? seed
: isStr(seed)
? seed
: JSON.stringify(seed)
switch (`${algo}`.toLowerCase()) {
case 'keccak':
return keccakAsHex(seed)
case 'blake2':
default:
return blake2AsHex(seed, bitLength)
// ToDo: add support for other algo from Polkadot/utils-crypto
}
return // unsuporrted
}
/**
* @name isAddress
* @summary validates if supplied is a valid address
*
* @param {String} address
* @param {String} type (optional) valid types: polkadot (default), ethereum
* @param {Number} chainId (optional) chainId for Ethereum address, ss58Format for Polkadot.
* Default: `undefined` (for Polkadot), `0` for Ethereum
* @param {Boolean} ignoreChecksum (optional) for Polkadot only.
* Default: false
*/
export const isAddress = (address, type, chainId, ignoreChecksum = false) => {
try {
switch (`${type}`.toLowerCase()) {
case 'ethereum': return isETHAddress(address, chainId ?? 0)
case 'polkadot':
default:
const { ss58Decode } = require('./convert')
// assume Polkadot/Totem address
const account = ss58Decode(address, ignoreChecksum, chainId)
// must be 32 bytes length
return !!account && account.length === 32
}
} catch (e) {
return false
}
}
isAddress.validTypes = {
ethereum: 'ethereum',
polkadot: 'polkadot',
}
export const isArr = x => Array.isArray(x)
// isArr2D checks if argument is a 2-dimentional array
export const isArr2D = x => isArr(x) && x.every(isArr)
// checks if convertible to an array by using `Array.from(x)`
export const isArrLike = x => isSet(x) || isMap(x) || isArr(x)
// check if function is Async. Does not work when Babel/Webpack is used due to code compilation.
export const isAsyncFn = x => x instanceof (async () => { }).constructor
&& x[Symbol.toStringTag] === 'AsyncFunction'
export const isBool = x => typeof x === 'boolean'
export const isBond = x => {
try {
return isObj(x) && isFn(x.tie) && isFn(x.untie)
} catch (e) {
return false
}
}
// Check if x is a valid Date instance
// Date object can sometimes be 'Invalid Date' without any timestamp.
// Date.getTime() is used to make sure it's a valid Date
export const isDate = x => x instanceof Date && isValidNumber(x.getTime())
export const isDefined = x => x !== undefined && x !== null
export const isError = x => x instanceof Error
export const isETHAddress = (address, chainId) => {
const { isAddress } = require('web3-utils')
return isAddress(address, chainId)
}
export const isFn = x => typeof x === 'function'
export const isHash = x => fallbackIfFails(() => HASH_REGEX.test(x), [], false)
export const isHex = x => fallbackIfFails(() => HEX_REGEX.test(x), [], false)
export const isInteger = x => Number.isInteger(x)
export const isMap = x => x instanceof Map
export const isNodeJS = () => fallbackIfFails(() => !(window && localStorage), [], true)
export const isObj = (x, strict = true) => !!x // excludes null, NaN, Infinity....
&& typeof x === 'object'
&& (
!strict
// excludes Array, Map, Set
|| !isArr(x)
&& !isMap(x)
&& !isSet(x)
)
// Checks if argument is an Array of Objects. Each element type must be object, otherwise will return false.
export const isObjArr = x => isArr(x) && x.every(isObj)
// Checks if argument is a Map of Objects. Each element type must be object, otherwise will return false.
export const isObjMap = x => isMap(x) && Array.from(x).every(([_, v]) => isObj(v))
export const isPositiveInteger = x => isInteger(x) && x > 0
export const isPositiveNumber = x => isValidNumber(x) && x > 0
export const isPromise = x => x instanceof Promise
export const isSet = x => x instanceof Set
export const isStr = x => typeof x === 'string'
export const isSubjectLike = x => isObj(x) && isFn(x.subscribe) && isFn(x.next)
export const isTouchable = () => fallbackIfFails(() => 'ontouchstart' in document.documentElement, [], false)
export const isUint8Arr = arr => arr instanceof Uint8Array
export const isURL = x => x instanceof URL
// checks if dateOrStr is a valid date
export const isValidDate = dateOrStr => {
const date = new Date(dateOrStr)
if (!isDate(date)) return false
// hack to detect & prevent `new Date(dateOrStr)` converting '2021-02-31' to '2021-03-03'
const [original, converted] = [`${dateOrStr}`, date.toISOString()]
.map(y => y
.replace('T', '')
.replace('Z', '')
.substr(0, 10)
)
return original === converted
}
export const isValidNumber = x => typeof x == 'number' && !isNaN(x) && isFinite(x)
export const isValidURL = (x, strict = true) => {
try {
const isAStr = isStr(x)
const url = isURL(x)
? x
: new URL(x)
// If strict mode is set to `true` and if a string value provided, it must match resulting value of new URL(x).
// This can be used to ensure that a URL can be queried without altering.
if (!isAStr || !strict) return true
// catch any auto-correction by `new URL()`.
// Eg: spaces in the domain name being replaced by`%20` or missing `/` in protocol being auto added
x = `${x}`
if (x.endsWith(url.hostname)) x += '/'
return url.href == x
} catch (e) {
return false
}
}
export const hasValue = x => {
try {
if (!isDefined(x)) return false
switch (typeof x) {
case 'string': return isStr(x) && !!x.trim()
case 'number': return isValidNumber(x)
// for both array and object
case 'object':
if (isArr(x)) return x.length > 0
if (isMap(x) || isSet(x)) return x.size > 0
return Object.keys(x).length > 0
case 'boolean':
default: return true // already defined
}
} catch (_) {
return false
}
}
/**
* @name getKeys
* @summary returns an Array of keys or indexes depending on input type
*
* @param {Array|Map|Object} source
*
* @returns {Array}
*/
export const getKeys = source => {
if (isArr(source)) return source.map((_, i) => i)
if (isMap(source)) return Array.from(source).map(x => x[0])
if (isObj(source)) return Object.keys(source)
return []
}
/**
* @name arrMapSlice
* @summary mimics the behaviour of Array.map() with the convenience of only executing callback on range of indexes
*
* @param {Array} data
* @param {Number} startIndex
* @param {Number} endIndex
* @param {Function} callback to be executed on each item within the set range
* Params:
* @currentValue
* @currentIndex
* @array
*
* @returns {Array} list of all items returned by @callback
*/
export const arrMapSlice = (data, startIndex, endIndex, callback) => {
const isAMap = isMap(data)
if (!isArr(data) && !isAMap) return []
const len = isAMap ? data.size : data.length
data = isAMap ? Array.from(data) : data
startIndex = startIndex || 0
endIndex = !endIndex || endIndex >= len ? len - 1 : endIndex
let result = []
for (var i = startIndex;i <= endIndex;i++) {
let key = i, value = data[i]
if (isAMap) {
key = data[i][0]
value = data[i][1]
}
result.push(callback(value, key, data, isAMap))
}
return result
}
/**
* @name arrReadOnly
* @summary sugar for `objReadOnly()` for an Array
*
* @param {Array} input
* @param {Boolean} strict
*
* @returns {Array}
*/
export const arrReadOnly = (input, strict, silent) => objReadOnly(input, strict, silent)
/**
* @name arrReverse
* @summary Reverse an array conditionally
*
* @param {Array} arr
* @param {Boolean} reverse (optional) condition to reverse the array.
* Default: true
* @param {Boolean} newArray (optional) whether to cnstruct new array or use input.
* Default: true
*
* @returns {Array}
*/
export const arrReverse = (arr, reverse = true, newArray = true) => {
if (!isArr(arr)) return []
if (newArray) arr = [...arr]
return reverse
? arr.reverse()
: arr
}
/**
* @name arrSearch
* @summary search array of objects
*
* @param {Array} arr
* @param {Object} keyValues specific keys and respective values to search for
* @param {Boolean} matchExact (optional) whether to match the value exactly as specified in @keyValues
* @param {Boolean} matchAll (optional) whether all or any supplied keys should match
* @param {Boolean} ignoreCase (optional)
* @param {Boolean} asArray (optional) wheter to return result as Array or Map
*
* @returns {Array|Map} Map (key = original index) or Array (index not preserved) if @returnArr == true
*/
export const arrSearch = (arr, keyValues, matchExact, matchAll, ignoreCase, asArray) => {
const result = asArray
? new Array()
: new Map()
if (!isObj(keyValues) || !isObjArr(arr)) return result
const keys = Object.keys(keyValues)
for (var index = 0;index < arr.length;index++) {
let matched = false
const item = arr[index]
for (const i in keys) {
const key = keys[i]
let keyword = keyValues[key]
let value = item[key]
if (ignoreCase && isStr(value)) {
value = value.toLowerCase()
keyword = isStr(keyword)
? keyword.toLowerCase()
: keyword
}
matched = !matchExact && (isStr(value) || isArr(value))
? value.indexOf(keyword) >= 0
: value === keyword
if ((matchAll && !matched) || (!matchAll && matched)) break
}
matched && (asArray
? result.push(item)
: result.set(index, item)
)
}
return result
}
/**
* @name arrSort
* @summary sort an array
*
* @param {Array} arr array to sort
* @param {String} key (optional) name of the property, if object array
* @param {Boolean} reverse (optional) reverse sort.
* Default: false
* @param {Boolean} caseInsensitive (optional) sort without the side-effects of case-sensitivenes.
* Default: true
* @param {Boolean} sortOriginal (optional) true => original array, false => keep original array unchanged.
* Default: false
*
* @returns {Array} sorted array
*/
export const arrSort = (
arr,
key,
reverse = false,
caseInsensitive = true,
sortOriginal = false
) => {
let sortedArr = sortOriginal
? arr
: [...arr]
const getValue = (obj, key) => {
let value
if (isObj(obj) && isDefined(key)) {
value = obj[key]
} else {
value = obj
}
return isStr(value) && caseInsensitive
? value.toLowerCase()
: value
}
sortedArr = sortedArr.sort((a, b) =>
getValue(a, key) > getValue(b, key)
? 1
: -1
)
return reverse
? arrReverse(sortedArr, true)
: sortedArr
}
/**
* @name arrToMap
* @summary generate a Map from one or more arrays
*
* @param {Array|Array[]} arr
* @param {*} key (optional) Array item property name to be uses as Map key.
*
* @returns {Map}
*/
export const arrToMap = (arr, key, algo = 'blake2', bitLength = 64) => new Map(
(arr || [])
.flat()
.filter(Boolean)
.map(item => [
key
&& item?.[key]
|| generateHash(
item,
algo,
bitLength
),
item
])
)
/**
* @name arrUnique
* @summary constructs a new array of unique values
*
* @param {...any} args
*
* @returns {Array}
*/
export const arrUnique = (...args) => Array.from(new Set([...args].flat()))
/**
* @name className
* @summary formats supplied value into CSS class name compatible string for React
*
* @param {Object|Array} value
*
* @returns {String}
*
* @example ```JavaScript
* const isSection = false
* const isIcon = true
* const withBorder = false
* const str = className([
* 'ui',
* { section: isSection, icon: isIcon },
* withBorder && 'bordered',
* ])
*
* // expected result: 'ui icon'
* ```
*/
export const className = value => {
if (isStr(value)) return value
if (isObj(value)) {
// convert into an array
value = Object.keys(value)
.map(key => !!value[key] && key)
}
if (!isArr(value)) return ''
return value
.filter(Boolean)
.map(x => !isObj(x) ? x : className(x))
.join(' ')
}
/**
* @name deferred
* @summary returns a function that invokes the callback function after certain delay/timeout
*
* @param {Function} callback function to be invoked after timeout
* @param {Number} delay (optional) timeout duration in milliseconds.
* Default: 50
* @param {*} thisArg (optional) the special `thisArgs` to be used when invoking the callback.
*
* @returns {Function}
*/
export const deferred = (
callback,
delay = 50,
thisArg,
tid
) => (...args) => {
clearTimeout(tid)
tid = setTimeout(
callback?.bind?.(thisArg),
delay,
...args //arguments for callback
)
}
/**
* @name deferredAsync
* @summary deferred function that returns a promise which resolves/rejejcts based on `callback` result.
* If a callback is ignored the promise will never resolve.
*
* @param {Function} callback function to be invoked after timeout
* @param {Number} delay (optional) timeout duration in milliseconds.
* Default: 50
* @param {*} thisArg (optional) the special `thisArgs` to be used when invoking the callback.
* @param {*} tid (optional) timeout id to be cleared on callback invocation.
*
* @returns {Function}
*/
export const deferredAsync = (
callback,
delay = 50,
thisArg,
tid,
) => (...args) => {
clearTimeout(tid)
return new Promise((resolve, reject) => {
tid = setTimeout(
() => (async () => await callback?.call?.(thisArg, ...args))()
.then(resolve, reject),
delay,
)
})
}
/**
* @name getFuncParams
* @summary extracts the parameter names of a given function.
*
* @param {Function} func
*
* @returns {Array}
*/
export const getFuncParams = func => func
.toString()
.replace('function', '')
.trim()
.split('(')[1]
.split(')')[0]
.split(', ')
export const getUrlParam = (name, url) => {
url ??= fallbackIfFails(() => window.location.href) || ''
try {
const search = '?' + (url.split('?')?.[1] || '')
const params = new URLSearchParams(search)
return name
? params.get(name) || ''
: [...params.keys()].reduce((obj, key) => ({
...obj,
[key]: params.get(key),
}), {})
} catch (_) {
return getUrlParamRegex(name, url)
}
}
/**
* @name getUrlParam
* @summary read parameters of a given URL
*
* @param {String} name (optional) if supplied will return a specific paramenter as string.
* Otherwise, will return an object containing all the URL parameters with respective values.
* @param {String} url
*
* @returns {String|Object}
*/
export const getUrlParamRegex = (name, url) => {
url ??= fallbackIfFails(() => window.location.href, [], '')
const params = {}
const regex = /[?&]+([^=&]+)=([^&]*)/gi
url.replace(
regex,
(_, key, value) => params[key] = decodeURIComponent(value)
)
return name
? params[name] || ''
: params
}
/**
* @name objCopy
* @summary deep-copy an object to another object
*
* @param {Object} source source object
* @param {Object} dest destination object
* @param {Array} ignore (optional) prevents @dest's property to be overriden
* if @source's property value is in the list
* Default: [undefined]
* @returns {Object}
*/
export const objCopy = (source = {}, dest = {}, ignore = [undefined]) => {
const sKeys = Object.keys(source)
for (let i = 0;i < sKeys.length;i++) {
const key = sKeys[i]
if (dest.hasOwnProperty(key) && ignore.includes(source[key])) continue
const value = source[key]
if (isArrLike(value)) {
let newValue = JSON.parse(JSON.stringify(Array.from(value)))
if (isMap(value)) {
newValue = new Map(newValue)
} else if (isSet(value)) {
newValue = new Set([...newValue])
}
dest[key] = newValue
} else if (isObj(value)) {
dest[key] = objCopy(source[key], dest[key], ignore)
} else {
dest[key] = value
}
}
return dest
}
/**
* @name objClean
* @summary constructs a new object with only the supplied property names (keys) and their respective values
*
* @param {Object} obj
* @param {Array} keys property names
* @param {Boolean} recursive (optional) Default: `false`
* @param {Boolean} ignoreIfNotExist (optional) if truthy, only include property if `obj.hasOwnProperty(key)`
* Default: `true`
*
* @returns {Object}
*/
export const objClean = (obj, keys, recursive = false, ignoreIfNotExist = true) => {
if (!isObj(obj) || !isArr(keys)) return {}
const result = {}
for (let i = 0;i < keys.length;i++) {
const key = keys[i]
if (ignoreIfNotExist && !obj.hasOwnProperty(key)) continue
let value = obj[key]
result[key] = value
// recursively clean up child property with object value
if (!recursive || !isObj(value)) continue
const childPrefix = `${key}.`
let childKeys = keys.filter(k => k.startsWith(childPrefix))
if (childKeys.length === 0) continue
// get rid of child key prefix
childKeys = childKeys.map(k =>
k.replace(new RegExp(childPrefix), '')
)
result[key] = objClean(
value,
childKeys,
recursive,
)
}
return result
}
/**
* @name objCreate
* @summary constructs a new object with supplied key(s) and value(s)
*
* @param {Array} keys
* @param {Array} values (optional)
* @param {Object} result (optional)
*
*
* @returns {Object}
*/
export const objCreate = (keys = [], values = [], result = {}) => {
if (!isArr(keys) || !isArr(values) || !isObj(result)) return {}
for (let i = 0;i < keys.length;i++) {
const key = keys[i]
const value = values[i]
result[key] = value
}
return result
}
/**
* @name objEvalRxProps
* @summary evaluate/extract values from properties with RxJS subject.
*
* @param {Object} obj
* @param {Array} recursive property names of child objects to check and evaluate/extract RxJS subject value.
*
* @returns {Object} a new object with RxJS subject values extracted for specified properties.
*/
export const objEvalRxProps = (obj = {}, recursive = []) => {
const output = { ...obj }
Object
.keys(output)
.forEach(key => {
output[key] = isSubjectLike(output[key])
? output[key].value
: recursive === true || recursive?.includes?.(key)
? objEvalRxProps(output[key], false)
: output[key]
})
return output
}
/**
* @name objHasKeys
* @summary checks if all the supplied keys exists in a object
*
* @param {Object} obj
* @param {Array} keys
* @param {Boolean} requireValue (optional) whether each property should have some value.
*
* @returns {Boolean}
*/
export function objHasKeys(obj = {}, keys = [], requireValue = false) {
if (!isObj(obj) || !isArr(keys)) return false
for (let i = 0;i < keys.length;i++) {
const key = keys[i]
if (!obj.hasOwnProperty(key)) return false
if (!requireValue) continue
if (!hasValue(obj[key])) return false
}
return true
}
/**
* @name objReadOnly
* @summary constructs a new read-only object where only new properties can be added.
*
* @param {Object} obj
* @param {Boolean} strict (optional) If true, any attempt to add or update property will fail.
* Otherwise, only new properties can be added but updates will fail.
* Default: false
* @param {Boolean} silent (optional) whether to throw error when property add/update fails.
* Default: false
*
* @returns {Object}
*/
export const objReadOnly = (obj, strict = false, silent = false) => new Proxy(obj || {}, {
setProperty: (self, key, value) => {
// prevents adding new or updating existing property
const isStrict = !isFn(strict)
? strict === true
: strict(self, key, value)
if (isStrict) {
if (silent) return true
throw new TypeError(`Assignment to constant ${Array.isArray(obj) ? 'array' : 'object'} key: ${key}`)
} else if (!self.hasOwnProperty(key)) {
self[key] = value
}
return true
},
get: (self, key) => self[key],
set: function (self, key, value) { return this.setProperty(self, key, value) },
defineProperty: function (self, key) { return this.setProperty(self, key, value) },
// Prevent removal of properties
deleteProperty: () => false
})
/**
* @name objSetProp
* @summary assign value to specified property
*
* @param {Object} obj
* @param {String} key
* @param {*} value
* @param {Boolean} condition (optional)
* @param {*} valueAlt (optional) value to use if condition is truthy
* @returns
*/
export const objSetProp = (obj, key, val, condition, valAlt) => {
obj[key] = !condition ? val : valAlt
return obj
}
/**
* @name objSetProp
* @summary assign value to specified property only if it is undefined
*
* @param {Object} obj
* @param {String} key
* @param {*} value
* @param {Boolean} condition (optional)
* @param {*} valueAlt (optional) value to use if condition is truthy
*
* @returns {Object}
*/
export const objSetPropUndefined = (obj, key, v1, condition, v2) => {
obj[key] === undefined && objSetProp(obj, key, v1, condition, v2)
return obj
}
/**
* @name objSorted
* @summary create a new object with properties sorted by name
*
* @param {Object} obj
* @param {Array} keys
*
* @returns {Object} sorted object
*/
export const objSort = (obj, keys, ...args) => objClean(
obj,
keys
|| Object
.keys(obj)
.sort(),
...args
)
/**
* @name objToUrlParams
* @summary constructs URL param string from an object, excluding any `undefined` values
*
* @param {Object} obj
*
* @returns {String}
*/
export const objToUrlParams = (obj = {}, excludeUndefined = true) => {
const params = new URLSearchParams()
Object
.keys(obj)
.forEach(key => {
const value = obj[key]
if (value === undefined && excludeUndefined) return
params.set(key, value)
})
return params.toString()
}
// Object.keys(obj)
// .map(key => {
// const value = obj[key]
// if (excludeUndefined && value === undefined) return
// const valueEscaped = !isArr(value)
// ? escape(value)
// // prevents escaping comma when joining array
// : value.map(escape).join()
// return `${key}=${valueEscaped}`
// })
// .filter(Boolean)
// .join('&')
export const objToFormData = (obj = {}, excludeUndefined = true) => {
let formData = new FormData()
Object.keys(obj).forEach(key => {
let value = obj[key]
if (excludeUndefined && value === undefined) return
if (isArr(value)) value = value.join()
formData.append(key, value)
})
return formData
}
/**
* @name objWithoutKeys
* @summary constructs a new object excluding specific properties
*
* @param {Object} input
* @param {Array} keys property names to exclude
* @param {Object} output (optional) to delete unwanted props from the original `input` use it here.
* Default: a copy of the `input` object
*
* @returns {Object}
*/
export const objWithoutKeys = (input, keys, output = { ...input }) => {
if (!isObj(input)) return {}
if (!isArr(keys) || !keys.length) return input
for (let i = 0;i < keys.length;i++) {
delete output[keys[i]]
}
return output
}
/**
* @name mapFilter
* @summary Array.filter but for Map.
*
* @param {Map} map
* @param {Function} callback
*
* @returns {Map}
*/
export const mapFilter = (map, callback) => {
const result = new Map()
if (!isMap(map)) return result
if (!isFn(callback)) return map
for (let [key, value] of map.entries()) {
if (!callback(value, key, map)) return
result.set(key, value)
}
return result
}
/**
* @name mapFindByKey
* @summary finds a specific object by supplied object property/key and value within.
*
* Unused??
*
* @param {Map} map Map of objects
* @param {*} key object key to match or null if value is not an object
* @param {*} value
* @param {Boolean} matchExact
*
* @returns {*} first item partial/fully matching @value with supplied @key
*/
export const mapFindByKey = (map, key, value, matchExact) => {
for (let [_, item] of map.entries()) {
const val = key === null
? item
: item[key]
const found = !matchExact && (isStr(val) || isArr(val))
? val.indexOf(value) >= 0
: val === value
if (found) return item
}
}
/**
* @name mapJoin
* @summary creates a new Map by combining two or more Maps
*
* @param {Map[]|Array[]} maps...
*
* @returns {Map}
*
* @example `javascript
* const maps = [
* new Map([['a', 1]]),
* new Map([['b', 2]]),
* ]
* const joined = mapJoin(...maps) // Map(2) {'a' => 1, 'b' => 2}
*
* // use 2D array
* const maps = [
* [['a', 1]],
* [['b', 2]],
* ]
* const joined = mapJoin(...maps) // Map(2) {'a' => 1, 'b' => 2}
* `
*/
export const mapJoin = (...maps) => new Map(
maps
.map(map => {
const arr = fallbackIfFails(Array.from, [map], [])