-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimespan.js
174 lines (161 loc) · 5.2 KB
/
timespan.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"use strict";
const assert = require("assert");
const ms = 1;
const s = 1000 * ms;
const m = 60 * s;
const h = 60 * m;
const d = 24 * h;
const w = 7 * d;
const M = 30.44 * d;
const y = 365.25 * d;
const UNITS = {
msec: ms,
ms,
seconds: s,
second: s,
sec: s,
"": s,
s,
minutes: m,
minute: m,
min: m,
m,
hours: h,
hour: h,
hr: h,
h,
days: d,
day: d,
d,
weeks: w,
week: w,
w,
months: M,
month: M,
M,
years: y,
year: y,
y
};
const DEFAULTS = {
abbv: true,
capitalize: false,
valueSep: " "
};
const longname = {
ms: "millisecond",
s: "second",
m: "minute",
h: "hour",
d: "day",
w: "week",
M: "month",
y: "year"
};
function init(initOpts = {}) {
const units = { ...UNITS };
if (typeof initOpts == "string") {
initOpts = { unit: initOpts };
}
assert.ok(initOpts != null && typeof initOpts == "object", `Invalid defaults, expected object but got ${typeof initOpts}`);
const defaults = {
...DEFAULTS,
...initOpts
};
if (defaults.unit) {
assert.strictEqual(typeof defaults.unit, "string", `Invalid unit, expected string but got ${typeof defaults.unit}`);
const val = units[defaults.unit];
assert.ok(val, `Unknown unit ${defaults.unit}`);
units[""] = val;
}
if (defaults.unitSep != null) {
assert.strictEqual(typeof defaults.unitSep, "string", `Invalid unitSep, expected string but got ${typeof defaults.unitSep}`);
}
assert.strictEqual(typeof defaults.valueSep, "string", `Invalid valueSep, expected string but got ${typeof defaults.valueSep}`);
return {
parse,
getString
};
function parse(timespan, unit) {
assert.ok(timespan, "Expected timespan");
if (typeof timespan === "number") {
timespan += "";
}
assert.strictEqual(typeof timespan, "string", `Invalid timespan, expected string but got ${typeof timespan}`);
if (unit == null) {
unit = "";
}
assert.strictEqual(typeof unit, "string", `Invalid unit, expected string but got ${typeof unit}`);
const divider = units[unit];
assert.ok(divider, `Unknown unit ${unit}`);
const tss = /^\s*-?(\d+\s*[a-z]*\s*)+$/.test(timespan) && timespan.match(/\d+\s*[a-z]*\s*/g);
assert.ok(tss, `Invalid format for timespan ${timespan}`);
const sign = timespan.trim().startsWith("-") ? -1 : 1;
const value = tss.reduce((sum, ts) => sum + getValue(ts), 0);
return sign * (value / divider);
}
function getValue(ts) {
const n = parseInt(ts, 10);
const suf = ts.replace(/[\d\s-]/g, "");
const mutliplier = units[suf];
assert.ok(mutliplier, `Invalid timespan, unknown unit ${suf}`);
return n * mutliplier;
}
function getString(value, opts = {}) {
if (typeof opts === "string") {
opts = { unit: opts };
}
if (opts == null) {
opts = {};
}
const {
unit = "",
abbv = defaults.abbv,
capitalize = defaults.capitalize,
unitSep = defaults.unitSep != null ? defaults.unitSep : abbv ? "" : " ",
valueSep = defaults.valueSep
} = opts;
if (typeof value === "string" && /^-?\d+(\.\d+)?$/.test(value)) {
value = +value;
}
assert.strictEqual(typeof value, "number", `Invalid value, expected number but got ${typeof value}`);
assert.ok(isFinite(value), `Invalid value ${value}`);
assert.strictEqual(typeof unit, "string", `Invalid unit, expected string but got ${typeof unit}`);
assert.ok(units[unit], `Unknown unit ${unit}`);
assert.strictEqual(typeof abbv, "boolean", `Invalid abbv, expected boolean but got ${typeof abbv}`);
assert.strictEqual(typeof capitalize, "boolean", `Invalid capitalize, expected boolean but got ${typeof capitalize}`);
assert.strictEqual(typeof unitSep, "string", `Invalid unitSep, expected string but got ${typeof unitSep}`);
assert.strictEqual(typeof valueSep, "string", `Invalid valueSep, expected string but got ${typeof valueSep}`);
const tss = [];
const sign = value < 0 ? "-" : "";
value = Math.abs(Math.round(value * units[unit]));
const writeValue = unitName => {
const unitVal = units[unitName];
if (value >= unitVal) {
tss.push(format(Math.floor(value / unitVal), unitName));
value %= unitVal;
}
};
writeValue("y");
writeValue("w");
writeValue("d");
writeValue("h");
writeValue("m");
writeValue("s");
writeValue("ms");
return sign + tss.join(valueSep);
function format(val, unitName) {
if (!abbv) {
unitName = longname[unitName];
if (val != 1) {
unitName += "s";
}
}
if (capitalize) {
unitName = unitName[0].toUpperCase() + unitName.slice(1);
}
return val + unitSep + unitName;
}
}
}
module.exports = Object.assign(init, init());