@codady/utils 0.0.8 → 0.0.9
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/dist/utils.cjs.js +20 -3
- package/dist/utils.cjs.min.js +3 -3
- package/dist/utils.esm.js +20 -3
- package/dist/utils.esm.min.js +3 -3
- package/dist/utils.umd.js +20 -3
- package/dist/utils.umd.min.js +3 -3
- package/dist.zip +0 -0
- package/modules.js +4 -2
- package/modules.ts +4 -2
- package/package.json +1 -1
- package/src/getUniqueId.js +35 -0
- package/src/getUniqueId.ts +40 -0
package/dist/utils.cjs.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
/*!
|
|
3
|
-
* @since Last modified: 2025-12-19
|
|
3
|
+
* @since Last modified: 2025-12-19 15:23:38
|
|
4
4
|
* @name Utils for web front-end.
|
|
5
|
-
* @version 0.0.
|
|
5
|
+
* @version 0.0.9
|
|
6
6
|
* @author AXUI development team <3217728223@qq.com>
|
|
7
7
|
* @description This is a set of general-purpose JavaScript utility functions developed by the AXUI team. All functions are pure and do not involve CSS or other third-party libraries. They are suitable for any web front-end environment.
|
|
8
8
|
* @see {@link https://www.axui.cn|Official website}
|
|
@@ -203,6 +203,22 @@ const requireTypes = (data, require, cb) => {
|
|
|
203
203
|
return dataType;
|
|
204
204
|
};
|
|
205
205
|
|
|
206
|
+
const getUniqueId = (prefix, extra = true) => {
|
|
207
|
+
// Current timestamp in milliseconds (since Unix epoch)
|
|
208
|
+
// This provides the primary uniqueness guarantee
|
|
209
|
+
const timestamp = Date.now(),
|
|
210
|
+
// Generate a base-36 random string (0-9, a-z)
|
|
211
|
+
// Math.random() returns a number in [0, 1), converting to base-36 gives a compact representation
|
|
212
|
+
// substring(2, 11) extracts 9 characters starting from index 2
|
|
213
|
+
random = Math.random().toString(36).substring(2, 11),
|
|
214
|
+
// Additional 4-digit random number for extra randomness
|
|
215
|
+
// This helps avoid collisions in high-frequency generation scenarios
|
|
216
|
+
extraRandom = extra ? '_' + Math.floor(Math.random() * 10000).toString().padStart(4, '0') : '', prefixString = prefix ? prefix + '_' : '';
|
|
217
|
+
// Construct the final ID string
|
|
218
|
+
// Format: [prefix_]timestamp_randomBase36_extraRandom
|
|
219
|
+
return `${prefixString}${timestamp}_${random}${extraRandom}`;
|
|
220
|
+
};
|
|
221
|
+
|
|
206
222
|
const utils = {
|
|
207
223
|
//executeStr,
|
|
208
224
|
getDataType,
|
|
@@ -212,7 +228,8 @@ const utils = {
|
|
|
212
228
|
deepClone,
|
|
213
229
|
deepCloneToJSON,
|
|
214
230
|
wrapArrayMethods,
|
|
215
|
-
mutableMethods
|
|
231
|
+
mutableMethods,
|
|
232
|
+
getUniqueId
|
|
216
233
|
};
|
|
217
234
|
|
|
218
235
|
module.exports = utils;
|
package/dist/utils.cjs.min.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @since Last modified: 2025-12-19
|
|
2
|
+
* @since Last modified: 2025-12-19 15:23:38
|
|
3
3
|
* @name Utils for web front-end.
|
|
4
|
-
* @version 0.0.
|
|
4
|
+
* @version 0.0.9
|
|
5
5
|
* @author AXUI development team <3217728223@qq.com>
|
|
6
6
|
* @description This is a set of general-purpose JavaScript utility functions developed by the AXUI team. All functions are pure and do not involve CSS or other third-party libraries. They are suitable for any web front-end environment.
|
|
7
7
|
* @see {@link https://www.axui.cn|Official website}
|
|
@@ -12,4 +12,4 @@
|
|
|
12
12
|
* @copyright This software supports the MIT License, allowing free learning and commercial use, but please retain the terms 'ax,' 'axui,' 'AX,' and 'AXUI' within the software.
|
|
13
13
|
* @license MIT license
|
|
14
14
|
*/
|
|
15
|
-
"use strict";const getDataType=e=>{let t,r=Object.prototype.toString.call(e).slice(8,-1);return t="Function"===r&&/^\s*class\s+/.test(e.toString())?"Class":"Object"===r&&Object.getPrototypeOf(e)!==Object.prototype?"Instance":r,t},deepClone=e=>{const t=getDataType(e);if("Object"===t){const t={},r=Object.getOwnPropertySymbols(e);for(const r in e)t[r]=deepClone(e[r]);if(r.length>0)for(const o of r)t[o]=deepClone(e[o]);return t}if("Array"===t)return e.map(e=>deepClone(e));if("Date"===t)return new Date(e.getTime());if("RegExp"===t){const t=e;return new RegExp(t.source,t.flags)}return e},deepCloneToJSON=e=>{const t=getDataType(e);if("Object"===t){const t={};for(const r in e)t[r]=deepCloneToJSON(e[r]);for(const e in t)void 0===t[e]&&Reflect.deleteProperty(t,e);return t}if("Array"===t){return e.map((e,t)=>deepCloneToJSON(e)).filter(e=>void 0!==e)}return["Number","String","Boolean","Null"].includes(t)?e:void 0},mutableMethods=["push","pop","shift","unshift","splice","sort","reverse","copyWithin","fill"],wrapArrayMethods=({target:e,onBeforeMutate:t=()=>{},onAfterMutate:r=()=>{},allowList:o,props:
|
|
15
|
+
"use strict";const getDataType=e=>{let t,r=Object.prototype.toString.call(e).slice(8,-1);return t="Function"===r&&/^\s*class\s+/.test(e.toString())?"Class":"Object"===r&&Object.getPrototypeOf(e)!==Object.prototype?"Instance":r,t},deepClone=e=>{const t=getDataType(e);if("Object"===t){const t={},r=Object.getOwnPropertySymbols(e);for(const r in e)t[r]=deepClone(e[r]);if(r.length>0)for(const o of r)t[o]=deepClone(e[o]);return t}if("Array"===t)return e.map(e=>deepClone(e));if("Date"===t)return new Date(e.getTime());if("RegExp"===t){const t=e;return new RegExp(t.source,t.flags)}return e},deepCloneToJSON=e=>{const t=getDataType(e);if("Object"===t){const t={};for(const r in e)t[r]=deepCloneToJSON(e[r]);for(const e in t)void 0===t[e]&&Reflect.deleteProperty(t,e);return t}if("Array"===t){return e.map((e,t)=>deepCloneToJSON(e)).filter(e=>void 0!==e)}return["Number","String","Boolean","Null"].includes(t)?e:void 0},mutableMethods=["push","pop","shift","unshift","splice","sort","reverse","copyWithin","fill"],wrapArrayMethods=({target:e,onBeforeMutate:t=()=>{},onAfterMutate:r=()=>{},allowList:o,props:a={}})=>{if(!Array.isArray(e))throw new TypeError("The 'target' parameter must be an array.");o&&!o?.length||(o=mutableMethods);const s={};for(let n of o)s[n]=function(...o){const s={},i=e.length;switch(n){case"push":case"unshift":s.addedItems=[...o];break;case"pop":s.poppedValue=e[i-1];break;case"shift":s.shiftedValue=e[0];break;case"splice":const[t,r]=o,a=t<0?Math.max(i+t,0):Math.min(t,i),n=void 0===r?i-a:r;s.deletedItems=e.slice(a,a+n);break;case"sort":case"reverse":s.oldSnapshot=[...e];break;case"fill":case"copyWithin":const p=o[1]||0,l=void 0===o[2]?i:o[2];s.oldItems=e.slice(p,l),s.start=p,s.end=l}t?.(s);const p=Array.prototype[n].apply(e,o),l={value:p,key:n,args:o,context:s,target:e,...a};return r?.(l),p};return s},requireTypes=(e,t,r)=>{let o=Array.isArray(t)?t:[t],a=getDataType(e),s=a.toLowerCase(),n=o.map(e=>e.toLowerCase()),i=s.includes("html")?"element":s;if(r)try{if(!n.includes(i))throw new TypeError(`Expected data type(s): [${n.join(", ")}], but got: ${i}`)}catch(e){r(e,a)}else if(!n.includes(i))throw new TypeError(`Expected data type(s): [${n.join(", ")}], but got: ${i}`);return a},getUniqueId=(e,t=!0)=>`${e?e+"_":""}${Date.now()}_${Math.random().toString(36).substring(2,11)}${t?"_"+Math.floor(1e4*Math.random()).toString().padStart(4,"0"):""}`,utils={getDataType:getDataType,requireTypes:requireTypes,deepClone:deepClone,deepCloneToJSON:deepCloneToJSON,wrapArrayMethods:wrapArrayMethods,mutableMethods:mutableMethods,getUniqueId:getUniqueId};module.exports=utils;
|
package/dist/utils.esm.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
/*!
|
|
3
|
-
* @since Last modified: 2025-12-19
|
|
3
|
+
* @since Last modified: 2025-12-19 15:23:38
|
|
4
4
|
* @name Utils for web front-end.
|
|
5
|
-
* @version 0.0.
|
|
5
|
+
* @version 0.0.9
|
|
6
6
|
* @author AXUI development team <3217728223@qq.com>
|
|
7
7
|
* @description This is a set of general-purpose JavaScript utility functions developed by the AXUI team. All functions are pure and do not involve CSS or other third-party libraries. They are suitable for any web front-end environment.
|
|
8
8
|
* @see {@link https://www.axui.cn|Official website}
|
|
@@ -201,6 +201,22 @@ const requireTypes = (data, require, cb) => {
|
|
|
201
201
|
return dataType;
|
|
202
202
|
};
|
|
203
203
|
|
|
204
|
+
const getUniqueId = (prefix, extra = true) => {
|
|
205
|
+
// Current timestamp in milliseconds (since Unix epoch)
|
|
206
|
+
// This provides the primary uniqueness guarantee
|
|
207
|
+
const timestamp = Date.now(),
|
|
208
|
+
// Generate a base-36 random string (0-9, a-z)
|
|
209
|
+
// Math.random() returns a number in [0, 1), converting to base-36 gives a compact representation
|
|
210
|
+
// substring(2, 11) extracts 9 characters starting from index 2
|
|
211
|
+
random = Math.random().toString(36).substring(2, 11),
|
|
212
|
+
// Additional 4-digit random number for extra randomness
|
|
213
|
+
// This helps avoid collisions in high-frequency generation scenarios
|
|
214
|
+
extraRandom = extra ? '_' + Math.floor(Math.random() * 10000).toString().padStart(4, '0') : '', prefixString = prefix ? prefix + '_' : '';
|
|
215
|
+
// Construct the final ID string
|
|
216
|
+
// Format: [prefix_]timestamp_randomBase36_extraRandom
|
|
217
|
+
return `${prefixString}${timestamp}_${random}${extraRandom}`;
|
|
218
|
+
};
|
|
219
|
+
|
|
204
220
|
const utils = {
|
|
205
221
|
//executeStr,
|
|
206
222
|
getDataType,
|
|
@@ -210,7 +226,8 @@ const utils = {
|
|
|
210
226
|
deepClone,
|
|
211
227
|
deepCloneToJSON,
|
|
212
228
|
wrapArrayMethods,
|
|
213
|
-
mutableMethods
|
|
229
|
+
mutableMethods,
|
|
230
|
+
getUniqueId
|
|
214
231
|
};
|
|
215
232
|
|
|
216
233
|
export { utils as default };
|
package/dist/utils.esm.min.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @since Last modified: 2025-12-19
|
|
2
|
+
* @since Last modified: 2025-12-19 15:23:38
|
|
3
3
|
* @name Utils for web front-end.
|
|
4
|
-
* @version 0.0.
|
|
4
|
+
* @version 0.0.9
|
|
5
5
|
* @author AXUI development team <3217728223@qq.com>
|
|
6
6
|
* @description This is a set of general-purpose JavaScript utility functions developed by the AXUI team. All functions are pure and do not involve CSS or other third-party libraries. They are suitable for any web front-end environment.
|
|
7
7
|
* @see {@link https://www.axui.cn|Official website}
|
|
@@ -12,4 +12,4 @@
|
|
|
12
12
|
* @copyright This software supports the MIT License, allowing free learning and commercial use, but please retain the terms 'ax,' 'axui,' 'AX,' and 'AXUI' within the software.
|
|
13
13
|
* @license MIT license
|
|
14
14
|
*/
|
|
15
|
-
const getDataType=e=>{let t,r=Object.prototype.toString.call(e).slice(8,-1);return t="Function"===r&&/^\s*class\s+/.test(e.toString())?"Class":"Object"===r&&Object.getPrototypeOf(e)!==Object.prototype?"Instance":r,t},deepClone=e=>{const t=getDataType(e);if("Object"===t){const t={},r=Object.getOwnPropertySymbols(e);for(const r in e)t[r]=deepClone(e[r]);if(r.length>0)for(const o of r)t[o]=deepClone(e[o]);return t}if("Array"===t)return e.map(e=>deepClone(e));if("Date"===t)return new Date(e.getTime());if("RegExp"===t){const t=e;return new RegExp(t.source,t.flags)}return e},deepCloneToJSON=e=>{const t=getDataType(e);if("Object"===t){const t={};for(const r in e)t[r]=deepCloneToJSON(e[r]);for(const e in t)void 0===t[e]&&Reflect.deleteProperty(t,e);return t}if("Array"===t){return e.map((e,t)=>deepCloneToJSON(e)).filter(e=>void 0!==e)}return["Number","String","Boolean","Null"].includes(t)?e:void 0},mutableMethods=["push","pop","shift","unshift","splice","sort","reverse","copyWithin","fill"],wrapArrayMethods=({target:e,onBeforeMutate:t=()=>{},onAfterMutate:r=()=>{},allowList:o,props:a={}})=>{if(!Array.isArray(e))throw new TypeError("The 'target' parameter must be an array.");o&&!o?.length||(o=mutableMethods);const
|
|
15
|
+
const getDataType=e=>{let t,r=Object.prototype.toString.call(e).slice(8,-1);return t="Function"===r&&/^\s*class\s+/.test(e.toString())?"Class":"Object"===r&&Object.getPrototypeOf(e)!==Object.prototype?"Instance":r,t},deepClone=e=>{const t=getDataType(e);if("Object"===t){const t={},r=Object.getOwnPropertySymbols(e);for(const r in e)t[r]=deepClone(e[r]);if(r.length>0)for(const o of r)t[o]=deepClone(e[o]);return t}if("Array"===t)return e.map(e=>deepClone(e));if("Date"===t)return new Date(e.getTime());if("RegExp"===t){const t=e;return new RegExp(t.source,t.flags)}return e},deepCloneToJSON=e=>{const t=getDataType(e);if("Object"===t){const t={};for(const r in e)t[r]=deepCloneToJSON(e[r]);for(const e in t)void 0===t[e]&&Reflect.deleteProperty(t,e);return t}if("Array"===t){return e.map((e,t)=>deepCloneToJSON(e)).filter(e=>void 0!==e)}return["Number","String","Boolean","Null"].includes(t)?e:void 0},mutableMethods=["push","pop","shift","unshift","splice","sort","reverse","copyWithin","fill"],wrapArrayMethods=({target:e,onBeforeMutate:t=()=>{},onAfterMutate:r=()=>{},allowList:o,props:a={}})=>{if(!Array.isArray(e))throw new TypeError("The 'target' parameter must be an array.");o&&!o?.length||(o=mutableMethods);const n={};for(let s of o)n[s]=function(...o){const n={},p=e.length;switch(s){case"push":case"unshift":n.addedItems=[...o];break;case"pop":n.poppedValue=e[p-1];break;case"shift":n.shiftedValue=e[0];break;case"splice":const[t,r]=o,a=t<0?Math.max(p+t,0):Math.min(t,p),s=void 0===r?p-a:r;n.deletedItems=e.slice(a,a+s);break;case"sort":case"reverse":n.oldSnapshot=[...e];break;case"fill":case"copyWithin":const i=o[1]||0,l=void 0===o[2]?p:o[2];n.oldItems=e.slice(i,l),n.start=i,n.end=l}t?.(n);const i=Array.prototype[s].apply(e,o),l={value:i,key:s,args:o,context:n,target:e,...a};return r?.(l),i};return n},requireTypes=(e,t,r)=>{let o=Array.isArray(t)?t:[t],a=getDataType(e),n=a.toLowerCase(),s=o.map(e=>e.toLowerCase()),p=n.includes("html")?"element":n;if(r)try{if(!s.includes(p))throw new TypeError(`Expected data type(s): [${s.join(", ")}], but got: ${p}`)}catch(e){r(e,a)}else if(!s.includes(p))throw new TypeError(`Expected data type(s): [${s.join(", ")}], but got: ${p}`);return a},getUniqueId=(e,t=!0)=>`${e?e+"_":""}${Date.now()}_${Math.random().toString(36).substring(2,11)}${t?"_"+Math.floor(1e4*Math.random()).toString().padStart(4,"0"):""}`,utils={getDataType:getDataType,requireTypes:requireTypes,deepClone:deepClone,deepCloneToJSON:deepCloneToJSON,wrapArrayMethods:wrapArrayMethods,mutableMethods:mutableMethods,getUniqueId:getUniqueId};export{utils as default};
|
package/dist/utils.umd.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
/*!
|
|
3
|
-
* @since Last modified: 2025-12-19
|
|
3
|
+
* @since Last modified: 2025-12-19 15:23:38
|
|
4
4
|
* @name Utils for web front-end.
|
|
5
|
-
* @version 0.0.
|
|
5
|
+
* @version 0.0.9
|
|
6
6
|
* @author AXUI development team <3217728223@qq.com>
|
|
7
7
|
* @description This is a set of general-purpose JavaScript utility functions developed by the AXUI team. All functions are pure and do not involve CSS or other third-party libraries. They are suitable for any web front-end environment.
|
|
8
8
|
* @see {@link https://www.axui.cn|Official website}
|
|
@@ -207,6 +207,22 @@
|
|
|
207
207
|
return dataType;
|
|
208
208
|
};
|
|
209
209
|
|
|
210
|
+
const getUniqueId = (prefix, extra = true) => {
|
|
211
|
+
// Current timestamp in milliseconds (since Unix epoch)
|
|
212
|
+
// This provides the primary uniqueness guarantee
|
|
213
|
+
const timestamp = Date.now(),
|
|
214
|
+
// Generate a base-36 random string (0-9, a-z)
|
|
215
|
+
// Math.random() returns a number in [0, 1), converting to base-36 gives a compact representation
|
|
216
|
+
// substring(2, 11) extracts 9 characters starting from index 2
|
|
217
|
+
random = Math.random().toString(36).substring(2, 11),
|
|
218
|
+
// Additional 4-digit random number for extra randomness
|
|
219
|
+
// This helps avoid collisions in high-frequency generation scenarios
|
|
220
|
+
extraRandom = extra ? '_' + Math.floor(Math.random() * 10000).toString().padStart(4, '0') : '', prefixString = prefix ? prefix + '_' : '';
|
|
221
|
+
// Construct the final ID string
|
|
222
|
+
// Format: [prefix_]timestamp_randomBase36_extraRandom
|
|
223
|
+
return `${prefixString}${timestamp}_${random}${extraRandom}`;
|
|
224
|
+
};
|
|
225
|
+
|
|
210
226
|
const utils = {
|
|
211
227
|
//executeStr,
|
|
212
228
|
getDataType,
|
|
@@ -216,7 +232,8 @@
|
|
|
216
232
|
deepClone,
|
|
217
233
|
deepCloneToJSON,
|
|
218
234
|
wrapArrayMethods,
|
|
219
|
-
mutableMethods
|
|
235
|
+
mutableMethods,
|
|
236
|
+
getUniqueId
|
|
220
237
|
};
|
|
221
238
|
|
|
222
239
|
return utils;
|
package/dist/utils.umd.min.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @since Last modified: 2025-12-19
|
|
2
|
+
* @since Last modified: 2025-12-19 15:23:38
|
|
3
3
|
* @name Utils for web front-end.
|
|
4
|
-
* @version 0.0.
|
|
4
|
+
* @version 0.0.9
|
|
5
5
|
* @author AXUI development team <3217728223@qq.com>
|
|
6
6
|
* @description This is a set of general-purpose JavaScript utility functions developed by the AXUI team. All functions are pure and do not involve CSS or other third-party libraries. They are suitable for any web front-end environment.
|
|
7
7
|
* @see {@link https://www.axui.cn|Official website}
|
|
@@ -12,4 +12,4 @@
|
|
|
12
12
|
* @copyright This software supports the MIT License, allowing free learning and commercial use, but please retain the terms 'ax,' 'axui,' 'AX,' and 'AXUI' within the software.
|
|
13
13
|
* @license MIT license
|
|
14
14
|
*/
|
|
15
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).utils=t()}(this,function(){"use strict";const getDataType=e=>{let t,r=Object.prototype.toString.call(e).slice(8,-1);return t="Function"===r&&/^\s*class\s+/.test(e.toString())?"Class":"Object"===r&&Object.getPrototypeOf(e)!==Object.prototype?"Instance":r,t},deepClone=e=>{const t=getDataType(e);if("Object"===t){const t={},r=Object.getOwnPropertySymbols(e);for(const r in e)t[r]=deepClone(e[r]);if(r.length>0)for(const o of r)t[o]=deepClone(e[o]);return t}if("Array"===t)return e.map(e=>deepClone(e));if("Date"===t)return new Date(e.getTime());if("RegExp"===t){const t=e;return new RegExp(t.source,t.flags)}return e},deepCloneToJSON=e=>{const t=getDataType(e);if("Object"===t){const t={};for(const r in e)t[r]=deepCloneToJSON(e[r]);for(const e in t)void 0===t[e]&&Reflect.deleteProperty(t,e);return t}if("Array"===t){return e.map((e,t)=>deepCloneToJSON(e)).filter(e=>void 0!==e)}return["Number","String","Boolean","Null"].includes(t)?e:void 0},e=["push","pop","shift","unshift","splice","sort","reverse","copyWithin","fill"];return{getDataType:getDataType,requireTypes:(e,t,r)=>{let o=Array.isArray(t)?t:[t],
|
|
15
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).utils=t()}(this,function(){"use strict";const getDataType=e=>{let t,r=Object.prototype.toString.call(e).slice(8,-1);return t="Function"===r&&/^\s*class\s+/.test(e.toString())?"Class":"Object"===r&&Object.getPrototypeOf(e)!==Object.prototype?"Instance":r,t},deepClone=e=>{const t=getDataType(e);if("Object"===t){const t={},r=Object.getOwnPropertySymbols(e);for(const r in e)t[r]=deepClone(e[r]);if(r.length>0)for(const o of r)t[o]=deepClone(e[o]);return t}if("Array"===t)return e.map(e=>deepClone(e));if("Date"===t)return new Date(e.getTime());if("RegExp"===t){const t=e;return new RegExp(t.source,t.flags)}return e},deepCloneToJSON=e=>{const t=getDataType(e);if("Object"===t){const t={};for(const r in e)t[r]=deepCloneToJSON(e[r]);for(const e in t)void 0===t[e]&&Reflect.deleteProperty(t,e);return t}if("Array"===t){return e.map((e,t)=>deepCloneToJSON(e)).filter(e=>void 0!==e)}return["Number","String","Boolean","Null"].includes(t)?e:void 0},e=["push","pop","shift","unshift","splice","sort","reverse","copyWithin","fill"];return{getDataType:getDataType,requireTypes:(e,t,r)=>{let o=Array.isArray(t)?t:[t],n=getDataType(e),s=n.toLowerCase(),a=o.map(e=>e.toLowerCase()),i=s.includes("html")?"element":s;if(r)try{if(!a.includes(i))throw new TypeError(`Expected data type(s): [${a.join(", ")}], but got: ${i}`)}catch(e){r(e,n)}else if(!a.includes(i))throw new TypeError(`Expected data type(s): [${a.join(", ")}], but got: ${i}`);return n},deepClone:deepClone,deepCloneToJSON:deepCloneToJSON,wrapArrayMethods:({target:t,onBeforeMutate:r=()=>{},onAfterMutate:o=()=>{},allowList:n,props:s={}})=>{if(!Array.isArray(t))throw new TypeError("The 'target' parameter must be an array.");n&&!n?.length||(n=e);const a={};for(let e of n)a[e]=function(...n){const a={},i=t.length;switch(e){case"push":case"unshift":a.addedItems=[...n];break;case"pop":a.poppedValue=t[i-1];break;case"shift":a.shiftedValue=t[0];break;case"splice":const[e,r]=n,o=e<0?Math.max(i+e,0):Math.min(e,i),s=void 0===r?i-o:r;a.deletedItems=t.slice(o,o+s);break;case"sort":case"reverse":a.oldSnapshot=[...t];break;case"fill":case"copyWithin":const c=n[1]||0,l=void 0===n[2]?i:n[2];a.oldItems=t.slice(c,l),a.start=c,a.end=l}r?.(a);const c=Array.prototype[e].apply(t,n),l={value:c,key:e,args:n,context:a,target:t,...s};return o?.(l),c};return a},mutableMethods:e,getUniqueId:(e,t=!0)=>`${e?e+"_":""}${Date.now()}_${Math.random().toString(36).substring(2,11)}${t?"_"+Math.floor(1e4*Math.random()).toString().padStart(4,"0"):""}`}});
|
package/dist.zip
CHANGED
|
Binary file
|
package/modules.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Last modified: 2025/12/19
|
|
2
|
+
* Last modified: 2025/12/19 15:23:21
|
|
3
3
|
*/
|
|
4
4
|
'use strict';
|
|
5
5
|
import deepClone from './src/deepClone';
|
|
@@ -8,6 +8,7 @@ import getDataType from './src/getDataType';
|
|
|
8
8
|
import wrapArrayMethods from './src/wrapArrayMethods';
|
|
9
9
|
import mutableMethods from './src/mutableMethods';
|
|
10
10
|
import requireTypes from './src/requireTypes';
|
|
11
|
+
import getUniqueId from './src/getUniqueId';
|
|
11
12
|
const utils = {
|
|
12
13
|
//executeStr,
|
|
13
14
|
getDataType,
|
|
@@ -17,6 +18,7 @@ const utils = {
|
|
|
17
18
|
deepClone,
|
|
18
19
|
deepCloneToJSON,
|
|
19
20
|
wrapArrayMethods,
|
|
20
|
-
mutableMethods
|
|
21
|
+
mutableMethods,
|
|
22
|
+
getUniqueId
|
|
21
23
|
};
|
|
22
24
|
export default utils;
|
package/modules.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Last modified: 2025/12/19
|
|
2
|
+
* Last modified: 2025/12/19 15:23:21
|
|
3
3
|
*/
|
|
4
4
|
'use strict'
|
|
5
5
|
import deepClone from './src/deepClone';
|
|
@@ -11,6 +11,7 @@ import mutableMethods from './src/mutableMethods';
|
|
|
11
11
|
import parseStr from './src/parseStr';
|
|
12
12
|
import renderTpl from './src/renderTpl';
|
|
13
13
|
import requireTypes from './src/requireTypes';
|
|
14
|
+
import getUniqueId from './src/getUniqueId';
|
|
14
15
|
|
|
15
16
|
|
|
16
17
|
|
|
@@ -23,7 +24,8 @@ const utils = {
|
|
|
23
24
|
deepClone,
|
|
24
25
|
deepCloneToJSON,
|
|
25
26
|
wrapArrayMethods,
|
|
26
|
-
mutableMethods
|
|
27
|
+
mutableMethods,
|
|
28
|
+
getUniqueId
|
|
27
29
|
};
|
|
28
30
|
|
|
29
31
|
export default utils;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codady/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"author": "AXUI Development Team",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "This is a set of general-purpose JavaScript utility functions developed by the AXUI team. All functions are pure and do not involve CSS or other third-party libraries. They are suitable for any web front-end environment.",
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2025/12/19 15:18:57
|
|
3
|
+
* Generates a unique identifier string
|
|
4
|
+
*
|
|
5
|
+
* The ID format is: `[prefix_]timestamp_randomBase36_extraRandom`
|
|
6
|
+
* Example outputs:
|
|
7
|
+
* - "1734597601234_abc123def4567"
|
|
8
|
+
* - "snax_1734597601235_ghi789jkl0123_8899"
|
|
9
|
+
*
|
|
10
|
+
* Features:
|
|
11
|
+
* 1. Millisecond timestamp ensures uniqueness across time
|
|
12
|
+
* 2. Base-36 random string (9 chars = ~3.5e13 possibilities)
|
|
13
|
+
* 3. Additional 4-digit random number for extra entropy
|
|
14
|
+
* 4. Optional prefix for categorization
|
|
15
|
+
*
|
|
16
|
+
* @param prefix - Optional prefix string for the ID (e.g., 'snax', 'user', 'doc')
|
|
17
|
+
* @param extra - Optional 4-digit random number
|
|
18
|
+
* @returns A unique identifier string
|
|
19
|
+
*/
|
|
20
|
+
export const getUniqueId = (prefix, extra = true) => {
|
|
21
|
+
// Current timestamp in milliseconds (since Unix epoch)
|
|
22
|
+
// This provides the primary uniqueness guarantee
|
|
23
|
+
const timestamp = Date.now(),
|
|
24
|
+
// Generate a base-36 random string (0-9, a-z)
|
|
25
|
+
// Math.random() returns a number in [0, 1), converting to base-36 gives a compact representation
|
|
26
|
+
// substring(2, 11) extracts 9 characters starting from index 2
|
|
27
|
+
random = Math.random().toString(36).substring(2, 11),
|
|
28
|
+
// Additional 4-digit random number for extra randomness
|
|
29
|
+
// This helps avoid collisions in high-frequency generation scenarios
|
|
30
|
+
extraRandom = extra ? '_' + Math.floor(Math.random() * 10000).toString().padStart(4, '0') : '', prefixString = prefix ? prefix + '_' : '';
|
|
31
|
+
// Construct the final ID string
|
|
32
|
+
// Format: [prefix_]timestamp_randomBase36_extraRandom
|
|
33
|
+
return `${prefixString}${timestamp}_${random}${extraRandom}`;
|
|
34
|
+
};
|
|
35
|
+
export default getUniqueId;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2025/12/19 15:18:57
|
|
3
|
+
* Generates a unique identifier string
|
|
4
|
+
*
|
|
5
|
+
* The ID format is: `[prefix_]timestamp_randomBase36_extraRandom`
|
|
6
|
+
* Example outputs:
|
|
7
|
+
* - "1734597601234_abc123def4567"
|
|
8
|
+
* - "snax_1734597601235_ghi789jkl0123_8899"
|
|
9
|
+
*
|
|
10
|
+
* Features:
|
|
11
|
+
* 1. Millisecond timestamp ensures uniqueness across time
|
|
12
|
+
* 2. Base-36 random string (9 chars = ~3.5e13 possibilities)
|
|
13
|
+
* 3. Additional 4-digit random number for extra entropy
|
|
14
|
+
* 4. Optional prefix for categorization
|
|
15
|
+
*
|
|
16
|
+
* @param prefix - Optional prefix string for the ID (e.g., 'snax', 'user', 'doc')
|
|
17
|
+
* @param extra - Optional 4-digit random number
|
|
18
|
+
* @returns A unique identifier string
|
|
19
|
+
*/
|
|
20
|
+
export const getUniqueId = (prefix?: string, extra = true): string => {
|
|
21
|
+
// Current timestamp in milliseconds (since Unix epoch)
|
|
22
|
+
// This provides the primary uniqueness guarantee
|
|
23
|
+
const timestamp = Date.now(),
|
|
24
|
+
|
|
25
|
+
// Generate a base-36 random string (0-9, a-z)
|
|
26
|
+
// Math.random() returns a number in [0, 1), converting to base-36 gives a compact representation
|
|
27
|
+
// substring(2, 11) extracts 9 characters starting from index 2
|
|
28
|
+
random = Math.random().toString(36).substring(2, 11),
|
|
29
|
+
|
|
30
|
+
// Additional 4-digit random number for extra randomness
|
|
31
|
+
// This helps avoid collisions in high-frequency generation scenarios
|
|
32
|
+
extraRandom = extra ? '_' + Math.floor(Math.random() * 10000).toString().padStart(4, '0') : '',
|
|
33
|
+
prefixString = prefix ? prefix + '_' : '';
|
|
34
|
+
|
|
35
|
+
// Construct the final ID string
|
|
36
|
+
// Format: [prefix_]timestamp_randomBase36_extraRandom
|
|
37
|
+
return `${prefixString}${timestamp}_${random}${extraRandom}`;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export default getUniqueId;
|