Skip to content

Instantly share code, notes, and snippets.

View nilscox's full-sized avatar

nilscox nilscox

View GitHub Profile
@nilscox
nilscox / gci.sh
Last active September 20, 2016 17:36
C compile on-the-fly
#!/bin/sh
INCLUDES='
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
'
help() {
script="$1"
@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() {
@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 / main.js
Created November 20, 2018 12:43
async main
const main = async () => {
};
(async () => {
try {
await main();
process.exit(0);
}
catch (e) {
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 / 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));
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 / 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 { 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 },
});
}
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);