Skip to content

Instantly share code, notes, and snippets.

@SergProduction
Last active February 22, 2018 11:38

Revisions

  1. SergProduction revised this gist Feb 22, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion types.js
    Original 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) => type === newType || newType
    const newType = (otherType) => otherType === newType || newType
    return newType
    }
    /*
  2. SergProduction revised this gist Feb 22, 2018. 1 changed file with 28 additions and 13 deletions.
    41 changes: 28 additions & 13 deletions types.js
    Original 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 Preson = t.objectOf({
    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]

    const Personal = t.arrayOf(Preson)
    Person(person1)

    const personal = [
    {id: 1, name: 'Alex'},
    {id: 1, name: 'Joh'}
    ]
    Personal(personal)

    const logPersonal = createFunc(Personal, t.bool)((p1, p2) => {
    console.log(p1, p2)
    })
    // new types

    logPersonal(personal, true)
    */
    const True = createType()
    const False = createType()
    const Bool = t.oneOfType([True, False])
  3. SergProduction revised this gist Feb 20, 2018. No changes.
  4. SergProduction revised this gist Feb 20, 2018. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions types.js
    Original 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)
    */
  5. SergProduction created this gist Feb 20, 2018.
    60 changes: 60 additions & 0 deletions types.js
    Original 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)
    */