Skip to content

Instantly share code, notes, and snippets.

View sebinsua's full-sized avatar
🐚

Seb Insua sebinsua

🐚
View GitHub Profile
"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 =>
@sebinsua
sebinsua / rollup.config.js
Last active June 3, 2018 17:43
Write named exports onto a path of a `package.json`
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;
@sebinsua
sebinsua / window-generator.js
Created August 28, 2018 23:19
window-generator.js
#!/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;
}
}
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)])
)
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;
const takeNumber = pattern => {
const [_number] = pattern.match(/^\d+/g) || [];
return parseInt(_number, 10) || 1;
};
const str = pattern => {
let count = 1;
let parts = "";
let part = "";
@sebinsua
sebinsua / permute.js
Last active January 29, 2020 00:27
Permutations and combinations from scratch.
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];
#!/usr/bin/env node
const matrix = [
[1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0],
[1, 1, 1, 0, 0, 0],
[1, 1, 1, 1, 1, 0],
[1, 1, 1, 0, 0, 0]
];
#!/usr/bin/env node
const NUMBERS = {
0: "zero",
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
@sebinsua
sebinsua / EventEmitter.js
Created February 10, 2020 16:03
This horrible EventEmitter design is the implementation of an interview question.
#!/usr/bin/env node
// This horrible design is the implementation of an interview question.
class Subscription {
constructor(parent, eventName, callback) {
this.parent = parent;
this.eventName = eventName;
this.callback = callback;
}