@zohodesk/utils 1.3.14 → 1.3.16
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/README.md +11 -1
- package/es/constantProps/constant.js +2 -0
- package/es/crud/ArrayManager.js +63 -0
- package/es/crud/ObjectManager.js +54 -0
- package/es/crud/arrayCRUD.js +76 -0
- package/es/crud/objectCRUD.js +49 -0
- package/es/deepEqual.js +26 -0
- package/es/eventListenerUtils.js +28 -0
- package/es/getCurrentTime.js +18 -0
- package/es/index.js +4 -1
- package/es/renderNode.js +28 -0
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
#
|
|
1
|
+
# DOT - Util Collections
|
|
2
2
|
|
|
3
3
|
In this package, We provide lots of util fuctions for web devlopment
|
|
4
4
|
|
|
5
|
+
# 1.3.16
|
|
6
|
+
|
|
7
|
+
- `getCurrentTime`, `objectCRUD`, `arrayCRUD`, `deepEqual`, `ArrayManager`, `ObjectManager`, `eventListenerUtils` util added.
|
|
8
|
+
|
|
9
|
+
# 1.3.15
|
|
10
|
+
|
|
11
|
+
- **renderNodeUtils**
|
|
12
|
+
- `renderNode` function added to render content that can be either a function or a React element.
|
|
13
|
+
- `isRenderable` function added to check if the content is renderable (either a function or a valid React element).
|
|
14
|
+
|
|
5
15
|
# 1.3.14
|
|
6
16
|
|
|
7
17
|
- **StringUtils**
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { appendToArray, deleteFromArrayOfObjectById, deleteFromArrayByItem, updateArrayOfObjectItemById, updateArrayItemByItem, emptyArray } from "./arrayCRUD";
|
|
2
|
+
import { DUMMY_ARRAY } from "../constantProps/constant";
|
|
3
|
+
|
|
4
|
+
class ArrayManager {
|
|
5
|
+
constructor() {
|
|
6
|
+
let initialArray = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DUMMY_ARRAY;
|
|
7
|
+
this.array = initialArray;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
addValue(item) {
|
|
11
|
+
this.array = appendToArray({
|
|
12
|
+
array: this.array,
|
|
13
|
+
item
|
|
14
|
+
});
|
|
15
|
+
return this.array;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
removeValueById(id) {
|
|
19
|
+
this.array = deleteFromArrayOfObjectById({
|
|
20
|
+
arrayOfObject: this.array,
|
|
21
|
+
id
|
|
22
|
+
});
|
|
23
|
+
return this.array;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
removeValueByItem(item) {
|
|
27
|
+
this.array = deleteFromArrayByItem({
|
|
28
|
+
array: this.array,
|
|
29
|
+
item
|
|
30
|
+
});
|
|
31
|
+
return this.array;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
clearValue() {
|
|
35
|
+
this.array = emptyArray();
|
|
36
|
+
return this.array;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
updateValueById(id, newItem) {
|
|
40
|
+
this.array = updateArrayOfObjectItemById({
|
|
41
|
+
arrayOfObject: this.array,
|
|
42
|
+
id,
|
|
43
|
+
newItem
|
|
44
|
+
});
|
|
45
|
+
return this.array;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
updateValueByItem(currentItem, newItem) {
|
|
49
|
+
this.array = updateArrayItemByItem({
|
|
50
|
+
array: this.array,
|
|
51
|
+
currentItem,
|
|
52
|
+
newItem
|
|
53
|
+
});
|
|
54
|
+
return this.array;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
getValue() {
|
|
58
|
+
return this.array;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export default ArrayManager;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { emptyObject, mergeObjects, updateObjectValueByKey, removeObjectByKey, removeObjectByKeys } from "./objectCRUD";
|
|
2
|
+
import { DUMMY_OBJECT } from "../constantProps/constant";
|
|
3
|
+
|
|
4
|
+
class ObjectManager {
|
|
5
|
+
constructor() {
|
|
6
|
+
let initialObject = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DUMMY_OBJECT;
|
|
7
|
+
this.object = initialObject;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
setValue(newObject) {
|
|
11
|
+
this.object = mergeObjects({
|
|
12
|
+
object: this.object,
|
|
13
|
+
newObject
|
|
14
|
+
});
|
|
15
|
+
return this.object;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
clearValue() {
|
|
19
|
+
this.object = emptyObject();
|
|
20
|
+
return this.object;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
updateValueByKey(key, value) {
|
|
24
|
+
this.object = updateObjectValueByKey({
|
|
25
|
+
object: this.object,
|
|
26
|
+
key,
|
|
27
|
+
value
|
|
28
|
+
});
|
|
29
|
+
return this.object;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
removeValueByKeys(keys) {
|
|
33
|
+
this.object = removeObjectByKeys({
|
|
34
|
+
object: this.object,
|
|
35
|
+
keys
|
|
36
|
+
});
|
|
37
|
+
return this.object;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
removeValueByKey(key) {
|
|
41
|
+
this.object = removeObjectByKey({
|
|
42
|
+
object: this.object,
|
|
43
|
+
key
|
|
44
|
+
});
|
|
45
|
+
return this.object;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
getValue() {
|
|
49
|
+
return this.object;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export default ObjectManager;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { DUMMY_ARRAY } from "../constantProps/constant";
|
|
2
|
+
import { deepEqual } from "../deepEqual";
|
|
3
|
+
export const appendToArray = _ref => {
|
|
4
|
+
let {
|
|
5
|
+
array,
|
|
6
|
+
item
|
|
7
|
+
} = _ref;
|
|
8
|
+
return [...array, item];
|
|
9
|
+
};
|
|
10
|
+
export const updateArrayOfObjectItemById = _ref2 => {
|
|
11
|
+
let {
|
|
12
|
+
arrayOfObject,
|
|
13
|
+
id,
|
|
14
|
+
newItem
|
|
15
|
+
} = _ref2;
|
|
16
|
+
let found = false;
|
|
17
|
+
return arrayOfObject.map(object => {
|
|
18
|
+
if (!found && id !== undefined && object.id === id) {
|
|
19
|
+
found = true;
|
|
20
|
+
return Array.isArray(newItem) ? [...newItem] : typeof newItem === 'object' ? { ...object,
|
|
21
|
+
...newItem
|
|
22
|
+
} : newItem;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return object;
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
export const updateArrayItemByItem = _ref3 => {
|
|
29
|
+
let {
|
|
30
|
+
array,
|
|
31
|
+
currentItem,
|
|
32
|
+
newItem
|
|
33
|
+
} = _ref3;
|
|
34
|
+
let found = false;
|
|
35
|
+
return array.map(arrayItem => {
|
|
36
|
+
if (!found && currentItem !== undefined && deepEqual(arrayItem, currentItem)) {
|
|
37
|
+
found = true;
|
|
38
|
+
return newItem;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return arrayItem;
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
export const deleteFromArrayOfObjectById = _ref4 => {
|
|
45
|
+
let {
|
|
46
|
+
arrayOfObject,
|
|
47
|
+
id
|
|
48
|
+
} = _ref4;
|
|
49
|
+
let found = false;
|
|
50
|
+
return arrayOfObject.filter(object => {
|
|
51
|
+
if (!found && id !== undefined && object.id === id) {
|
|
52
|
+
found = true;
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return true;
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
export const deleteFromArrayByItem = _ref5 => {
|
|
60
|
+
let {
|
|
61
|
+
array,
|
|
62
|
+
currentItem
|
|
63
|
+
} = _ref5;
|
|
64
|
+
let found = false;
|
|
65
|
+
return array.filter(arrayItem => {
|
|
66
|
+
if (!found && currentItem !== undefined && deepEqual(arrayItem, currentItem)) {
|
|
67
|
+
found = true;
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return true;
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
export const emptyArray = () => {
|
|
75
|
+
return DUMMY_ARRAY;
|
|
76
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { DUMMY_OBJECT } from "../constantProps/constant";
|
|
2
|
+
export const mergeObjects = _ref => {
|
|
3
|
+
let {
|
|
4
|
+
object,
|
|
5
|
+
newObject
|
|
6
|
+
} = _ref;
|
|
7
|
+
return { ...object,
|
|
8
|
+
...newObject
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export const emptyObject = () => {
|
|
12
|
+
return DUMMY_OBJECT;
|
|
13
|
+
};
|
|
14
|
+
export const updateObjectValueByKey = _ref2 => {
|
|
15
|
+
let {
|
|
16
|
+
object,
|
|
17
|
+
key,
|
|
18
|
+
value
|
|
19
|
+
} = _ref2;
|
|
20
|
+
return { ...object,
|
|
21
|
+
[key]: value
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
export const removeObjectByKeys = _ref3 => {
|
|
25
|
+
let {
|
|
26
|
+
object,
|
|
27
|
+
keys
|
|
28
|
+
} = _ref3;
|
|
29
|
+
const newObject = { ...object
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
if (Array.isArray(keys)) {
|
|
33
|
+
keys.forEach(key => {
|
|
34
|
+
delete newObject[key];
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return newObject;
|
|
39
|
+
};
|
|
40
|
+
export const removeObjectByKey = _ref4 => {
|
|
41
|
+
let {
|
|
42
|
+
object,
|
|
43
|
+
key
|
|
44
|
+
} = _ref4;
|
|
45
|
+
const newObject = { ...object
|
|
46
|
+
};
|
|
47
|
+
delete newObject[key];
|
|
48
|
+
return newObject;
|
|
49
|
+
};
|
package/es/deepEqual.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const deepEqual = (a, b) => {
|
|
2
|
+
if (a === b) {
|
|
3
|
+
return true;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (typeof a === 'object' && typeof b === 'object' && a !== null && b !== null) {
|
|
7
|
+
if (Array.isArray(a) !== Array.isArray(b)) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const keysA = Object.keys(a);
|
|
12
|
+
const keysB = Object.keys(b);
|
|
13
|
+
|
|
14
|
+
if (keysA.length !== keysB.length) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return keysA.every(key => deepEqual(a[key], b[key]));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (typeof a === 'function' && typeof b === 'function') {
|
|
22
|
+
return a.toString().replace(/\s+/g, '') === b.toString().replace(/\s+/g, '');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return false;
|
|
26
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export const addCustomEventListener = _ref => {
|
|
2
|
+
let {
|
|
3
|
+
eventName,
|
|
4
|
+
callback,
|
|
5
|
+
targetElement,
|
|
6
|
+
options
|
|
7
|
+
} = _ref;
|
|
8
|
+
|
|
9
|
+
if (!targetElement || typeof targetElement.addEventListener !== 'function') {
|
|
10
|
+
throw new Error('addCustomEventListener: targetElement is not a valid EventTarget. ' + 'Received: ' + String(targetElement));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
targetElement.addEventListener(eventName, callback, options);
|
|
14
|
+
};
|
|
15
|
+
export const removeCustomEventListener = _ref2 => {
|
|
16
|
+
let {
|
|
17
|
+
eventName,
|
|
18
|
+
callback,
|
|
19
|
+
targetElement,
|
|
20
|
+
options
|
|
21
|
+
} = _ref2;
|
|
22
|
+
|
|
23
|
+
if (!targetElement || typeof targetElement.removeEventListener !== 'function') {
|
|
24
|
+
throw new Error('removeCustomEventListener: targetElement is not a valid EventTarget. ' + 'Received: ' + String(targetElement));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
targetElement.removeEventListener(eventName, callback, options);
|
|
28
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export default function getCurrentTime() {
|
|
2
|
+
const now = new Date();
|
|
3
|
+
const hours = now.getHours();
|
|
4
|
+
const minutes = now.getMinutes().toString().padStart(2, '0');
|
|
5
|
+
const seconds = now.getSeconds().toString().padStart(2, '0');
|
|
6
|
+
const ampm = hours >= 12 ? 'PM' : 'AM';
|
|
7
|
+
const formattedHours = hours % 12 || 12; // Convert 24-hour time to 12-hour format
|
|
8
|
+
|
|
9
|
+
const hours24 = hours.toString().padStart(2, '0');
|
|
10
|
+
return {
|
|
11
|
+
format12h: `${formattedHours}:${minutes}:${seconds} ${ampm}`,
|
|
12
|
+
format24h: `${hours24}:${minutes}:${seconds}`,
|
|
13
|
+
hours: hours,
|
|
14
|
+
minutes: parseInt(minutes),
|
|
15
|
+
seconds: parseInt(seconds),
|
|
16
|
+
meridiem: ampm
|
|
17
|
+
};
|
|
18
|
+
}
|
package/es/index.js
CHANGED
|
@@ -7,4 +7,7 @@ export { default as combineCustomStyle } from "./combineCustomStyle";
|
|
|
7
7
|
export { default as compileClassNames } from "./compileClassNames";
|
|
8
8
|
export { default as ErrorBoundary } from "./ErrorBoundary";
|
|
9
9
|
export { default as loadResource } from "./loadResource";
|
|
10
|
-
export { default as accessKeyCodes, accessKeyNames } from "./accessKeyCodes";
|
|
10
|
+
export { default as accessKeyCodes, accessKeyNames } from "./accessKeyCodes";
|
|
11
|
+
export { default as renderNode, isRenderable } from "./renderNode";
|
|
12
|
+
export { default as getCurrentTime } from "./getCurrentTime";
|
|
13
|
+
export { addCustomEventListener, removeCustomEventListener } from "./eventListenerUtils";
|
package/es/renderNode.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import React, { isValidElement } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Utility function to render content that can be either a function or a React element
|
|
4
|
+
* @param {Function|React.ReactElement|null|undefined} content - The content to render
|
|
5
|
+
* @param {Object} props - Optional props to pass to the function if content is a function
|
|
6
|
+
* @returns {React.ReactElement|null} - The rendered content or null
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export default function renderNode(content, props) {
|
|
10
|
+
let result = content;
|
|
11
|
+
|
|
12
|
+
if (typeof content === 'function') {
|
|
13
|
+
result = content(props);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return /*#__PURE__*/isValidElement(result) ? result : null;
|
|
17
|
+
}
|
|
18
|
+
;
|
|
19
|
+
/**
|
|
20
|
+
* Checks if the content is renderable (function or valid React element)
|
|
21
|
+
* @param {any} content - The content to check
|
|
22
|
+
* @returns {boolean} - True if content is renderable
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export function isRenderable(content) {
|
|
26
|
+
return typeof content === 'function' || /*#__PURE__*/isValidElement(content);
|
|
27
|
+
}
|
|
28
|
+
;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zohodesk/utils",
|
|
3
|
-
"version": "1.3.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.3.16",
|
|
4
|
+
"description": "DOT Utils Collection",
|
|
5
5
|
"main": "es/index.js",
|
|
6
6
|
"module": "es/index.js",
|
|
7
7
|
"private": false,
|
|
@@ -21,15 +21,17 @@
|
|
|
21
21
|
"download": "react-cli clean ./node_modules ./package-lock.json && npm install",
|
|
22
22
|
"build": "type react-cli && react-cli build:component:es",
|
|
23
23
|
"build:watch": "react-cli build:component:es -w",
|
|
24
|
+
"build:es": "react-cli build:library:es",
|
|
24
25
|
"clean": "react-cli clean unittest coverage es result.json",
|
|
25
26
|
"rtl": "react-cli rtl ./src ./es",
|
|
26
27
|
"nodefix": "node ../buildtool_patch.js",
|
|
27
28
|
"prepare": "npm run clean && npm run build && npm run rtl",
|
|
28
29
|
"test": "react-cli test",
|
|
29
|
-
"jsdoc": "jsdoc -c jsdoc.json"
|
|
30
|
+
"jsdoc": "jsdoc -c jsdoc.json",
|
|
31
|
+
"expublish": "npm publish --tag experimental-version"
|
|
30
32
|
},
|
|
31
33
|
"files": [
|
|
32
34
|
"es",
|
|
33
35
|
"!**/__tests__"
|
|
34
36
|
]
|
|
35
|
-
}
|
|
37
|
+
}
|