Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[solve]Unable to import my own JavaScript library#524

Answeredbyexqmjmz
exqmjmz asked this question inQ&A
Discussion options

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); */functionULID(){constBASE32=['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'];letlast=-1;/* Pre-allocate work buffers / views */letulid=newUint8Array(16);lettime=newDataView(ulid.buffer,0,6);letrand=newUint8Array(ulid.buffer,6,10);letdest=newArray(26);functionencode(ulid){dest[0]=BASE32[ulid[0]>>5];dest[1]=BASE32[(ulid[0]>>0)&0x1f];for(leti=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];}returndest.join('');}returnfunction(){letnow=Date.now();if(now===last){/* 80-bit overflow is so incredibly unlikely that it's not             * considered as a possiblity here.             */for(leti=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);}returnencode(ulid);};}exports.ULID=ULID
You must be logged in to vote

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(typeofdefine==="function"&&define.amd){define(["exports"],factory);}elseif(typeofexports!=="undefined"){factory(exports);}else{varmod={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

Comment options

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(typeofdefine==="function"&&define.amd){define(["exports"],factory);}elseif(typeofexports!=="undefined"){factory(exports);}else{varmod={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);     */functionULID(){varBASE32=['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'];varlast=-1;/* Pre-allocate work buffers / views */varulid=newUint8Array(16);vartime=newDataView(ulid.buffer,0,6);varrand=newUint8Array(ulid.buffer,6,10);vardest=newArray(26);functionencode(ulid){dest[0]=BASE32[ulid[0]>>5];dest[1]=BASE32[(ulid[0]>>0)&0x1f];for(vari=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];}returndest.join('');}returnfunction(){varnow=Date.now();if(now===last){/* 80-bit overflow is so incredibly unlikely that it's not                 * considered as a possiblity here.                 */for(vari=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);}returnencode(ulid);};}functiongenerator(){varulid=newULID()returnulid()}// expose ulid to other modulesexports.generator=generator;});

UMD model example code
code source:https://gist.github.com/kamleshchandnani/07c63f3d728672d91f97b69bbf700eed

// File log.js(function(global,factory){if(typeofdefine==="function"&&define.amd){define(["exports"],factory);}elseif(typeofexports!=="undefined"){factory(exports);}else{varmod={exports:{}};factory(mod.exports);global.log=mod.exports;}})(this,function(exports){"use strict";functionlog(){console.log("Example of UMD module system");}// expose log to other modulesexports.log=log;});
You must be logged in to vote
1 reply
@FalkWolsky
Comment options

This is truly valuable! Thank you very much!

Answer selected byFalkWolsky
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@exqmjmz@FalkWolsky

[8]ページ先頭

©2009-2025 Movatter.jp