@questwork/q-utilities 0.1.3 → 0.1.4

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.
@@ -415,107 +415,85 @@ class KeyValueObject {
415
415
  }
416
416
 
417
417
  static addItem(arr, key, value) {
418
- arr.push(
419
- { key, value }
420
- )
418
+ arr.push(this.init({ key, value }))
421
419
  }
422
420
  static addRecord(arr = [], key, value) {
423
- const self = this
424
421
  if (!this.hasKeyValue(arr, key, value)) {
425
- arr.push(self.init({ key, value }))
422
+ arr.push(this.init({ key, value }))
426
423
  }
427
424
  return arr
428
425
  }
429
-
430
426
  static appendRecord(arr = [], key, value) {
431
427
  return arr.map((item) => {
432
- if (item.key === key) {
428
+ if (this.sameKey(item, key)) {
433
429
  item.value = [...item.value, ...value]
434
430
  }
435
431
  return item
436
432
  })
437
433
  }
438
-
439
- static fromObject(options = {}) {
440
- const self = this
441
- return Object.keys(options).reduce((acc, key) => {
442
- acc.push(self.init({ key, value: options[key] }))
443
- return acc
444
- }, [])
445
- }
446
-
447
- static removeByKey(arr, key) {
448
- return arr.reduce((acc, item) => {
449
- if (item.key !== key) {
450
- acc.push(item)
434
+ static appendValueArray(arr = [], key, value) {
435
+ return arr.map((item) => {
436
+ if (this.sameKey(item, key)) {
437
+ item.value = [...item.value, ...value]
451
438
  }
452
- return acc
453
- }, [])
439
+ return item
440
+ })
454
441
  }
455
-
456
442
  static foundByKey(arr = [], key) {
457
443
  const found = arr.find((m) => {
458
- return m.key === key
444
+ return this.sameKey(m, key)
459
445
  })
460
446
  return found || null
461
447
  }
462
-
463
448
  static foundValueByKey(arr = [], key) {
464
449
  const found = this.foundByKey(arr, key)
465
450
  return found ? found.value : null
466
451
  }
467
-
452
+ static fromObject(options = {}) {
453
+ return Object.keys(options).reduce((acc, key) => {
454
+ acc.push(this.init({ key, value: options[key] }))
455
+ return acc
456
+ }, [])
457
+ }
468
458
  static getValueByKey(arr = [], key) {
469
- const found = arr.find((i) => {
470
- return i.key === key
471
- })
472
- if (found) {
473
- return found.value
474
- }
475
- return null
459
+ return this.foundValueByKey(arr, key)
476
460
  }
477
-
478
461
  static getValueByKeyFromArray(arr = [], key) {
479
462
  if (arr.length === 0) {
480
463
  return null
481
464
  }
482
465
  const firstArr = arr.shift()
483
466
  const found = firstArr.find((i) => {
484
- return i.key === key
467
+ return this.sameKey(i, key)
485
468
  })
486
469
  if (found && found.value) {
487
470
  return found.value
488
471
  }
489
472
  return this.getValueByKeyFromArray(arr, key)
490
473
  }
491
-
492
474
  static getValuesByKey(arr = [], key) {
493
475
  return arr.reduce((acc, item) => {
494
- if (item.key === key) {
476
+ if (this.sameKey(item, key)) {
495
477
  acc.push(item.value)
496
478
  }
497
479
  return acc
498
480
  }, [])
499
481
  }
500
-
501
482
  static hasKeyValue(arr = [], key, value) {
502
483
  if (typeof value === 'undefined') {
503
- return arr.filter((item) => item.key === key).length > 0
484
+ return arr.filter((item) => this.sameKey(item, key)).length > 0
504
485
  }
505
- return arr.filter((item) => (item.key === key && item.value === value)).length > 0
486
+ return arr.filter((item) => (this.sameKey(item, key) && item.value === value)).length > 0
506
487
  }
507
-
508
488
  static insertOrUpdateRecord(arr = [], key, value) {
509
- const self = this
510
489
  let copy = [...arr]
511
- if (!self.hasKeyValue(arr, key)) {
512
- copy.push(self.init({ key, value }))
490
+ if (!this.hasKeyValue(arr, key)) {
491
+ copy.push(this.init({ key, value }))
513
492
  } else {
514
- copy = self.updateRecord(arr, key, value)
493
+ copy = this.updateRecord(arr, key, value)
515
494
  }
516
495
  return copy
517
496
  }
518
-
519
497
  static keys(arr = []) {
520
498
  if (Array.isArray(arr)) {
521
499
  return arr.reduce((acc, item) => {
@@ -525,7 +503,6 @@ class KeyValueObject {
525
503
  }
526
504
  return []
527
505
  }
528
-
529
506
  static merge(toArr, fromArr) {
530
507
  (fromArr || []).map((from) => {
531
508
  const found = toArr.find((to) => {
@@ -539,7 +516,17 @@ class KeyValueObject {
539
516
  })
540
517
  return toArr
541
518
  }
542
-
519
+ static removeByKey(arr, key) {
520
+ return arr.reduce((acc, item) => {
521
+ if (!this.sameKey(item, key)) {
522
+ acc.push(item)
523
+ }
524
+ return acc
525
+ }, [])
526
+ }
527
+ static sameKey(item, key) {
528
+ return item.key === key
529
+ }
543
530
  static toObject(arr = []) {
544
531
  if (Array.isArray(arr)) {
545
532
  return arr.reduce((acc, item) => {
@@ -549,7 +536,6 @@ class KeyValueObject {
549
536
  }
550
537
  return {}
551
538
  }
552
-
553
539
  static toString(arr = [], delimiter = '; ') {
554
540
  if (Array.isArray(arr)) {
555
541
  return arr.reduce((acc, item) => {
@@ -559,10 +545,9 @@ class KeyValueObject {
559
545
  }
560
546
  return ''
561
547
  }
562
-
563
548
  static updateRecord(arr = [], key, value) {
564
549
  return arr.map((item) => {
565
- if (item.key === key) {
550
+ if (this.sameKey(item, key)) {
566
551
  return {
567
552
  ...item,
568
553
  value
@@ -571,11 +556,9 @@ class KeyValueObject {
571
556
  return item
572
557
  })
573
558
  }
574
-
575
559
  static updateOrInsertRecord(arr = [], key, value) {
576
560
  return this.insertOrUpdateRecord(arr, key, value)
577
561
  }
578
-
579
562
  static updateRecordsFromArray(arr = [], updateArr = []) {
580
563
  if (Array.isArray(arr) && Array.isArray(updateArr)) {
581
564
  const obj1 = this.toObject(arr)
@@ -587,7 +570,6 @@ class KeyValueObject {
587
570
  }
588
571
  return []
589
572
  }
590
-
591
573
  static values(arr = []) {
592
574
  if (Array.isArray(arr)) {
593
575
  return arr.reduce((acc, item) => {
@@ -634,16 +616,25 @@ class Metadata extends KeyValueObject {
634
616
  })
635
617
  return instance.isValid ? instance : null
636
618
  }
619
+ static get _classname() {
620
+ return 'Metadata'
621
+ }
637
622
 
638
- static foundByKey(arr = [], key) {
639
- const found = (arr || []).find((m) => {
640
- return m.key === stringFormatter(key)
623
+ static merge(toArr, fromArr) {
624
+ (fromArr || []).map((from) => {
625
+ const found = toArr.find((to) => {
626
+ return stringFormatter(to.key) === stringFormatter(from.key)
627
+ })
628
+ if (found) {
629
+ found.value = (found.value || []).concat(from.value)
630
+ } else {
631
+ toArr.push(from)
632
+ }
641
633
  })
642
- return found || null
634
+ return toArr
643
635
  }
644
-
645
- static get _classname() {
646
- return 'Metadata'
636
+ static sameKey(item, key) {
637
+ return stringFormatter(item.key) === stringFormatter(key)
647
638
  }
648
639
  }
649
640
 
package/dist/index.min.js CHANGED
@@ -390,107 +390,85 @@ class KeyValueObject {
390
390
  }
391
391
 
392
392
  static addItem(arr, key, value) {
393
- arr.push(
394
- { key, value }
395
- )
393
+ arr.push(this.init({ key, value }))
396
394
  }
397
395
  static addRecord(arr = [], key, value) {
398
- const self = this
399
396
  if (!this.hasKeyValue(arr, key, value)) {
400
- arr.push(self.init({ key, value }))
397
+ arr.push(this.init({ key, value }))
401
398
  }
402
399
  return arr
403
400
  }
404
-
405
401
  static appendRecord(arr = [], key, value) {
406
402
  return arr.map((item) => {
407
- if (item.key === key) {
403
+ if (this.sameKey(item, key)) {
408
404
  item.value = [...item.value, ...value]
409
405
  }
410
406
  return item
411
407
  })
412
408
  }
413
-
414
- static fromObject(options = {}) {
415
- const self = this
416
- return Object.keys(options).reduce((acc, key) => {
417
- acc.push(self.init({ key, value: options[key] }))
418
- return acc
419
- }, [])
420
- }
421
-
422
- static removeByKey(arr, key) {
423
- return arr.reduce((acc, item) => {
424
- if (item.key !== key) {
425
- acc.push(item)
409
+ static appendValueArray(arr = [], key, value) {
410
+ return arr.map((item) => {
411
+ if (this.sameKey(item, key)) {
412
+ item.value = [...item.value, ...value]
426
413
  }
427
- return acc
428
- }, [])
414
+ return item
415
+ })
429
416
  }
430
-
431
417
  static foundByKey(arr = [], key) {
432
418
  const found = arr.find((m) => {
433
- return m.key === key
419
+ return this.sameKey(m, key)
434
420
  })
435
421
  return found || null
436
422
  }
437
-
438
423
  static foundValueByKey(arr = [], key) {
439
424
  const found = this.foundByKey(arr, key)
440
425
  return found ? found.value : null
441
426
  }
442
-
427
+ static fromObject(options = {}) {
428
+ return Object.keys(options).reduce((acc, key) => {
429
+ acc.push(this.init({ key, value: options[key] }))
430
+ return acc
431
+ }, [])
432
+ }
443
433
  static getValueByKey(arr = [], key) {
444
- const found = arr.find((i) => {
445
- return i.key === key
446
- })
447
- if (found) {
448
- return found.value
449
- }
450
- return null
434
+ return this.foundValueByKey(arr, key)
451
435
  }
452
-
453
436
  static getValueByKeyFromArray(arr = [], key) {
454
437
  if (arr.length === 0) {
455
438
  return null
456
439
  }
457
440
  const firstArr = arr.shift()
458
441
  const found = firstArr.find((i) => {
459
- return i.key === key
442
+ return this.sameKey(i, key)
460
443
  })
461
444
  if (found && found.value) {
462
445
  return found.value
463
446
  }
464
447
  return this.getValueByKeyFromArray(arr, key)
465
448
  }
466
-
467
449
  static getValuesByKey(arr = [], key) {
468
450
  return arr.reduce((acc, item) => {
469
- if (item.key === key) {
451
+ if (this.sameKey(item, key)) {
470
452
  acc.push(item.value)
471
453
  }
472
454
  return acc
473
455
  }, [])
474
456
  }
475
-
476
457
  static hasKeyValue(arr = [], key, value) {
477
458
  if (typeof value === 'undefined') {
478
- return arr.filter((item) => item.key === key).length > 0
459
+ return arr.filter((item) => this.sameKey(item, key)).length > 0
479
460
  }
480
- return arr.filter((item) => (item.key === key && item.value === value)).length > 0
461
+ return arr.filter((item) => (this.sameKey(item, key) && item.value === value)).length > 0
481
462
  }
482
-
483
463
  static insertOrUpdateRecord(arr = [], key, value) {
484
- const self = this
485
464
  let copy = [...arr]
486
- if (!self.hasKeyValue(arr, key)) {
487
- copy.push(self.init({ key, value }))
465
+ if (!this.hasKeyValue(arr, key)) {
466
+ copy.push(this.init({ key, value }))
488
467
  } else {
489
- copy = self.updateRecord(arr, key, value)
468
+ copy = this.updateRecord(arr, key, value)
490
469
  }
491
470
  return copy
492
471
  }
493
-
494
472
  static keys(arr = []) {
495
473
  if (Array.isArray(arr)) {
496
474
  return arr.reduce((acc, item) => {
@@ -500,7 +478,6 @@ class KeyValueObject {
500
478
  }
501
479
  return []
502
480
  }
503
-
504
481
  static merge(toArr, fromArr) {
505
482
  (fromArr || []).map((from) => {
506
483
  const found = toArr.find((to) => {
@@ -514,7 +491,17 @@ class KeyValueObject {
514
491
  })
515
492
  return toArr
516
493
  }
517
-
494
+ static removeByKey(arr, key) {
495
+ return arr.reduce((acc, item) => {
496
+ if (!this.sameKey(item, key)) {
497
+ acc.push(item)
498
+ }
499
+ return acc
500
+ }, [])
501
+ }
502
+ static sameKey(item, key) {
503
+ return item.key === key
504
+ }
518
505
  static toObject(arr = []) {
519
506
  if (Array.isArray(arr)) {
520
507
  return arr.reduce((acc, item) => {
@@ -524,7 +511,6 @@ class KeyValueObject {
524
511
  }
525
512
  return {}
526
513
  }
527
-
528
514
  static toString(arr = [], delimiter = '; ') {
529
515
  if (Array.isArray(arr)) {
530
516
  return arr.reduce((acc, item) => {
@@ -534,10 +520,9 @@ class KeyValueObject {
534
520
  }
535
521
  return ''
536
522
  }
537
-
538
523
  static updateRecord(arr = [], key, value) {
539
524
  return arr.map((item) => {
540
- if (item.key === key) {
525
+ if (this.sameKey(item, key)) {
541
526
  return {
542
527
  ...item,
543
528
  value
@@ -546,11 +531,9 @@ class KeyValueObject {
546
531
  return item
547
532
  })
548
533
  }
549
-
550
534
  static updateOrInsertRecord(arr = [], key, value) {
551
535
  return this.insertOrUpdateRecord(arr, key, value)
552
536
  }
553
-
554
537
  static updateRecordsFromArray(arr = [], updateArr = []) {
555
538
  if (Array.isArray(arr) && Array.isArray(updateArr)) {
556
539
  const obj1 = this.toObject(arr)
@@ -562,7 +545,6 @@ class KeyValueObject {
562
545
  }
563
546
  return []
564
547
  }
565
-
566
548
  static values(arr = []) {
567
549
  if (Array.isArray(arr)) {
568
550
  return arr.reduce((acc, item) => {
@@ -609,16 +591,25 @@ class Metadata extends KeyValueObject {
609
591
  })
610
592
  return instance.isValid ? instance : null
611
593
  }
594
+ static get _classname() {
595
+ return 'Metadata'
596
+ }
612
597
 
613
- static foundByKey(arr = [], key) {
614
- const found = (arr || []).find((m) => {
615
- return m.key === stringFormatter(key)
598
+ static merge(toArr, fromArr) {
599
+ (fromArr || []).map((from) => {
600
+ const found = toArr.find((to) => {
601
+ return stringFormatter(to.key) === stringFormatter(from.key)
602
+ })
603
+ if (found) {
604
+ found.value = (found.value || []).concat(from.value)
605
+ } else {
606
+ toArr.push(from)
607
+ }
616
608
  })
617
- return found || null
609
+ return toArr
618
610
  }
619
-
620
- static get _classname() {
621
- return 'Metadata'
611
+ static sameKey(item, key) {
612
+ return stringFormatter(item.key) === stringFormatter(key)
622
613
  }
623
614
  }
624
615
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@questwork/q-utilities",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Questwork QUtilities",
5
5
  "main": "dist/index.min.js",
6
6
  "type": "module",