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 path = require("path"); | |
| const findUp = require("find-up"); | |
| const globby = require("globby"); | |
| const getLernaRoot = () => findUp.sync("lerna.json"); | |
| const getPackageJsonRoot = () => findUp.sync("package.json"); | |
| const toPackageJsonPath = originalPath => |
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 { readFileSync, writeFileSync } = require('fs'); | |
| const writeNamedExportsToPathOfPkg = (output, path, namedExports) => { | |
| if (!namedExports || namedExports.length === 0) { | |
| return; | |
| } | |
| const pkgJson = JSON.parse(readFileSync(output, 'utf8')); | |
| let level = pkgJson; |
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
| #!/usr/bin/env node | |
| function window(values = [], step = 1, size = 1) { | |
| function* _window() { | |
| for (let i = 0; i < values.length; i += step) { | |
| const items = values.slice(i, i + size); | |
| yield items; | |
| } | |
| } |
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
| #!/usr/bin/env ts-node | |
| type ValueFn = (index?: number) => any | Promise<any>; | |
| const identity = v => v; | |
| async function* range(start: number, end: number, value: ValueFn = identity) { | |
| let index = start; | |
| while (index < end) { | |
| yield await value(index); |
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 deepClone = (value, seen = new Map()) => { | |
| if (seen.has(value)) { | |
| return value; | |
| } else { | |
| seen.set(value, true); | |
| } | |
| if (Array.isArray(value)) { | |
| const newArr = []; | |
| for (let el of 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
| type GetValueFn<TIdentity, TValue> = (id: TIdentity) => Promise<TValue>; | |
| type KeyValueObject<TValue> = { [key: string]: TValue }; | |
| const getObject = <TIdentity, TValue>( | |
| getValue: GetValueFn<TIdentity, TValue> | |
| ) => async (itemIds: Array<TIdentity>): Promise<KeyValueObject<TValue>> => { | |
| return Object.fromEntries( | |
| await Promise.all( | |
| itemIds.map(async itemId => [itemId, await getValue(itemId)]) | |
| ) |
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 floodFill = (grid = [], startPoint, newColor) => { | |
| const [y, x] = startPoint; | |
| const startColor = grid[y][x]; | |
| const points = [startPoint]; | |
| while (points.length) { | |
| const [y, x] = points.shift(); | |
| grid[y][x] = null; |
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 takeNumber = pattern => { | |
| const [_number] = pattern.match(/^\d+/g) || []; | |
| return parseInt(_number, 10) || 1; | |
| }; | |
| const str = pattern => { | |
| let count = 1; | |
| let parts = ""; | |
| let part = ""; |
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 permute(arr) { | |
| if (arr.length === 2) { | |
| const [a, b] = arr; | |
| return [[a, b], [b, a]]; | |
| } | |
| let permutationItems = []; | |
| for (let idx = 0; idx < arr.length; idx++) { | |
| const first = arr[idx]; |
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
| #!/usr/bin/env node | |
| function intersect(x, y) { | |
| const setX = new Set(x); | |
| const setY = new Set(y); | |
| let intersected = []; | |
| for (let value of setX) { | |
| // I worked with Sets because of an assumption that this is | |
| // cheaper than Array#includes. |