-
Beta Was this translation helpful? Give feedback.
Answered by
exqmjmz
Nov 24, 2023
Replies: 1 comment 1 reply
-
problem solve. Modified code: // File log.js
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["exports"], factory);
} else if (typeof exports !== "undefined") {
factory(exports);
} else {
var mod = {
exports: {}
};
factory(mod.exports);
global.ULID = mod.exports;
}
})(this, function (exports) {
"use strict";
/**
* Constructs a ULID generator closure that emits universally unique,
* monotonic values.
*
* var generator = ULID();
* var ulid0 = generator();
* var ulid1 = generator();
* assert(ulid0 < ulid1);
*/
function ULID() {
var BASE32 = [
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q',
'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z'
];
var last = -1;
/* Pre-allocate work buffers / views */
var ulid = new Uint8Array(16);
var time = new DataView(ulid.buffer, 0, 6);
var rand = new Uint8Array(ulid.buffer, 6, 10);
var dest = new Array(26);
function encode(ulid) {
dest[0] = BASE32[ulid[0] >> 5];
dest[1] = BASE32[(ulid[0] >> 0) & 0x1f];
for (var i = 0; i < 3; i++) {
dest[i * 8 + 2] = BASE32[ulid[i * 5 + 1] >> 3];
dest[i * 8 + 3] = BASE32[(ulid[i * 5 + 1] << 2 | ulid[i * 5 + 2] >> 6) & 0x1f];
dest[i * 8 + 4] = BASE32[(ulid[i * 5 + 2] >> 1) & 0x1f];
dest[i * 8 + 5] = BASE32[(ulid[i * 5 + 2] << 4 | ulid[i * 5 + 3] >> 4) & 0x1f];
dest[i * 8 + 6] = BASE32[(ulid[i * 5 + 3] << 1 | ulid[i * 5 + 4] >> 7) & 0x1f];
dest[i * 8 + 7] = BASE32[(ulid[i * 5 + 4] >> 2) & 0x1f];
dest[i * 8 + 8] = BASE32[(ulid[i * 5 + 4] << 3 | ulid[i * 5 + 5] >> 5) & 0x1f];
dest[i * 8 + 9] = BASE32[(ulid[i * 5 + 5] >> 0) & 0x1f];
}
return dest.join('');
}
return function () {
var now = Date.now();
if (now === last) {
/* 80-bit overflow is so incredibly unlikely that it's not
* considered as a possiblity here.
*/
for (var i = 9; i >= 0; i--)
if (rand[i]++ < 255)
break;
} else {
last = now;
time.setUint16(0, (now / 4294967296.0) | 0);
time.setUint32(2, now | 0);
window.crypto.getRandomValues(rand);
}
return encode(ulid);
};
}
function generator(){
var ulid = new ULID()
return ulid()
}
// expose ulid to other modules
exports.generator = generator;
}); UMD model example code // File log.js
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["exports"], factory);
} else if (typeof exports !== "undefined") {
factory(exports);
} else {
var mod = {
exports: {}
};
factory(mod.exports);
global.log = mod.exports;
}
})(this, function (exports) {
"use strict";
function log() {
console.log("Example of UMD module system");
}
// expose log to other modules
exports.log = log;
}); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
FalkWolsky
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
problem solve.
The custom JavaScript library needs to be imported using the UMD approach.
library import success
Modified code: