-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbarrier-sync.js
More file actions
44 lines (34 loc) · 1.25 KB
/
barrier-sync.js
File metadata and controls
44 lines (34 loc) · 1.25 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
'use strict';
/**
* An entry barrier opens only if the ticket meets all of the following criteria:
*
* 1. it is valid for travel from that station;
* 2. it has not expired;
* 3. it has not already been used for the maximum number of journeys allowed.
*/
var Spec = require('./../lib/bspec').SyncSpec;
var TODAY = new Date(2015, 2, 1);
var isTicketExpired = function isTicketExpired(ticket) {
return (TODAY > ticket.expiresAt);
};
var isMaxJourneys = function isMaxJourneys(ticket) {
return (ticket.cur_journeys >= ticket.max_journeys);
};
var isValidFromStation = function isValidFromStation(name, ticket) {
return (ticket.stations.indexOf(name) !== -1);
};
function makeBarrierSpec(stationName) {
return Spec(isValidFromStation.bind(null, stationName))
.and(Spec(isTicketExpired).not())
.and(Spec(isMaxJourneys).not());
}
var lowangenBarrier = makeBarrierSpec('Lowangen');
var rivaBarrier = makeBarrierSpec('Riva');
var ticket = {
stations: [ 'Lowangen' ],
expiresAt: new Date(2015, 2, 6),
max_journeys: 30,
cur_journeys: 11
};
console.log('The ticket can be used to enter the Lowangen station:', lowangenBarrier.isSatisfiedBy(ticket));
console.log('The ticket can be used to enter the Riva station:', rivaBarrier.isSatisfiedBy(ticket));