Skip to content

Instantly share code, notes, and snippets.

View nilscox's full-sized avatar

nilscox nilscox

View GitHub Profile
@nilscox
nilscox / mock-cross-fetch.spec.ts
Created October 1, 2021 10:31
Jest mock module example
import fetch from 'cross-fetch';
import { mocked } from 'ts-jest/utils';
import { getApiVersion } from './getApiVersion';
jest.mock('cross-fetch');
const mockFetch = mocked(fetch);
describe('getApiVersion', () => {
it('request the api version', async () => {
import { expect } from 'earljs';
type Range = [number, number];
const max = Math.max;
const lastElement = <T>(arr: T[]) => arr[arr.length - 1];
const overlap = (r1: Range, r2: Range): boolean => {
if (r1[0] > r2[0]) {
return overlap(r2, r1);
import { act, renderHook } from '@testing-library/react-hooks';
import useEditableDataset from '../useEditableDataset';
function render<T>(input?: T[], onUpdate?: 'prepend' | 'append') {
return renderHook(props => useEditableDataset(props.input, props.onUpdate), {
initialProps: { input, onUpdate },
});
}
@nilscox
nilscox / nodes-list-to-tree.spec.ts
Created August 22, 2021 17:59
Transform a tree from a nested set representation to a recursive structure.
import { expect } from 'earljs';
import { Node, nodesListToTree, StaticNode } from './nodes-list-to-tree';
const createStaticNode = (label: string, nLeft: number, nRight: number): StaticNode => ({
label,
nLeft,
nRight,
});
import EventEmitter from "events";
import { terminal as term } from "terminal-kit";
const randInt = (min: number, max: number) => {
return ~~(Math.random() * (max - min) + min);
};
const choose = <T>(arr: T[] | readonly T[]): T => {
return arr[randInt(0, arr.length)];
};
@nilscox
nilscox / relative-to-alias-imports.js
Created January 21, 2021 20:42
Replace all typescript imports starting with "../" with "src/..."
import fs from 'fs';
import glob from 'glob';
const files = glob(process.argv[2], { sync: true });
const re = /^(import .* from ')((\.\.\/)+)(.*)$/;
for (const filePath of files) {
const path = filePath.split('/').slice(1, -1);
const file = String(fs.readFileSync(filePath));
const stringifyRequest = (request) => {
const { req, res, _data } = request;
const requestStr = [
...req._header.split('\r\n').slice(0, -1),
...JSON.stringify(_data).split('\n'),
].map(line => '> ' + line).join('\r\n');
const responseStr = [
[res.statusCode, res.statusMessage, res.httpVersion].join(' '),
@nilscox
nilscox / main.js
Created November 20, 2018 12:43
async main
const main = async () => {
};
(async () => {
try {
await main();
process.exit(0);
}
catch (e) {
@nilscox
nilscox / networkStorage.js
Created June 27, 2018 10:05
Web storage through the network
// @flow
const NETWORK_STORAGE_URL = process.env.NETWORK_STORAGE_URL;
const storage = {
getItem: (key: string): Promise<string> => {
return fetch([NETWORK_STORAGE_URL, key].join('/'))
.then(res => {
if (res.status === 404)
@nilscox
nilscox / bench.js
Created December 19, 2017 11:14
JS array loop benchmark
COUNT=100000
function timefunc(f) {
const start = Date.now();
f();
const end = Date.now();
return end - start;
}
function array_foreach() {