Skip to content

Instantly share code, notes, and snippets.

@ealush
ealush / enforce-proxy-benchmark.js
Created February 22, 2018 08:37
enforce proxy: benchmark test case
import { enforce } from 'passable';
enforce();
enforce(1);
enforce(2).isNumber();
enforce([]).allOf({
isArray: true,
isEmpty: true
});
@ealush
ealush / proxy-getter.js
Created February 21, 2018 11:48
Simple proxy getter example
// Le'ts start with this object
const orig = {
a: 'get',
b: 'moving'
};
// Now let's write a handler that serves as a getter for our object.
const handler = {
// by setting the `get` function we say that we intend to
// intercept `get` interactions with the object
@ealush
ealush / enforce-custom-rules.js
Last active February 21, 2018 11:20
enforce: custom rules
const enforce = new Enforce({
hasDog: (value) => /dog/.test(value)
});
enforce('dog').hasDog();
// Proxy {isArray: ƒ, largerThan: ƒ, smallerThan: ƒ, isEmpty: ƒ, hasDog: ƒ}
enforce('cat').hasDog();
// Uncaught Error: hasDog: YOU SHALL NOT PASS!
@ealush
ealush / test-proxy-approach.js
Created February 21, 2018 10:39
enforce: test proxy approach
const enforce = new Enforce();
enforce(55);
// Proxy {isArray: ƒ, largerThan: ƒ, smallerThan: ƒ, isEmpty: ƒ}
enforce(55).largerThan(20);
// Proxy {isArray: ƒ, largerThan: ƒ, smallerThan: ƒ, isEmpty: ƒ}
enforce(55).largerThan(200);
// Uncaught Error: largerThan: YOU SHALL NOT PASS!
@ealush
ealush / rules.js
Last active March 11, 2018 07:16
enforce: rules-file-example
const rules = {
isArray: (value) => Array.isArray(value),
largerThan: (value, compare) => value > compare,
smallerThan: (value, compare) => value < compare,
isEmpty: (value) => !value || Object.keys(value).length === 0
};
// export default rules
@ealush
ealush / test-its-christmas.js
Last active February 21, 2018 10:32
Test the `it's christmas approach`
enforce()
// {isArray: ƒ, largerThan: ƒ, smallerThan: ƒ, isEmpty: ƒ}
enforce(55).smallerThan(100);
// {isArray: ƒ, largerThan: ƒ, smallerThan: ƒ, isEmpty: ƒ}
enforce(55).smallerThan(100).largerThan(56);
// Uncaught Error: validation failed for largerThan
enforce('passable').isArray();
// 1: accepts a value
enforce(1)
// 2: returns an object
{
isNumber: // func,
largerThan: // func,
...
}
@ealush
ealush / its-christmas-approach.js
Last active March 4, 2018 12:23
enforce: it's christmas approach
// import rules from './rules.js';
// This is a `rule runner` It runs the rule it gets
// * throws an error on failure
// * returns the wrapped rules object on success so you can keep on chaining
function ruleRunner(rule, wrapped, value, ...args) {
const result = rule(value, ...args);
// if the result is not explicitly true, throw
if (result !== true) {
@ealush
ealush / passable-example.js
Last active February 21, 2018 08:58
passable-example
import passable, { enforce } from passable;
passable('new_user_form', (test) => {
test('username', 'Must be between 5 and 20 chars', () => {
enforce(username).allOf({
largerThan: 4,
smallerThan: 20
});
});
@ealush
ealush / naive-enforce.js
Last active March 4, 2018 12:21
naive-enforce
// import rules from './rules';
class Enforce {
constructor(custom = {}) {
this.rules = Object.assign({}, rules, custom);
return this.enforce;
}
enforce = (value) => {
this.value = value;