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
// Ideally, Map would have this as a method, but the proposal is still only on | |
// stage 2: https://github.com/tc39/proposal-upsert | |
function emplace<K, V>( | |
map: Map<K, V>, | |
key: K, | |
supplier: (key: K) => V, | |
): V; | |
function emplace<K, V, This>( | |
map: Map<K, V>, | |
key: K, |
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 type AtLeastOne<T> = [T, ...T[]]; | |
export function groupByUsing<V, K extends keyof V, U>( | |
iterable: Iterable<V>, | |
key: K | ((value: V, index: number) => V[K]), | |
callback: (value: V, index: number) => U, | |
): Map<V[K], AtLeastOne<U>> { | |
const mapper = typeof key !== "function" ? (v: V) => v[key] : key; | |
const map = new Map<V[K], AtLeastOne<U>>(); |
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
/** | |
* Explode implementation. The inputs to this function are not validated, use | |
* {@link explode} for inputs that might not meet the requirements. | |
* | |
* @param delimiter Non-empty string | |
* @param string | |
* @param limit Integer in the range of [2 - 4294967295] | |
*/ | |
export function explodeUnchecked( | |
delimiter: string, |
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 drop(iterator, amount) { | |
for (let i = 0; !(i >= amount); ++i) { | |
if (iterator.next().done) { | |
break; | |
} | |
} | |
return iterator; | |
} |
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 once(fn) { | |
const { name, length } = fn; | |
let called = false; | |
// match .name of argument | |
const wrapped = { | |
[name]() { | |
if (!called) { | |
called = true; | |
fn.apply(this, arguments); |
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 const groupByUsing = (iterable, key, callback) => { | |
const mapper = typeof key !== "function" ? x => x[key] : key; | |
const map = new Map(); | |
let i = -1; | |
for (const value of iterable) { | |
const mappedKey = mapper(value, ++i, key); | |
const mappedValue = callback(value, i, key); | |
const group = map.get(mappedKey); |
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
async function concurrentWorker(context, callback) { | |
const { queue } = context; | |
const abortToken = Symbol(); | |
while (context.ok) { | |
const { done, value } = queue.next(); | |
if (done) { | |
context.ok = false; | |
return; | |
} |
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 const IndexableMap = class extends Map { | |
constructor(iterable) { | |
super(); | |
this._values = []; | |
if (iterable != null) { | |
for (const [key, value] of iterable) { | |
this.set(key, value); | |
} | |
} | |
} |
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* parseNdjson(ndjson) { | |
let i = 0; | |
do { | |
const newline = ndjson.indexOf("\n", i); | |
const string = newline === -1 ? ndjson.slice(i) : ndjson.slice(i, newline); | |
const json = string.trim(); | |
if (json) { | |
yield JSON.parse(json); | |
} | |
i = newline + 1; |
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
const map = { | |
"-1": "💥", | |
"0": "🟦", | |
}; | |
const inRange = (number, max) => 0 <= number && number < max; | |
const neighbors = [ | |
[-1, -1], [-1, 0], [-1, 1], | |
[0, -1], [ 0, 1], |