-
Notifications
You must be signed in to change notification settings - Fork 275
-
I want to import my custom JavaScript library.
But there was an error during the library import.
What should I do?
image
error msg:
Cannot set properties of undefined (setting 'ULID')
CODE:
/** * Constructs a ULID generator closure that emits universally unique, * monotonic values. * * let generator = ULID(); * let ulid0 = generator(); * let ulid1 = generator(); * assert(ulid0 < ulid1); */ function ULID() { const 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' ]; let last = -1; /* Pre-allocate work buffers / views */ let ulid = new Uint8Array(16); let time = new DataView(ulid.buffer, 0, 6); let rand = new Uint8Array(ulid.buffer, 6, 10); let dest = new Array(26); function encode(ulid) { dest[0] = BASE32[ ulid[0] >> 5]; dest[1] = BASE32[(ulid[0] >> 0) & 0x1f]; for (let 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() { let now = Date.now(); if (now === last) { /* 80-bit overflow is so incredibly unlikely that it's not * considered as a possiblity here. */ for (let 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); }; } exports.ULID = ULID
Beta Was this translation helpful? Give feedback.
All reactions
problem solve.
The custom JavaScript library needs to be imported using the UMD approach.
library import success
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 = gene...
Replies: 1 comment 1 reply
-
problem solve.
The custom JavaScript library needs to be imported using the UMD approach.
library import success
image
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
code source:https://gist.github.com/kamleshchandnani/07c63f3d728672d91f97b69bbf700eed
// 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.
All reactions
-
❤️ 1
-
This is truly valuable! Thank you very much!
Beta Was this translation helpful? Give feedback.