Skip to content

Commit 7103971

Browse files
committed
feat: add uid function to generate UUIDs with customizable length
1 parent 90fd526 commit 7103971

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@
114114
};
115115
}
116116

117+
function uid(length = 36) {
118+
let pattern = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
119+
if (length > 36) {
120+
length = 36; // If requested length is more than 36, set it to 36.
121+
}
122+
123+
let uuid = pattern.replace(/[xy]/g, function (c) {
124+
var r = (Math.random() * 16) | 0;
125+
var v = c === 'x' ? r : (r & 0x3 | 0x8);
126+
return v.toString(16);
127+
}).substring(0, length); // Truncate to the requested length.
128+
129+
return uuid;
130+
}
131+
117132
function checkValue(value) {
118133
if (/{{\s*([\w\W]+)\s*}}/g.test(value)) return false;
119134
else return true;
@@ -1286,6 +1301,7 @@
12861301
return {
12871302
getRelativePath,
12881303
ObjectId,
1304+
uid,
12891305
checkValue,
12901306
isValidDate,
12911307
dotNotationToObject,

0 commit comments

Comments
 (0)