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
| /** | |
| * Augments a sort function with a bi-map cache. Useful if the comparison is costly. | |
| * @param {SortFunction} comparator | |
| * @returns {SortFunction} | |
| */ | |
| export function cachedComparator (comparator) { | |
| const cache = new Map(); | |
| return (a, b) => { | |
| let result; |
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 async function waitForNFulfilled (n, ...promises) { | |
| const { length } = promises; | |
| if (n >= length) | |
| return Promise.all(promises); | |
| const leftPromises = []; | |
| const tallyPromises = []; | |
| // Promises can resolve on the same tick, so we need to mark the fulfilled ones in parallel |
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 { Transform } from "node:stream"; | |
| import { StringDecoder } from "node:string_decoder"; | |
| const EOL = "\n", | |
| EOLLength = EOL.length; | |
| export function createParser (encoding = "utf8") { | |
| return new Transform({ | |
| decodeStrings: false, | |
| readableObjectMode: true, |
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
| /** | |
| * greatestCommonAncestor - Finds the common ancestor of nodes in a document. | |
| * The nodes MUST belong to the same document(fragment). | |
| * @param {...Node} nodes - The nodes whose common ancestor is sought. | |
| * @returns {Node} - The common ancestor to all nodes given as arguments. | |
| */ | |
| export function greatestCommonAncestor () { | |
| return Array.prototype.reduce.call( | |
| arguments, // partial application `?` when? :( | |
| (ancestor, node) => { |
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
| "use strict"; | |
| const fsP = require("fs").promises; | |
| const path = require("path"); | |
| const { cwd, chdir } = process; | |
| const MAX_ARRAY_LENGTH = 2**32 - 1; | |
| /** | |
| * getMatchingFiles - Lazily generates file paths in a given `targetDir` directory matching the predicate |
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
| "use strict"; | |
| const MAX_ARRAY_LENGTH = 2 ** 32 - 1; | |
| /** | |
| * Insertion sort FTW | |
| * @param str {string} | |
| * @param limit {number} | |
| * @returns {number[]|undefined} |
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
| // In-place mutation | |
| export function arrayBulkDelete (array, valueOrPredicate) { | |
| const { length } = array, | |
| predicate = typeof valueOrPredicate === "function" | |
| ? valueOrPredicate | |
| : (x) => x === valueOrPredicate, | |
| indicesToDelete = []; | |
| let lastIndex = -2; |
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
| "use strict"; | |
| /** | |
| * Generate numbers in the Fibonacci sequence in a given interval. | |
| * | |
| * @generator | |
| * @function fibInInterval | |
| * @param {bigint|number} min - The lower limit of the interval. | |
| * @param {bigint|number} max - The upper limit of the interval. | |
| * @yields {bigint} The next number in the Fibonacci sequence respecting the interval. |
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 nodeResetter = function () { | |
| const knownNodes = new WeakMap; | |
| return { | |
| setupNode (originalNode) { | |
| knownNodes.set(originalNode, originalNode.cloneNode(false)); | |
| }, | |
| resetNode (originalNode) { | |
| originalNode.parentNode.replaceChild( | |
| knownNodes.get(originalNode).cloneNode(false), |
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
| // PLEASE NOTE: this is LAZY! It will only yield properties that are part of `namesMap` | |
| // not all properties of `source` | |
| export function *transformPropertyNames (source, namesMap) { | |
| for ( | |
| const [ entryKey, targetKey ] of typeof namesMap[Symbol.iterator] === "function" | |
| ? namesMap // iterable | |
| : Object.entries(namesMap) // object-style | |
| ) | |
| yield [ targetKey, source[entryKey] ]; | |
| } |
NewerOlder