Skip to content

Instantly share code, notes, and snippets.

View 3mcd's full-sized avatar
🦌

Eric McDaniel 3mcd

🦌
View GitHub Profile
@3mcd
3mcd / camelize-dir.js
Created January 11, 2023 15:26
A node script to convert snake_case to camelCase in all files in a directory
import { resolve, extname } from "path";
import { readdir, readFile, writeFile } from "fs/promises";
async function* getFiles(dir, exclude) {
const dirents = await readdir(dir, { withFileTypes: true });
for (const dirent of dirents) {
const res = resolve(dir, dirent.name);
if (exclude.test(res)) continue;
if (dirent.isDirectory()) {
yield* getFiles(res, exclude);
@3mcd
3mcd / javelin-api.ts
Last active September 29, 2022 20:10
javelin API
import {
changed,
createComponent,
createApp,
createQuery,
createType,
Group,
Format,
createSchema,
useConst,
export default '';
type Tail<T extends any[]> = T extends [any, ...infer U] ? U : never;
type Dispatch = (state: any, ...args: any[]) => any;
type DispatchState<D extends Dispatch> = Parameters<D>[0] | ReturnType<D>;
type DispatchApi = { [key: string]: Dispatch };
type State<D extends DispatchApi> = {
[K in keyof D]: DispatchState<D[K]>;
}[keyof D];
@3mcd
3mcd / javelin-box-example.ts
Last active July 9, 2022 17:48
Javelin 1.0 docs box example
import {
after,
App,
Component,
Resource,
useKeyboard,
useQuery,
useResource,
World,
} from "@javelin/ecs"
@3mcd
3mcd / javelin-react-hooks.tsx
Created September 3, 2020 22:23
Javelin ECS React Binding Example
import {
createComponentType,
number,
query,
Query,
Selector,
SelectorResult,
World,
} from "@javelin/ecs"
import React, {
@3mcd
3mcd / redux-module.ts
Created February 18, 2019 21:05
Typescript Redux Modules
import { produce } from "immer";
type Reducer<S, A> = (state: S, action: A) => S;
interface ISelectorMap<S> {
[key: string]: (state: S, ...args: any[]) => any;
}
interface IActionCreatorMap {
[actionType: string]: (...args: any) => any;
import React, { Component, Fragment } from "react";
import { render } from "react-dom";
// Constrain
// A component that takes a render prop (children) and passes a proxied
// version of remaining props to the render prop function. When a prop is
// accessed in the render prop function, Constrain will re-render when
// that prop changes in the future.
class Constrain extends Component {
type Ctor<T> = new (...args: any[]) => T
abstract class BaseComponent {
abstract update(): IterableIterator<any>
protected children: BaseComponent[] = []
protected getComponent(ctor: Ctor<BaseComponent>) {
return this.children.find(x => x instanceof ctor)
}
@3mcd
3mcd / guard.js
Last active December 31, 2016 00:20
const $rest = Symbol('rest');
const $normal = Symbol('normal');
const $optional = Symbol('optional');
const $pattern = Symbol('pattern');
const eq = a => b => a === b;
const toString = val => Object.prototype.toString.call(val).split(' ')[1].slice(0, -1);
const isString = a => typeof a === 'string';
const isArray = a => Array.isArray(a);
const _ensureNew = (ctor) => {
const fn = (...args) => !(this instanceof ctor) ? new ctor(...args) : ctor(...args);
fn.prototype = ctor.prototype;
return fn;
}
const ensureNew = (ctor) => {
const fn = _ensureNew(ctor);
fn.extend = Backbone.Model.extend;
return fn;