Created
August 23, 2017 14:32
-
-
Save ChrisPenner/11997de28e2e9adb6af31060a4828d66 to your computer and use it in GitHub Desktop.
Revisions
-
ChrisPenner created this gist
Aug 23, 2017 .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,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'))) )