Created
February 5, 2019 06:09
-
-
Save reggi/df936ce1e67a87b3f9c40856dca75fd6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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