@cocreate/utils 1.31.0 → 1.33.0
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/CHANGELOG.md +29 -0
- package/package.json +1 -1
- package/src/index.js +202 -183
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
+
# [1.33.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.32.0...v1.33.0) (2024-01-30)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* removed sortDataOld ([9755f81](https://github.com/CoCreate-app/CoCreate-utils/commit/9755f815fedd5156ea4a9c2b11b4896c10441424))
|
|
7
|
+
* supput values null, false, undefined, 0 ([954c126](https://github.com/CoCreate-app/CoCreate-utils/commit/954c1262b37c42d8f0badf9fe1c5855f4e244b8e))
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* queryData $type, $mod, $where operators ([94684a0](https://github.com/CoCreate-app/CoCreate-utils/commit/94684a0959fbf353d77ae363b522d5e17f209af0))
|
|
13
|
+
|
|
14
|
+
# [1.32.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.31.0...v1.32.0) (2024-01-17)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Bug Fixes
|
|
18
|
+
|
|
19
|
+
* getValueFromObject conditions and param names ([2fd7387](https://github.com/CoCreate-app/CoCreate-utils/commit/2fd73873cc42d0a22e43e04fee35f31227790f7b))
|
|
20
|
+
* getValueFromObject param trowError boolean will trow erro if property does not exist vs returning undefined ([39a9cab](https://github.com/CoCreate-app/CoCreate-utils/commit/39a9cabb9e109d1aae6a695ba47d76c95c8581f6))
|
|
21
|
+
* renamed isMatch to queryMatch ([b3c9429](https://github.com/CoCreate-app/CoCreate-utils/commit/b3c942950661b1d906c9f8a77a3bbd2f5693fd47))
|
|
22
|
+
* update to support new query system ([a267b52](https://github.com/CoCreate-app/CoCreate-utils/commit/a267b5260955a631330a2b22bd9fb316a00004c6))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Features
|
|
26
|
+
|
|
27
|
+
* query() function to handle queries similar to mongodb ([164bf45](https://github.com/CoCreate-app/CoCreate-utils/commit/164bf45d44f620fe8e6a4cf75fe744be68277d6c))
|
|
28
|
+
* sort by multiple keys ([7872a53](https://github.com/CoCreate-app/CoCreate-utils/commit/7872a5308bcf510e40385c57b45c0d899a40295a))
|
|
29
|
+
|
|
1
30
|
# [1.31.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.30.0...v1.31.0) (2024-01-08)
|
|
2
31
|
|
|
3
32
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -142,32 +142,36 @@
|
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
-
function getValueFromObject(
|
|
145
|
+
function getValueFromObject(object = {}, path = '', throwError = false) {
|
|
146
146
|
try {
|
|
147
|
-
if (
|
|
148
|
-
|
|
147
|
+
if (!Object.keys(object).length || !path) {
|
|
148
|
+
if (throwError)
|
|
149
|
+
throw new Error("Invalid input to getValueFromObject");
|
|
150
|
+
return
|
|
151
|
+
|
|
152
|
+
}
|
|
149
153
|
|
|
150
154
|
path = path.replace(/\[(\d+)\]/g, '.$1');
|
|
151
155
|
|
|
152
|
-
let
|
|
156
|
+
let data = object, subpath = path.split('.');
|
|
153
157
|
|
|
154
158
|
for (let i = 0; i < subpath.length; i++) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
159
|
+
if (throwError && !(subpath[i] in data))
|
|
160
|
+
throw new Error("Key not found in object: " + subpath[i]);
|
|
161
|
+
|
|
162
|
+
data = data[subpath[i]];
|
|
163
|
+
if (!data)
|
|
164
|
+
break;
|
|
158
165
|
}
|
|
159
|
-
|
|
166
|
+
|
|
167
|
+
return data;
|
|
160
168
|
} catch (error) {
|
|
161
|
-
console.
|
|
162
|
-
|
|
169
|
+
// console.error("Error in getValueFromObject:", error);
|
|
170
|
+
if (throwError)
|
|
171
|
+
throw error;
|
|
163
172
|
}
|
|
164
173
|
}
|
|
165
174
|
|
|
166
|
-
function isObjectEmpty(obj) {
|
|
167
|
-
for (var x in obj) { return false }
|
|
168
|
-
return true;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
175
|
function domParser(str) {
|
|
172
176
|
try {
|
|
173
177
|
var mainTag = str.match(/\<(?<tag>[a-z0-9]+)(.*?)?\>/).groups.tag;
|
|
@@ -298,6 +302,7 @@
|
|
|
298
302
|
}
|
|
299
303
|
|
|
300
304
|
if (Selector) {
|
|
305
|
+
// let selectors = Selector.split(/,(?![^()]*\))/g);
|
|
301
306
|
|
|
302
307
|
let selectors = Selector.split(',');
|
|
303
308
|
for (let j = 0; j < selectors.length; j++) {
|
|
@@ -418,146 +423,176 @@
|
|
|
418
423
|
return selector
|
|
419
424
|
}
|
|
420
425
|
|
|
421
|
-
|
|
422
426
|
function queryData(data, query) {
|
|
423
|
-
if (
|
|
424
|
-
|
|
427
|
+
if (query.$and) {
|
|
428
|
+
for (let i = 0; i < query.$and.length; i++) {
|
|
429
|
+
if (!queryData(data, query.$and[i]))
|
|
430
|
+
return false
|
|
431
|
+
}
|
|
432
|
+
}
|
|
425
433
|
|
|
426
|
-
if (
|
|
427
|
-
|
|
434
|
+
if (query.$nor) {
|
|
435
|
+
for (let i = 0; i < query.$nor.length; i++) {
|
|
436
|
+
if (queryData(data, query.$nor[i]))
|
|
437
|
+
return false;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
428
440
|
|
|
429
|
-
|
|
430
|
-
|
|
441
|
+
for (let key of Object.keys(query)) {
|
|
442
|
+
if (key === '$and' || key === '$or')
|
|
443
|
+
continue
|
|
444
|
+
if (!queryMatch(data, { [key]: query[key] }))
|
|
445
|
+
return false
|
|
446
|
+
}
|
|
431
447
|
|
|
432
|
-
if (
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
448
|
+
if (query.$or) {
|
|
449
|
+
for (let i = 0; i < query.$or.length; i++) {
|
|
450
|
+
if (queryData(data, query.$or[i]))
|
|
451
|
+
return true
|
|
452
|
+
}
|
|
453
|
+
}
|
|
436
454
|
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
for (let i = 0; i < query.length; i++) {
|
|
440
|
-
let dataValue
|
|
441
|
-
if (query[i].key.includes('.') || /\[([0-9]*)\]/g.test(query[i].key))
|
|
442
|
-
dataValue = getValueFromObject(data[n], query[i].key)
|
|
443
|
-
else
|
|
444
|
-
dataValue = data[n][query[i].key]
|
|
445
|
-
if (dataValue == undefined)
|
|
446
|
-
dataValue = ''
|
|
447
|
-
let logicalOperator = query[i].logicalOperator || 'and'
|
|
448
|
-
let queryValues = query[i].value
|
|
449
|
-
|
|
450
|
-
let queryIsArray = false
|
|
451
|
-
if (!Array.isArray(queryValues))
|
|
452
|
-
queryValues = [queryValues]
|
|
453
|
-
else
|
|
454
|
-
queryIsArray = true
|
|
455
|
+
return true;
|
|
456
|
+
}
|
|
455
457
|
|
|
458
|
+
function queryMatch(data, query) {
|
|
459
|
+
for (let key of Object.keys(query)) {
|
|
460
|
+
// if (!data.hasOwnProperty(key))
|
|
461
|
+
// return false
|
|
456
462
|
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
queryValue = queryValue.toLowerCase()
|
|
464
|
-
}
|
|
463
|
+
let dataValue
|
|
464
|
+
try {
|
|
465
|
+
dataValue = getValueFromObject(data, key, true)
|
|
466
|
+
} catch (error) {
|
|
467
|
+
return false
|
|
468
|
+
}
|
|
465
469
|
|
|
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
|
-
queryStatus =
|
|
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
|
-
|
|
470
|
+
if (typeof query[key] === 'string' || typeof query[key] === 'number' || typeof query[key] === 'boolean') {
|
|
471
|
+
if (Array.isArray(dataValue))
|
|
472
|
+
return dataValue.includes(query[key])
|
|
473
|
+
else
|
|
474
|
+
return dataValue === query[key]
|
|
475
|
+
} else if (Array.isArray(query[key])) {
|
|
476
|
+
if (Array.isArray(dataValue)) {
|
|
477
|
+
return isEqualArray(dataValue, query[key]);
|
|
478
|
+
} else {
|
|
479
|
+
return false;
|
|
480
|
+
}
|
|
481
|
+
} else {
|
|
482
|
+
for (let property of Object.keys(query[key])) {
|
|
483
|
+
if (!property.startsWith('$')) {
|
|
484
|
+
if (typeof dataValue !== 'object') {
|
|
485
|
+
return false;
|
|
486
|
+
} else
|
|
487
|
+
return queryMatch({ [property]: getValueFromObject(dataValue, property) }, { [property]: query[key][property] })
|
|
488
|
+
} else {
|
|
489
|
+
let queryValue = query[key][property]
|
|
490
|
+
let queryStatus = false
|
|
491
|
+
switch (property) {
|
|
492
|
+
case '$eq':
|
|
493
|
+
if (Array.isArray(dataValue) && Array.isArray(queryValue)) {
|
|
494
|
+
queryStatus = isEqualArray(dataValue, queryValue);
|
|
495
|
+
} else {
|
|
496
|
+
queryStatus = (dataValue === queryValue);
|
|
497
|
+
}
|
|
498
|
+
break;
|
|
499
|
+
case '$ne':
|
|
500
|
+
if (Array.isArray(dataValue) && Array.isArray(queryValue)) {
|
|
501
|
+
queryStatus = !isEqualArray(dataValue, queryValue);
|
|
502
|
+
} else {
|
|
503
|
+
queryStatus = (dataValue !== queryValue);
|
|
504
|
+
}
|
|
505
|
+
break;
|
|
506
|
+
case '$not':
|
|
507
|
+
queryStatus = !queryMatch(data, { [key]: query[key]['$not'] });
|
|
508
|
+
break;
|
|
509
|
+
case '$lt':
|
|
510
|
+
queryStatus = (dataValue < queryValue)
|
|
511
|
+
break;
|
|
512
|
+
case '$lte':
|
|
513
|
+
queryStatus = (dataValue <= queryValue)
|
|
514
|
+
break;
|
|
515
|
+
case '$gt':
|
|
516
|
+
queryStatus = (dataValue > queryValue)
|
|
517
|
+
break;
|
|
518
|
+
case '$gte':
|
|
519
|
+
queryStatus = (dataValue >= queryValue)
|
|
520
|
+
break;
|
|
521
|
+
case '$in':
|
|
522
|
+
if (Array.isArray(dataValue)) {
|
|
523
|
+
queryStatus = dataValue.some(element => queryValue.includes(element));
|
|
524
|
+
} else {
|
|
525
|
+
queryStatus = queryValue.includes(dataValue);
|
|
526
|
+
}
|
|
527
|
+
break;
|
|
528
|
+
case '$nin':
|
|
529
|
+
if (Array.isArray(dataValue)) {
|
|
530
|
+
queryStatus = !dataValue.some(element => queryValue.includes(element));
|
|
531
|
+
} else {
|
|
532
|
+
queryStatus = !queryValue.includes(dataValue);
|
|
533
|
+
}
|
|
534
|
+
break;
|
|
535
|
+
case '$all':
|
|
536
|
+
if (Array.isArray(dataValue) && Array.isArray(queryValue)) {
|
|
537
|
+
queryStatus = queryValue.every(element => dataValue.includes(element));
|
|
538
|
+
}
|
|
539
|
+
break;
|
|
540
|
+
case '$elemMatch':
|
|
541
|
+
if (Array.isArray(data[key])) {
|
|
542
|
+
queryStatus = data[key].some(element => queryMatch(element, query[key][property]));
|
|
543
|
+
}
|
|
544
|
+
break;
|
|
545
|
+
case '$size':
|
|
546
|
+
if (Array.isArray(dataValue)) {
|
|
547
|
+
queryStatus = (dataValue.length === queryValue);
|
|
548
|
+
}
|
|
549
|
+
break;
|
|
550
|
+
case '$exists':
|
|
551
|
+
queryStatus = (queryValue ? data.hasOwnProperty(key) : !data.hasOwnProperty(key));
|
|
552
|
+
break;
|
|
553
|
+
case '$regex':
|
|
554
|
+
if (typeof dataValue === 'string') {
|
|
555
|
+
let regex = new RegExp(queryValue);
|
|
556
|
+
queryStatus = regex.test(dataValue);
|
|
557
|
+
}
|
|
558
|
+
break;
|
|
559
|
+
case '$type':
|
|
560
|
+
let dataType = typeof dataValue;
|
|
561
|
+
if (Array.isArray(dataValue)) {
|
|
562
|
+
dataType = 'array';
|
|
563
|
+
}
|
|
564
|
+
queryStatus = (dataType === queryValue);
|
|
565
|
+
break;
|
|
566
|
+
case '$mod':
|
|
567
|
+
if (typeof dataValue === 'number' && Array.isArray(queryValue) && queryValue.length === 2) {
|
|
568
|
+
const [divisor, remainder] = queryValue;
|
|
569
|
+
queryStatus = (dataValue % divisor === remainder);
|
|
570
|
+
}
|
|
571
|
+
break;
|
|
572
|
+
case '$where':
|
|
573
|
+
if (typeof queryValue === 'function') {
|
|
574
|
+
try {
|
|
575
|
+
// queryStatus = queryValue.call(data);
|
|
576
|
+
} catch (error) {
|
|
577
|
+
console.error('Error in queryData $where function:', error);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
break;
|
|
536
581
|
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
queryStatus = true
|
|
540
|
-
break;
|
|
541
|
-
}
|
|
542
|
-
if (!queryIsArray || query[i].operator === "$nin") {
|
|
543
|
-
switch (logicalOperator) {
|
|
544
|
-
case 'and':
|
|
545
|
-
if (queryStatus == false)
|
|
546
|
-
return false
|
|
582
|
+
default:
|
|
583
|
+
console.log('unknown operator')
|
|
547
584
|
break;
|
|
548
|
-
|
|
549
|
-
// if (queryStatus == true)
|
|
550
|
-
// queryResult = queryStatus
|
|
551
|
-
// break;
|
|
585
|
+
|
|
552
586
|
}
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
587
|
+
if (!queryStatus)
|
|
588
|
+
return false
|
|
589
|
+
|
|
590
|
+
}
|
|
556
591
|
}
|
|
592
|
+
return true
|
|
557
593
|
}
|
|
558
|
-
}
|
|
559
594
|
|
|
560
|
-
|
|
595
|
+
}
|
|
561
596
|
}
|
|
562
597
|
|
|
563
598
|
function isEqualArray(arr1, arr2) {
|
|
@@ -643,50 +678,35 @@
|
|
|
643
678
|
return true
|
|
644
679
|
}
|
|
645
680
|
|
|
681
|
+
|
|
646
682
|
function sortData(data, sort) {
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
case 'string':
|
|
671
|
-
if (!a[key])
|
|
672
|
-
a[key] = ""
|
|
673
|
-
return a[key].localeCompare(b[key])
|
|
674
|
-
case 'number':
|
|
675
|
-
if (!a[key])
|
|
676
|
-
a[key] = 0
|
|
677
|
-
return a[key] - b[key]
|
|
678
|
-
case 'array':
|
|
679
|
-
case 'object':
|
|
680
|
-
break;
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
});
|
|
684
|
-
} catch (error) {
|
|
685
|
-
console.log(error)
|
|
683
|
+
return data.sort((a, b) => {
|
|
684
|
+
for (let i = 0; i < sort.length; i++) {
|
|
685
|
+
let key = sort[i].key;
|
|
686
|
+
if (a[key] == null && b[key] == null) continue;
|
|
687
|
+
if (a[key] == null)
|
|
688
|
+
return sort[i].direction === 'desc' ? -1 : 1;
|
|
689
|
+
if (b[key] == null)
|
|
690
|
+
return sort[i].direction === 'desc' ? 1 : -1;
|
|
691
|
+
|
|
692
|
+
if (typeof a[key] !== typeof b[key]) {
|
|
693
|
+
return typeof a[key] < typeof b[key] ? -1 : 1;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
if (a[key] !== b[key]) {
|
|
697
|
+
if (typeof a[key] === 'string') {
|
|
698
|
+
return sort[i].direction === 'desc' ?
|
|
699
|
+
b[key].localeCompare(a[key]) :
|
|
700
|
+
a[key].localeCompare(b[key]);
|
|
701
|
+
} else { // Assuming numeric or other comparable types
|
|
702
|
+
return sort[i].direction === 'desc' ?
|
|
703
|
+
(b[key] - a[key]) :
|
|
704
|
+
(a[key] - b[key]);
|
|
705
|
+
}
|
|
686
706
|
}
|
|
687
707
|
}
|
|
688
|
-
|
|
689
|
-
|
|
708
|
+
return 0;
|
|
709
|
+
});
|
|
690
710
|
}
|
|
691
711
|
|
|
692
712
|
function getAttributes(el) {
|
|
@@ -748,7 +768,6 @@
|
|
|
748
768
|
isValidDate,
|
|
749
769
|
dotNotationToObject,
|
|
750
770
|
getValueFromObject,
|
|
751
|
-
isObjectEmpty,
|
|
752
771
|
domParser,
|
|
753
772
|
parseTextToHtml,
|
|
754
773
|
escapeHtml,
|