@veritree/ui 0.0.1 → 0.1.1

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.
@@ -0,0 +1,34 @@
1
+ // Generate id
2
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
3
+ let gen = null;
4
+
5
+ function* idMaker() {
6
+ let index = 0;
7
+ while (true) yield index++;
8
+ }
9
+
10
+ export const genId = () => {
11
+ if (!gen) gen = idMaker();
12
+ return gen.next().value;
13
+ };
14
+
15
+ // Add leading zeros
16
+ // export const pad = (num, size) => {
17
+ // let s = num + '';
18
+
19
+ // while (s.length < size) {
20
+ // s = '0' + s;
21
+ // }
22
+
23
+ // return s;
24
+ // };
25
+
26
+ export const addZerosToId = (id) => {
27
+ let formattedId = null;
28
+
29
+ if (id < 10) formattedId = '000' + id;
30
+ else if (id < 100) formattedId = '00' + id;
31
+ else if (id < 1000) formattedId = '0' + id;
32
+
33
+ return '#' + formattedId;
34
+ };
@@ -0,0 +1,139 @@
1
+ /**
2
+ * @param {*} obj
3
+ * @returns boolean
4
+ */
5
+ export const isObj = (obj) => {
6
+ return obj !== null && typeof obj === 'object';
7
+ };
8
+
9
+ /**
10
+ * @param {*} obj
11
+ * @returns {boolean}
12
+ */
13
+ export const isAnEmptyObj = (obj) => {
14
+ if (!isObj(obj)) return false;
15
+
16
+ return Object.keys(obj).length === 0;
17
+ };
18
+
19
+ /**
20
+ * @param {string} key
21
+ * @param {obj} obj
22
+ * @returns {boolean}
23
+ */
24
+ export const isKeyInObj = (key, obj) => {
25
+ if (!isObj(obj) || !key) return false;
26
+
27
+ return Object.keys(obj).includes(key);
28
+ };
29
+
30
+ /**
31
+ * Compare objects deeply
32
+ * @source https://dmitripavlutin.com/how-to-compare-objects-in-javascript/
33
+ *
34
+ * @param {obj} obj1
35
+ * @param {obj} obj2
36
+ * @returns {boolean}
37
+ */
38
+ export const areObjsEqual = (obj1, obj2) => {
39
+ const keys1 = Object.keys(obj1);
40
+ const keys2 = Object.keys(obj2);
41
+
42
+ if (keys1.length !== keys2.length) {
43
+ return false;
44
+ }
45
+
46
+ for (const key of keys1) {
47
+ const val1 = obj1[key];
48
+ const val2 = obj2[key];
49
+ const areObjects = isObj(val1) && isObj(val2);
50
+
51
+ if (
52
+ (areObjects && !areObjsEqual(val1, val2)) ||
53
+ (!areObjects && val1 !== val2)
54
+ ) {
55
+ return false;
56
+ }
57
+ }
58
+
59
+ return true;
60
+ };
61
+
62
+ /**
63
+ * Compare two array of objects and returns an array with the differences
64
+ *
65
+ * @param {array} oldArr
66
+ * @param {array} newArr
67
+ * @returns {array}
68
+ */
69
+ export const diffArrOfObjs = (oldArr, newArr) => {
70
+ const result = [];
71
+
72
+ // compare all elements of oldArr with all elements of newArr
73
+ // and if there is any difference, push it to result
74
+ oldArr.forEach((obj, i) => {
75
+ if (!newArr[i]) return;
76
+
77
+ const obj2 = newArr[i];
78
+
79
+ if (!areObjsEqual(obj, obj2)) {
80
+ result.push(newArr[i]);
81
+ }
82
+ });
83
+
84
+ // compare size and if newArr has more elements,
85
+ // push them to result because they are new
86
+ if (oldArr.length < newArr.length) {
87
+ newArr.slice(oldArr.length).forEach((obj) => {
88
+ result.push(obj);
89
+ });
90
+ }
91
+
92
+ return result;
93
+ };
94
+
95
+ /**
96
+ * Compare two objects and returns an object with the differences
97
+ *
98
+ * @param {obj} oldObj
99
+ * @param {ojb} newObj
100
+ * @returns {obj}
101
+ */
102
+ export const diffObjs = (oldObj, newObj) => {
103
+ const result = {};
104
+ const oldKeys = Object.keys(oldObj);
105
+ const newKeys = Object.keys(newObj);
106
+
107
+ for (const oldKey of oldKeys) {
108
+ const val1 = oldObj[oldKey];
109
+ const val2 = newObj[oldKey];
110
+
111
+ if (!newObj[oldKey]) return;
112
+
113
+ if (typeof val1 === 'object') {
114
+ if (!areObjsEqual(val1, val2)) {
115
+ result[oldKey] = val2;
116
+ }
117
+ } else if (val1 !== val2) {
118
+ result[oldKey] = newObj[oldKey];
119
+ }
120
+ }
121
+
122
+ // compare size and if newKyes has more elements,
123
+ // push them to result because they are new
124
+ if (oldKeys.length < newKeys.length) {
125
+ for (const key of newKeys) {
126
+ if (!oldKeys.includes(key)) result[key] = newObj[key];
127
+ }
128
+ }
129
+
130
+ return result;
131
+ };
132
+
133
+ /**
134
+ * @param {obj} obj
135
+ * @returns {obj}
136
+ */
137
+ export const cloneObj = (obj) => {
138
+ return JSON.parse(JSON.stringify(obj));
139
+ };