@questwork/q-utilities 0.1.5 → 0.1.7

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.
@@ -587,7 +587,7 @@ class KeyValueObject {
587
587
  if (typeof value === 'undefined') {
588
588
  return arr.filter((item) => this.sameKey(item, key)).length > 0
589
589
  }
590
- return arr.filter((item) => (this.sameKey(item, key) && item.value === value)).length > 0
590
+ return arr.filter((item) => (this.sameKey(item, key) && _isSame(item.value, value))).length > 0
591
591
  }
592
592
  static insertOrUpdateRecord(arr = [], key, value) {
593
593
  let copy = [...arr]
@@ -629,7 +629,7 @@ class KeyValueObject {
629
629
  }, [])
630
630
  }
631
631
  static sameKey(item, key) {
632
- return item.key === key
632
+ return _isSame(item.key, key)
633
633
  }
634
634
  static toObject(arr = []) {
635
635
  if (Array.isArray(arr)) {
@@ -698,6 +698,10 @@ class KeyValueObject {
698
698
  }
699
699
  }
700
700
 
701
+ function _isSame(key1, key2) {
702
+ return key1 === key2
703
+ }
704
+
701
705
 
702
706
 
703
707
  ;// ./lib/models/keyValueObject/index.js
@@ -706,8 +710,15 @@ class KeyValueObject {
706
710
 
707
711
 
708
712
  ;// ./lib/helpers/stringFormatter/stringFormatter.js
709
- function stringFormatter(str) {
710
- return (str || '').toUpperCase().replace('-', '_').replace(' ', '_')
713
+ function stringFormatter(str, delimiter = '_') {
714
+ if (str === null || typeof str === 'undefined' || typeof str.toString === 'undefined') {
715
+ return null
716
+ }
717
+ return str.toString()
718
+ .trim()
719
+ .toUpperCase()
720
+ .replace('-', delimiter)
721
+ .replace(' ', delimiter)
711
722
  }
712
723
 
713
724
 
@@ -716,6 +727,8 @@ function stringFormatter(str) {
716
727
 
717
728
 
718
729
 
730
+ const DELIMITER = '_'
731
+
719
732
  class Metadata extends KeyValueObject {
720
733
  static init(options = {}) {
721
734
  if (options instanceof this) {
@@ -723,7 +736,7 @@ class Metadata extends KeyValueObject {
723
736
  }
724
737
  const instance = new this({
725
738
  ...options,
726
- key: stringFormatter(options.key),
739
+ key: stringFormatter(options.key, DELIMITER),
727
740
  })
728
741
  return instance.isValid ? instance : null
729
742
  }
@@ -734,7 +747,7 @@ class Metadata extends KeyValueObject {
734
747
  static merge(toArr, fromArr) {
735
748
  (fromArr || []).map((from) => {
736
749
  const found = toArr.find((to) => {
737
- return stringFormatter(to.key) === stringFormatter(from.key)
750
+ return metadata_isSame(to.key, from.key)
738
751
  })
739
752
  if (found) {
740
753
  found.value = (found.value || []).concat(from.value)
@@ -745,10 +758,14 @@ class Metadata extends KeyValueObject {
745
758
  return toArr
746
759
  }
747
760
  static sameKey(item, key) {
748
- return stringFormatter(item.key) === stringFormatter(key)
761
+ return metadata_isSame(item.key, key)
749
762
  }
750
763
  }
751
764
 
765
+ function metadata_isSame(key1, key2) {
766
+ return stringFormatter(key1, DELIMITER) === stringFormatter(key2, DELIMITER)
767
+ }
768
+
752
769
 
753
770
 
754
771
  ;// ./lib/models/metadata/index.js
@@ -1074,7 +1091,13 @@ class Service {
1074
1091
  return this.repo.init(options)
1075
1092
  }
1076
1093
  initFromArray(arr = []) {
1077
- return arr.map((i) => this.init(i))
1094
+ if (Array.isArray(arr)) {
1095
+ return arr.map((a) => this.init(a))
1096
+ }
1097
+ return []
1098
+ }
1099
+ initOnlyValidFromArray(arr = []) {
1100
+ return this.initFromArray(arr).filter((i) => i)
1078
1101
  }
1079
1102
 
1080
1103
  async saveAll({ docs = [], systemLog } = {}) {
package/dist/index.min.js CHANGED
@@ -562,7 +562,7 @@ class KeyValueObject {
562
562
  if (typeof value === 'undefined') {
563
563
  return arr.filter((item) => this.sameKey(item, key)).length > 0
564
564
  }
565
- return arr.filter((item) => (this.sameKey(item, key) && item.value === value)).length > 0
565
+ return arr.filter((item) => (this.sameKey(item, key) && _isSame(item.value, value))).length > 0
566
566
  }
567
567
  static insertOrUpdateRecord(arr = [], key, value) {
568
568
  let copy = [...arr]
@@ -604,7 +604,7 @@ class KeyValueObject {
604
604
  }, [])
605
605
  }
606
606
  static sameKey(item, key) {
607
- return item.key === key
607
+ return _isSame(item.key, key)
608
608
  }
609
609
  static toObject(arr = []) {
610
610
  if (Array.isArray(arr)) {
@@ -673,6 +673,10 @@ class KeyValueObject {
673
673
  }
674
674
  }
675
675
 
676
+ function _isSame(key1, key2) {
677
+ return key1 === key2
678
+ }
679
+
676
680
 
677
681
 
678
682
  ;// ./lib/models/keyValueObject/index.js
@@ -681,8 +685,15 @@ class KeyValueObject {
681
685
 
682
686
 
683
687
  ;// ./lib/helpers/stringFormatter/stringFormatter.js
684
- function stringFormatter(str) {
685
- return (str || '').toUpperCase().replace('-', '_').replace(' ', '_')
688
+ function stringFormatter(str, delimiter = '_') {
689
+ if (str === null || typeof str === 'undefined' || typeof str.toString === 'undefined') {
690
+ return null
691
+ }
692
+ return str.toString()
693
+ .trim()
694
+ .toUpperCase()
695
+ .replace('-', delimiter)
696
+ .replace(' ', delimiter)
686
697
  }
687
698
 
688
699
 
@@ -691,6 +702,8 @@ function stringFormatter(str) {
691
702
 
692
703
 
693
704
 
705
+ const DELIMITER = '_'
706
+
694
707
  class Metadata extends KeyValueObject {
695
708
  static init(options = {}) {
696
709
  if (options instanceof this) {
@@ -698,7 +711,7 @@ class Metadata extends KeyValueObject {
698
711
  }
699
712
  const instance = new this({
700
713
  ...options,
701
- key: stringFormatter(options.key),
714
+ key: stringFormatter(options.key, DELIMITER),
702
715
  })
703
716
  return instance.isValid ? instance : null
704
717
  }
@@ -709,7 +722,7 @@ class Metadata extends KeyValueObject {
709
722
  static merge(toArr, fromArr) {
710
723
  (fromArr || []).map((from) => {
711
724
  const found = toArr.find((to) => {
712
- return stringFormatter(to.key) === stringFormatter(from.key)
725
+ return metadata_isSame(to.key, from.key)
713
726
  })
714
727
  if (found) {
715
728
  found.value = (found.value || []).concat(from.value)
@@ -720,10 +733,14 @@ class Metadata extends KeyValueObject {
720
733
  return toArr
721
734
  }
722
735
  static sameKey(item, key) {
723
- return stringFormatter(item.key) === stringFormatter(key)
736
+ return metadata_isSame(item.key, key)
724
737
  }
725
738
  }
726
739
 
740
+ function metadata_isSame(key1, key2) {
741
+ return stringFormatter(key1, DELIMITER) === stringFormatter(key2, DELIMITER)
742
+ }
743
+
727
744
 
728
745
 
729
746
  ;// ./lib/models/metadata/index.js
@@ -1049,7 +1066,13 @@ class Service {
1049
1066
  return this.repo.init(options)
1050
1067
  }
1051
1068
  initFromArray(arr = []) {
1052
- return arr.map((i) => this.init(i))
1069
+ if (Array.isArray(arr)) {
1070
+ return arr.map((a) => this.init(a))
1071
+ }
1072
+ return []
1073
+ }
1074
+ initOnlyValidFromArray(arr = []) {
1075
+ return this.initFromArray(arr).filter((i) => i)
1053
1076
  }
1054
1077
 
1055
1078
  async saveAll({ docs = [], systemLog } = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@questwork/q-utilities",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Questwork QUtilities",
5
5
  "main": "dist/index.min.js",
6
6
  "type": "module",
@@ -10,11 +10,6 @@
10
10
  "default": "./dist/index.min.js"
11
11
  }
12
12
  },
13
- "scripts": {
14
- "build": "cross-env NODE_ENV=production minimize=false gulp",
15
- "lint": "eslint .",
16
- "test:models": "NODE_ENV=test mocha --exit 'lib/models/test.setup.js' 'lib/models/**/*.spec.js'"
17
- },
18
13
  "author": {
19
14
  "name": "Questwork Consulting Limited",
20
15
  "email": "info@questwork.com",
@@ -45,5 +40,10 @@
45
40
  },
46
41
  "engines": {
47
42
  "node": ">=10.0.0"
43
+ },
44
+ "scripts": {
45
+ "build": "cross-env NODE_ENV=production minimize=false gulp",
46
+ "lint": "eslint .",
47
+ "test:models": "NODE_ENV=test mocha --exit 'lib/models/test.setup.js' 'lib/models/**/*.spec.js'"
48
48
  }
49
- }
49
+ }