bspec is a small JavaScript library for structuring business rules.
A specification is a predicate that determines if an object does or does not satisfy some criteria. Business rules can be expressed as predicates and combined using operators such as "AND", "OR" and "NOT" to express more complex rules.
'use strict';
var bspec = require('bspec');
/**
* Constraint that only a customer who has specified a first given name can specify a second given name
*/
var hasFirstName = bspec.makeSync(function(customer) {
return !!(customer && customer.first_name);
});
var hasSecondName = bspec.makeSync(function(customer) {
return !!(customer && customer.second_name);
});
var customer1 = { first_name: 'Bob' };
var customer2 = { second_name: 'Pablo' };
var customer3 = { first_name: 'Juan', second_name: 'Pablo' };
var isCustomerNameValid = (hasSecondName.not()).or(hasFirstName);
console.log(isCustomerNameValid.isSatisfiedBy(customer1)); // true
console.log(isCustomerNameValid.isSatisfiedBy(customer2)); // false
console.log(isCustomerNameValid.isSatisfiedBy(customer3)); // true
installing with npm:
$ npm install bspec --save
To use bspec in browser, use the bspec.js
file in the /dist
directory of this repository, or build it manually. To build a fresh version:
$ npm install
$ gulp script
installing with bower:
$ bower install bspec
To run the tests for bspec:
$ npm test
[Grigoriy Chudnov] (mailto:[email protected])
Distributed under the The MIT License (MIT).