-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathorder.js
More file actions
53 lines (35 loc) · 1.33 KB
/
order.js
File metadata and controls
53 lines (35 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
var SyncSpec = require('./../lib/bspec').SyncSpec;
var util = require('util');
// is expired
function OrderExpiredSpec() { }
util.inherits(OrderExpiredSpec, SyncSpec);
OrderExpiredSpec.prototype.isSatisfiedBy = function isSatisfiedBy(order) {
return (new Date() > order.date);
};
// is valid
function OrderValidSpec() { }
util.inherits(OrderValidSpec, SyncSpec);
OrderValidSpec.prototype.isSatisfiedBy = function isSatisfiedBy(order) {
return order && order.hasOwnProperty('number') && order.hasOwnProperty('date');
};
// is processed
var processed = {};
function OrderProcessed() { }
util.inherits(OrderProcessed, SyncSpec);
OrderProcessed.prototype.isSatisfiedBy = function isSatisfiedBy(order) {
return !!processed[order.number];
};
var isExpired = new OrderExpiredSpec();
var isValid = new OrderValidSpec();
var isProcessed = new OrderProcessed();
var spec = isValid.and(isExpired.not()).and(isProcessed.not());
var order = { number: 'Z10', date: new Date('2014-05-06') };
console.log('check that the order is `valid`, `not expired` and `not processed`:');
if(spec.isSatisfiedBy(order)) {
console.log(' - true');
} else {
console.log(' - false');
}
// inspect composite specification
console.log('explain composite rule:', spec.explain()); // ((ValidOrderSpec AND (NOT OverDueOrderSpec)) AND (NOT OrderProcessed))