@codady/utils 0.0.11 → 0.0.13

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/src/isEmpty.js ADDED
@@ -0,0 +1,45 @@
1
+ /**
2
+ * @since Last modified: 2025/12/25 14:22:26
3
+ * @function isEmpty
4
+ * @description Determine whether it is empty data.The data itself is empty data: 0| ''|false|undefined|null; <br>empty function: function () {}|() => {}; <br>empty array and empty objects: []|{}| [null]| [ undefined]| ['']| [""];<br> empty symbol object: symbol()|symbol.For(), will be judged as empty.
5
+ * @param {*} data - Can be any data
6
+ * @returns {boolean} - Return true or false
7
+ * @example
8
+ * ax.isEmpty([null]);
9
+ * <!--return true-->
10
+ */
11
+ //简介:判断是否为空数据。本身为空的数据:0|''|false|undefined|null;空函数:function(){}|()=>{};空数组和空对象:[]|{}|[null]|[undefined]|['']|[""];空Symbol对象:Symbol()|Symbol.for(),都将判断为空。
12
+ //返回:true或false
13
+ 'use strict';
14
+ import getDataType from './getDataType';
15
+ const isEmpty = (data) => {
16
+ let type = getDataType(data), flag;
17
+ if (!data) {
18
+ //0,'',false,undefined,null
19
+ flag = true;
20
+ }
21
+ else {
22
+ //function(){}|()=>{}
23
+ //[null]|[undefined]|['']|[""]
24
+ //[]|{}
25
+ //Symbol()|Symbol.for()
26
+ //Set,Map
27
+ //Date/Regex
28
+ flag = (type === 'Object') ? (Object.keys(data).length === 0) :
29
+ (type === 'Array') ? data.join('') === '' :
30
+ (type === 'Function') ? (data.toString().replace(/\s+/g, '').match(/{.*}/g)[0] === '{}') :
31
+ (type === 'Symbol') ? (data.toString().replace(/\s+/g, '').match(/\(.*\)/g)[0] === '()') :
32
+ (type === 'Set' || type === 'Map') ? data.size === 0 :
33
+ type === 'Date' ? isNaN(data.getTime()) :
34
+ type === 'RegExp' ? data.source === '' :
35
+ type === 'ArrayBuffer' ? data.byteLength === 0 :
36
+ (type === 'NodeList' || type === 'HTMLCollection') ? data.length === 0 :
37
+ ('length' in data && typeof data.length === 'number') ? data.length === 0 :
38
+ ('size' in data && typeof data.size === 'number') ? data.size === 0 :
39
+ (type === 'Error' || data instanceof Error) ? data.message === '' :
40
+ (type.includes('Array') && (['Uint8Array', 'Int8Array', 'Uint16Array', 'Int16Array', 'Uint32Array', 'Int32Array', 'Float32Array', 'Float64Array'].includes(type))) ? data.length === 0 :
41
+ false;
42
+ }
43
+ return flag;
44
+ };
45
+ export default isEmpty;
package/src/isEmpty.ts ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ * @since Last modified: 2025/12/25 14:22:26
3
+ * @function isEmpty
4
+ * @description Determine whether it is empty data.The data itself is empty data: 0| ''|false|undefined|null; <br>empty function: function () {}|() => {}; <br>empty array and empty objects: []|{}| [null]| [ undefined]| ['']| [""];<br> empty symbol object: symbol()|symbol.For(), will be judged as empty.
5
+ * @param {*} data - Can be any data
6
+ * @returns {boolean} - Return true or false
7
+ * @example
8
+ * ax.isEmpty([null]);
9
+ * <!--return true-->
10
+ */
11
+ //简介:判断是否为空数据。本身为空的数据:0|''|false|undefined|null;空函数:function(){}|()=>{};空数组和空对象:[]|{}|[null]|[undefined]|['']|[""];空Symbol对象:Symbol()|Symbol.for(),都将判断为空。
12
+ //返回:true或false
13
+ 'use strict';
14
+ import getDataType from './getDataType';
15
+ const isEmpty = (data: any): boolean => {
16
+ let type = getDataType(data), flag: boolean;
17
+ if (!data) {
18
+ //0,'',false,undefined,null
19
+ flag = true;
20
+ } else {
21
+ //function(){}|()=>{}
22
+ //[null]|[undefined]|['']|[""]
23
+ //[]|{}
24
+ //Symbol()|Symbol.for()
25
+ //Set,Map
26
+ //Date/Regex
27
+ flag = (type === 'Object') ? (Object.keys(data).length === 0) :
28
+ (type === 'Array') ? data.join('') === '' :
29
+ (type === 'Function') ? (data.toString().replace(/\s+/g, '').match(/{.*}/g)[0] === '{}') :
30
+ (type === 'Symbol') ? (data.toString().replace(/\s+/g, '').match(/\(.*\)/g)[0] === '()') :
31
+ (type === 'Set' || type === 'Map') ? data.size === 0 :
32
+ type === 'Date' ? isNaN(data.getTime()) :
33
+ type === 'RegExp' ? data.source === '' :
34
+ type === 'ArrayBuffer' ? data.byteLength === 0 :
35
+ (type === 'NodeList' || type === 'HTMLCollection') ? data.length === 0 :
36
+ ('length' in data && typeof data.length === 'number') ? data.length === 0 :
37
+ ('size' in data && typeof data.size === 'number') ? data.size === 0 :
38
+ (type === 'Error' || data instanceof Error) ? data.message === '' :
39
+ (type.includes('Array') && (['Uint8Array', 'Int8Array', 'Uint16Array', 'Int16Array', 'Uint32Array', 'Int32Array', 'Float32Array', 'Float64Array'].includes(type))) ? data.length === 0 :
40
+ false;
41
+ }
42
+ return flag;
43
+ }
44
+ export default isEmpty;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Available Map mutation methods
3
+ */
4
+ export const mapMutableMethods = ['set', 'delete', 'clear'];
5
+ export default mapMutableMethods;
@@ -0,0 +1,78 @@
1
+ /**
2
+ * @since Last modified: 2025/12/25 14:29:53
3
+ * shallowCopy
4
+ *
5
+ * This function performs a shallow copy of various data structures:
6
+ * - Set
7
+ * - Map
8
+ * - Array
9
+ * - Plain Object (including properties with Symbol keys)
10
+ * - Date
11
+ * - RegExp
12
+ * - Buffer (Node.js)
13
+ * - ArrayBuffer and Typed Arrays
14
+ * - WeakSet
15
+ * - WeakMap
16
+ * - Error
17
+ *
18
+ * It returns a new instance of the data structure, but the elements or properties of reference types (such as objects or arrays) will be copied by reference.
19
+ * Primitive types (e.g., number, string, boolean) will be returned directly.
20
+ */
21
+ 'use strict';
22
+ import copyObjectWithSymbol from "./copyObjectWithSymbol";
23
+ import getDataType from "./getDataType";
24
+ const shallowCopy = (data, options = {}) => {
25
+ const dataType = getDataType(data);
26
+ // Check if data is a Set
27
+ if (dataType === 'Set') {
28
+ // Shallow copy Set
29
+ return new Set([...data]);
30
+ }
31
+ // Check if data is a Map
32
+ if (dataType === 'Map') {
33
+ // Shallow copy Map
34
+ return new Map([...data]);
35
+ }
36
+ // Check if data is an Array
37
+ if (Array.isArray(data)) {
38
+ // Shallow copy Array
39
+ return [...data];
40
+ }
41
+ // Check if data is a Plain Object (including Symbol keys)
42
+ if (dataType === 'object') {
43
+ // Ensure the object type includes string and symbol keys
44
+ // Shallow copy the object and include the Symbol properties
45
+ return copyObjectWithSymbol(data);
46
+ }
47
+ // Check if data is a Date
48
+ if (dataType === 'Date') {
49
+ return new Date(data.getTime());
50
+ }
51
+ // Check if data is a RegExp
52
+ if (dataType === 'RegExp') {
53
+ return new RegExp(data.source, data.flags);
54
+ }
55
+ // Check if data is a Buffer (for Node.js)
56
+ if (dataType === 'Buffer') {
57
+ return Buffer.from(data);
58
+ }
59
+ // Check if data is an ArrayBuffer or TypedArray
60
+ if (dataType === 'ArrayBuffer' || ArrayBuffer.isView(data)) {
61
+ return data.slice(0);
62
+ }
63
+ // Check if data is a WeakSet
64
+ if (dataType === 'WeakSet') {
65
+ return new WeakSet([...data]);
66
+ }
67
+ // Check if data is a WeakMap
68
+ if (dataType === 'WeakMap') {
69
+ return new WeakMap([...data]);
70
+ }
71
+ // Check if data is an Error
72
+ if (dataType === 'Error') {
73
+ return new Error(data.message);
74
+ }
75
+ // For other types (such as numbers, strings, booleans, etc.), return the original value
76
+ return data;
77
+ };
78
+ export default shallowCopy;
@@ -0,0 +1,93 @@
1
+ /**
2
+ * @since Last modified: 2025/12/25 14:29:53
3
+ * shallowCopy
4
+ *
5
+ * This function performs a shallow copy of various data structures:
6
+ * - Set
7
+ * - Map
8
+ * - Array
9
+ * - Plain Object (including properties with Symbol keys)
10
+ * - Date
11
+ * - RegExp
12
+ * - Buffer (Node.js)
13
+ * - ArrayBuffer and Typed Arrays
14
+ * - WeakSet
15
+ * - WeakMap
16
+ * - Error
17
+ *
18
+ * It returns a new instance of the data structure, but the elements or properties of reference types (such as objects or arrays) will be copied by reference.
19
+ * Primitive types (e.g., number, string, boolean) will be returned directly.
20
+ */
21
+ 'use strict';
22
+
23
+ import copyObjectWithSymbol from "./copyObjectWithSymbol";
24
+ import getDataType from "./getDataType";
25
+
26
+ const shallowCopy = (data: any,options={}): any => {
27
+ const dataType = getDataType(data);
28
+
29
+ // Check if data is a Set
30
+ if (dataType === 'Set') {
31
+ // Shallow copy Set
32
+ return new Set([...data]);
33
+ }
34
+
35
+ // Check if data is a Map
36
+ if (dataType === 'Map') {
37
+ // Shallow copy Map
38
+ return new Map([...data]);
39
+ }
40
+
41
+ // Check if data is an Array
42
+ if (Array.isArray(data)) {
43
+ // Shallow copy Array
44
+ return [...data];
45
+ }
46
+
47
+ // Check if data is a Plain Object (including Symbol keys)
48
+ if (dataType === 'object') {
49
+ // Ensure the object type includes string and symbol keys
50
+ // Shallow copy the object and include the Symbol properties
51
+ return copyObjectWithSymbol(data);
52
+ }
53
+
54
+ // Check if data is a Date
55
+ if (dataType === 'Date') {
56
+ return new Date(data.getTime());
57
+ }
58
+
59
+ // Check if data is a RegExp
60
+ if (dataType === 'RegExp') {
61
+ return new RegExp(data.source, data.flags);
62
+ }
63
+
64
+ // Check if data is a Buffer (for Node.js)
65
+ if (dataType === 'Buffer') {
66
+ return Buffer.from(data);
67
+ }
68
+
69
+ // Check if data is an ArrayBuffer or TypedArray
70
+ if (dataType === 'ArrayBuffer' || ArrayBuffer.isView(data)) {
71
+ return data.slice(0);
72
+ }
73
+
74
+ // Check if data is a WeakSet
75
+ if (dataType === 'WeakSet') {
76
+ return new WeakSet([...data]);
77
+ }
78
+
79
+ // Check if data is a WeakMap
80
+ if (dataType === 'WeakMap') {
81
+ return new WeakMap([...data]);
82
+ }
83
+
84
+ // Check if data is an Error
85
+ if (dataType === 'Error') {
86
+ return new Error(data.message);
87
+ }
88
+
89
+ // For other types (such as numbers, strings, booleans, etc.), return the original value
90
+ return data;
91
+ };
92
+
93
+ export default shallowCopy;