This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class LockError extends Error {} | |
| function createLock () { | |
| /** @type {boolean} */ | |
| let locked = false; | |
| /** @type {PromiseObject[]} */ | |
| let pending = []; | |
| function acquire () { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import fs from 'fs'; | |
| import path from 'path'; | |
| const fsp = fs.promises; | |
| /** | |
| * Creates a directory tree | |
| * @param {string} dirname Path to directory to read | |
| * @returns {object} A directory tree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function debounce<F extends ((...args: any) => any)> ( | |
| fn: F, | |
| ms: number | |
| ) { | |
| /** | |
| * Not the correct type, because of these conflicts: | |
| * - Node's setTimeout returns a `Timeout` object | |
| * - Browser's setTimeout returns a `number` | |
| * | |
| * TypeScript doesn't provide an option to specifically exclude certain type |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function range (from, to) { | |
| if (!to) { | |
| to = from; | |
| from = 0; | |
| } | |
| if (from > to) { | |
| throw RangeError('`from` is higher than `to`'); | |
| } | |
| return Array.from({ length: to - from }, (_, i) => i + from); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function readable (init, start) { | |
| function subscribe (listener) { | |
| let running = true; | |
| let prev = init; | |
| let stop; | |
| function set (curr) { | |
| if (running && curr !== prev) { | |
| prev = curr; | |
| if (stop) listener(curr); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function noop () {} | |
| export function writable (init, start) { | |
| let subscribers = []; | |
| let prev = init; | |
| let running = false; | |
| let stop; | |
| function notify () { | |
| let subs = subscribers.concat(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function count (str: string, search: string) { | |
| let n = -1; | |
| let i = 0; | |
| while (i !== -1) { | |
| i = str.indexOf(search, i + 1); | |
| n++; | |
| } | |
| return n; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let epoch = 1420070400000; | |
| let increment = 0; | |
| function timestamp_to_snowflake (timestamp = Date.now()) { | |
| let snowflake = ''; | |
| // timestamp | |
| snowflake += (timestamp - epoch).toString(2).padStart(42, '0'); | |
| // worker id | |
| snowflake += '00001'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 249 bytes | |
| var o={helloWorld:"hello-world-123",fooBar:"foo-bar-245",barBaz:"bar-baz-934"};let l=Object.assign(document.createElement("div"),{textContent:"Hello world!",className:[o.helloWorld,o.helloWorld,o.fooBar,o.barBaz].join(" ")});document.body.append(l); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function __async (generator) { | |
| return function (...args) { | |
| let it = generator.apply(this, args); | |
| return new Promise((resolve, reject) => { | |
| step(it.next()); | |
| function step (result) { | |
| if (result.done) resolve(result.value); | |
| else Promise.resolve(result.value).then(fulfilled, rejected); |