Skip to content

Instantly share code, notes, and snippets.

@ChrisPenner
Created August 23, 2017 14:32
Show Gist options
  • Select an option

  • Save ChrisPenner/11997de28e2e9adb6af31060a4828d66 to your computer and use it in GitHub Desktop.

Select an option

Save ChrisPenner/11997de28e2e9adb6af31060a4828d66 to your computer and use it in GitHub Desktop.

Revisions

  1. ChrisPenner created this gist Aug 23, 2017.
    25 changes: 25 additions & 0 deletions heyting-validation.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    const all = (...preds) => (obj) => preds.map(f => f(obj)).reduce((a, b) => a && b, true)
    const any = (...preds) => (obj) => preds.map(f => f(obj)).reduce((a, b) => a || b, false)
    const oneOf = (...preds) => (obj) => preds.map(f => f(obj)).reduce((a, b) => a ? !b : b, false)
    const has = (prop) => (obj) => obj[prop] !== undefined
    const not = (pred) => (obj) => !pred(obj)
    const equals = (prop, val) => (obj) => obj[prop] === val
    const implies = (f, g) => (obj) => !f(obj) || g(obj);

    const validate = all(implies(has('selectedIndex'), equals('isOpen', true)))

    validate({}) // true; passes all 'implications'
    validate({'selectedIndex': 3}) // false; isOpen !== true!
    validate({'selectedIndex': 3, isOpen: true}) // true; passes all implications


    // more complex example
    const complexValidate = all(
    implies(equals('mode', 'indexed'),
    all(
    has('selectedIndex'),
    obj => obj['selectedIndex'] <= 5
    )
    ),
    implies(equals('mode', 'disabled'), not(has('selectedIndex')))
    )