Skip to content

Instantly share code, notes, and snippets.

@reggi
Created February 5, 2019 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reggi/df936ce1e67a87b3f9c40856dca75fd6 to your computer and use it in GitHub Desktop.
Save reggi/df936ce1e67a87b3f9c40856dca75fd6 to your computer and use it in GitHub Desktop.
const libs = []
export enum Types {
number,
numbers,
string,
buffer,
}
export enum Actions {
add,
subtract,
readFile,
}
libs.push({
action: Actions.add,
from: Types.numbers,
get: Types.number,
fn: (...n: number[]): number => n.reduce((a, b) => a + b, 0),
})
libs.push({
action: Actions.subtract,
from: Types.numbers,
get: Types.number,
fn: (...n: number[]): number => n.reduce((a, b) => a - b, 0),
})
libs.push({
action: Actions.readFile,
from: Types.string,
get: Types.string,
fn: async (s:string): Promise<string> => promisify(fs.readFile)(s, 'UTF8'),
})
libs.push({
action: Actions.readFile,
from: Types.string,
get: Types.buffer,
fn: async (s:string): Promise<Buffer> => promisify(fs.readFile)(s),
})
const library = (a: Actions, from: Types, get: Types, lib) => {
const found = lodash.find(lib, fn => {
return (
lodash.isEqual(fn.from, from) &&
lodash.isEqual(fn.get, get)
);
});
if (!found) throw new Error('no conversion');
return found.fn;
}
const { readFile } = Actions;
const { string: s } = Types;
const x = library(readFile, s, s, libs)
x('./tres.ts').then(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment