Skip to content

Instantly share code, notes, and snippets.

@xemasiv
Last active April 13, 2018 11:23
Show Gist options
  • Save xemasiv/634b9ec605fd5801ebaad4ecfb69a402 to your computer and use it in GitHub Desktop.
Save xemasiv/634b9ec605fd5801ebaad4ecfb69a402 to your computer and use it in GitHub Desktop.
Checker.js - truthy, falsey & equality comparisons simplified.

Checker.js

  • Waterfall checks for:
    • truthiness
    • falseyness
    • equality
  • Perks
    • built-in argument iteration support for .t() and .f()
    • built-in pairwise iteration support for .eq() and ineq
    • uses fast-deep equal for .eq() and ineq()
    • .can().be().chained()
    • just call .check() at the end
    • Returns the result of function / promise

Sample 1:

Executes given functions

new Checker()
  .t(1, true)
  .f(0, false)
  .eq(1,1,1,1)
  .ineq(1,2)
  .pass(() => {
    console.log('pass');
  })
  .fail(() => {
  	console.log('fail');
  })
  .check();

// pass

Sample 2:

Returns a promise, makes it chainable with then.

new Checker()
  .t(1, true)
  .f(0, false)
  .eq(1,1,1)
  .ineq(1,2)
  .pass(() => Promise.resolve('pass'))
  .fail(() => Promise.reject('fail'))
  .check()
  .then(() => {
  	console.log('passed!');
  })
  .catch(() => {
  	console.error('failed!');
  });

// passed!
class Checker{
constructor () {
this._passed = true;
}
static pairwise (list) {
// https://codereview.stackexchange.com/a/75667
let pairs = new Array((list.length * (list.length - 1)) / 2);
let pos = 0;
for (let i = 0; i < list.length; i++) {
for (let j = i + 1; j < list.length; j++) {
pairs[pos++] = [list[i], list[j]];
}
}
return pairs;
}
static equal (a, b) {
// https://github.com/epoberezkin/fast-deep-equal
let isArray = Array.isArray;
let keyList = Object.keys;
let hasProp = Object.prototype.hasOwnProperty;
if (a === b) return true;
let arrA = isArray(a), arrB = isArray(b), i, length, key;
if (arrA && arrB) {
length = a.length;
if (length != b.length) return false;
for (i = 0; i < length; i++)
if (!equal(a[i], b[i])) return false;
return true;
}
if (arrA != arrB) return false;
let dateA = a instanceof Date, dateB = b instanceof Date;
if (dateA != dateB) return false;
if (dateA && dateB) return a.getTime() == b.getTime();
let regexpA = a instanceof RegExp, regexpB = b instanceof RegExp;
if (regexpA != regexpB) return false;
if (regexpA && regexpB) return a.toString() == b.toString();
if (a instanceof Object && b instanceof Object) {
let keys = keyList(a);
length = keys.length;
if (length !== keyList(b).length)
return false;
for (i = 0; i < length; i++)
if (!hasProp.call(b, keys[i])) return false;
for (i = 0; i < length; i++) {
key = keys[i];
if (!equal(a[key], b[key])) return false;
}
return true;
}
return false;
}
t (...args) {
let instance = this;
args.map((arg) => {
if (Boolean(arg) === false) {
instance._passed = false;
}
});
return this;
}
f (...args) {
let instance = this;
args.map((arg) => {
if (Boolean(arg) === true) {
instance._passed = false;
}
});
return this;
}
eq (...args) {
let instance = this;
let pairs = Checker.pairwise(args);
pairs.map((pair) => {
if (Checker.equal.apply(null, pair) === false) {
instance._passed = false;
}
});
return this;
}
ineq (...args) {
let instance = this;
let pairs = Checker.pairwise(args);
pairs.map((pair) => {
if (Checker.equal.apply(null, pair) === true) {
instance._passed = false;
}
});
return this;
}
pass (fn) {
this._passFn = fn;
return this;
}
fail (fn) {
this._failFn = fn;
return this;
}
check () {
if (this._passed === true) {
return this._passFn();
} else {
return this._failFn();
}
}
}
@xemasiv
Copy link
Author

xemasiv commented Apr 13, 2018

To add:

xi() for parseInt()
xf() for parseFloat()
xs() for toString()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment