Skip to content

Instantly share code, notes, and snippets.

@gjohnson
Created November 18, 2012 22:47
Show Gist options
  • Save gjohnson/4107930 to your computer and use it in GitHub Desktop.
Save gjohnson/4107930 to your computer and use it in GitHub Desktop.
constraints
var constraint = require('constraint');
var assert = require('assert');
// create engine
var engine = constraint()
.contains('whatever', 'red')
.equals('some.thing.really.nested', 'works');
// need a custom rule?
function gt(a, b){
return a > b;
}
engine.rule(gt, 'age', 21);
// object to test
var mock = {
age: 26,
whatever: 'redventures'
some: {
thing: {
really: {
nested: 'works'
}
}
}
};
// any of the rules match
var a = engine.any(mock);
// all of the rules match
var b = engine.all(mock);
// results
assert(a, true);
assert(b, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment