/
usage.js
61 lines (47 loc) · 2.09 KB
/
usage.js
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
54
55
56
57
58
59
60
61
/*jslint indent: 2, maxlen: 80, node: true */
/* -*- tab-width: 2 -*- */
'use strict';
function readmeDemo(t) {
// #BEGIN# usage demo
var phtd = require('parse-human-timeout-duration'),
timeouts = { read: '0.2 sec' };
t.equal(phtd('2.5 minutes'), 150);
t.equal(phtd('2.5 minutes', { unit: 'ms' }), 150e3);
t.throws(phtd, [timeouts.read, { min: 1 }],
/Timespan must be at least 1 sec/);
t.throws(phtd, [timeouts.read, { min: 500, unit: 'ms' }],
/Timespan must be at least 500 ms/);
t.throws(phtd, ['1 year'], /^RangeError: Timespan too long/);
t.throws(phtd, ['0 sec'], /Timespan must be positive$/);
t.throws(phtd, ['0 sec', { optional: true }],
/Timespan must be positive, or "false" to disable/);
t.throws(phtd, [false], /Timespan required/);
t.equal(phtd(false, { optional: true }), false);
t.throws(phtd, ['false'], /Invalid duration: Found no number/);
t.throws(phtd, ['false', { optional: true }],
/Invalid duration: Found no number/);
t.throws(phtd, ['never'], /Timespan required/);
t.equal(phtd('never', { optional: true }), false);
t.equal(phtd('off', { optional: true }), false);
t.throws(phtd, ['0 ms', { descr: 'database timeout' }],
/RangeError: database timeout: Timespan must be positive/);
t.equal(phtd('read', { lookup: timeouts }), 0.2);
t.throws(phtd, ['write', { lookup: timeouts }],
/InvalidDuration: write: Invalid duration: Found no time unit/);
t.throws(phtd, ['write', { lookup: timeouts, descr: 'database config' }],
/InvalidDuration: database config \[write\]: Invalid/);
t.equal(phtd({ duration: '2.5 minutes' }), 150);
t.throws(phtd, [{ key: '2.5 minutes' }], /no time unit/);
t.equal(phtd({ key: 'read', lookup: timeouts }), 0.2);
t.throws(phtd, [{ duration: 'read', lookup: timeouts }], /no time unit/);
// #ENDOF# usage demo
}
(function () {
var ass = require('assert'), t = { equal: ass.strictEqual };
t.throws = function throws(f, a, e) {
function g() { f.apply(null, a); }
ass.throws(g, e);
};
readmeDemo(t);
}());
console.log("+OK usage demo test passed."); //= "+OK usage demo test passed."