Last active
February 22, 2018 11:38
Revisions
-
SergProduction revised this gist
Feb 22, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -29,7 +29,7 @@ const createType = (type) => { if (type) { return (val) => type(val) ? val : null } const newType = (otherType) => otherType === newType || newType return newType } /* -
SergProduction revised this gist
Feb 22, 2018 . 1 changed file with 28 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -25,6 +25,15 @@ const types = () => { } } const createType = (type) => { if (type) { return (val) => type(val) ? val : null } const newType = (otherType) => type === newType || newType return newType } /* // ------feature------ const createFunc = (...types) => (fn) => (...param) => { const isValid = types.every((type, i) => type(param[i])) if (isValid === false) { @@ -35,25 +44,31 @@ const createFunc = (...types) => (fn) => (...param) => { } const createFuncR = (...types) => (...param) => (fn) => createFuncType(...types)(fn)(...param) */ // ------used------ // primitive types const t = types() const Person = createType(t.objectOf({ id: t.number, name: t.string, })) const Personal = createType(t.arrayOf(Person)) const person1 = {id: 2, name: 'Alex'} const person2 = {id: 1, name: 'Joh'} const personal = [person1, person2] Person(person1) Personal(personal) // new types const True = createType() const False = createType() const Bool = t.oneOfType([True, False]) -
SergProduction revised this gist
Feb 20, 2018 . No changes.There are no files selected for viewing
-
SergProduction revised this gist
Feb 20, 2018 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -11,6 +11,8 @@ const types = () => { const arrayOf = (type) => (val) => getType(val) === 'array' && type(val[0]) const objectOf = (obj) => (val) => Object.entries(obj).every(([key, value]) => value(val[key])) const oneOfType = (types) => (val) => types.some(type => type(val)) const oneOf = (values) => (val) => values.some(type => type === val) const sumOf = (values) => (param) => values.every((type, i) => type === param[i]) return { func, @@ -33,7 +35,6 @@ const createFunc = (...types) => (fn) => (...param) => { } const createFuncR = (...types) => (...param) => (fn) => createFuncType(...types)(fn)(...param) // ------used------ /* const t = types() @@ -55,6 +56,4 @@ const logPersonal = createFunc(Personal, t.bool)((p1, p2) => { }) logPersonal(personal, true) */ -
SergProduction created this gist
Feb 20, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,60 @@ // ------lib------ const getType = (o) => { return Object.prototype.toString.call(o).slice(8,-1).toLocaleLowerCase() } const types = () => { const func = (val) => getType(val) === 'function' const number = (val) => getType(val) === 'number' const string = (val) => getType(val) === 'string' const bool = (val) => getType(val) === 'boolean' const arrayOf = (type) => (val) => getType(val) === 'array' && type(val[0]) const objectOf = (obj) => (val) => Object.entries(obj).every(([key, value]) => value(val[key])) const oneOfType = (types) => (val) => types.some(type => type(val)) return { func, number, string, bool, arrayOf, objectOf, oneOfType, } } const createFunc = (...types) => (fn) => (...param) => { const isValid = types.every((type, i) => type(param[i])) if (isValid === false) { console.error('type is not valid') return } fn(...param) } const createFuncR = (...types) => (...param) => (fn) => createFuncType(...types)(fn)(...param) // ------used------ /* const t = types() const Preson = t.objectOf({ id: t.number, name: t.string, }) const Personal = t.arrayOf(Preson) const personal = [ {id: 1, name: 'Alex'}, {id: 1, name: 'Joh'} ] const logPersonal = createFunc(Personal, t.bool)((p1, p2) => { console.log(p1, p2) }) logPersonal(personal, true) */