Skip to content

Instantly share code, notes, and snippets.

View fersilva16's full-sized avatar
🦀

Fernando Silva fersilva16

🦀
View GitHub Profile
@fersilva16
fersilva16 / transform-path-join.ts
Last active November 19, 2021 00:33
Transform `path.join` to string literal
import {
API,
ASTPath,
CallExpression,
FileInfo,
Literal,
MemberExpression,
StringLiteral,
VariableDeclaration
} from 'jscodeshift'
@fersilva16
fersilva16 / asyncEach.ts
Created December 14, 2021 14:49
Async iterators utils
export async function asyncEach<T>(
array: { map(callback: (value: T) => any): Promise<any>[] },
iteratee: (value: T) => Promise<any>,
) {
return Promise.all(array.map(iteratee));
}
export async function asyncEachSeries<T>(
array: {
reduce(callback: (prev: any, value: T, index: number) => any, initialValue: any): any;
@fersilva16
fersilva16 / recursive-chunk.ts
Last active December 14, 2021 23:15
Chunk array by n length
function chunk(n: number, xs: number[]) {
if (!xs.length) return [];
return [xs.slice(0, n), ...chunk(n, xs.slice(n))];
}
@fersilva16
fersilva16 / day05a.ts
Created December 19, 2021 16:44
Advent of Code 2021 - Day 05 A - TypeScript solution using mutable array
function day05a(xs: [number, number, number, number][], matrix: number[][]) {
xs.forEach((x) => {
const [x1, y1, x2, y2] = x;
if (x1 == x2 || y1 == y2) {
for (let yi = Math.min(y1, y2); yi <= Math.max(y1, y2); yi++) {
for (let xi = Math.min(x1, x2); xi <= Math.max(x1, x2); xi++)
m[yi][xi]++;
}
}
@fersilva16
fersilva16 / transform-import-shared.ts
Last active December 23, 2021 03:23
Transform imports from a file that has been moved to a new shared workspace
import type { API, FileInfo } from 'jscodeshift';
import path from 'path';
/**
* Path to the original file, if you're using imports without extensions,
* then don't provide the extension to the path
* example:
* import File from '../file.tsx'; // Use `path/to/original/file.tsx`
* import File from '../file'; // Use just `path/to/original/file`
*/
@fersilva16
fersilva16 / input.ts
Last active December 30, 2021 01:48
Transform imports to namespace import
import { load } from '@workspace/shared';
import Foo from '@workspace/shared';
import * as Bar from '@workspace/shared';
load();
Foo.doSomething();
Bar.default.doSomething();
function test() {
const load = {};
@fersilva16
fersilva16 / .eslintrc.js
Created December 29, 2021 01:32
ESLint config for monorepos that adds support for importing from root package
const fs = require('fs');
const glob = require('glob');
const { workspaces } = require('./package.json');
const noExtraneousOverrides = workspaces
.map((workspace) => {
return glob
.sync(workspace)
.filter((entry) => entry.substr(0, 1) !== '.' && fs.lstatSync(entry).isDirectory())
@fersilva16
fersilva16 / asyncReduce.ts
Created January 11, 2022 00:25
Async array reduce implementation
export async function asyncReduce<T, U>(
array: T[],
iteratee: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => Promise<U>,
initialValue: U
): Promise<U> {
return array.reduce(
(promise, currentValue, currentIndex) =>
promise.then((previousValue) => iteratee(previousValue, currentValue, currentIndex, array)),
Promise.resolve(initialValue)
);
@fersilva16
fersilva16 / transformMUIImports.ts
Last active January 13, 2022 19:13
Fix MUI imports from v4
import type { FileInfo, API } from 'jscodeshift';
const newPackagesMap: Record<string, string> = {
'@material-ui/core': '@mui/material',
'@material-ui/icons': '@mui/icons-material',
'@material-ui/styles': '@mui/styles',
};
const newPackages = Object.keys(newPackagesMap);
@fersilva16
fersilva16 / asyncFilter.ts
Created February 4, 2022 17:58
Async array filter
import { asyncEach } from './asyncEach'; // From https://gist.github.com/fersilva16/bdf2d0b9a5eedbcf7232760df876a87d
export async function asyncFilter<T>(
array: { map(callback: (value: T) => any): Promise<any>[] },
predicate: (value: T) => Promise<boolean>
): Promise<T[]> {
const result: T[] = [];
await asyncEach(array, async (value, ...args) => {
const shouldAdd = await predicate(value, ...args);