Skip to content

Commit e9c7aaa

Browse files
committed
fix: Config expiresIn can contain periods. i.e, 1.5 weeks
1 parent 9193d1a commit e9c7aaa

File tree

4 files changed

+61
-50
lines changed

4 files changed

+61
-50
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const ALPHA_NUMERIC_DOT = /([0-9.]+)([a-zA-Z]+)/g;
2+
const TIMES = {
3+
ms: 1,
4+
millisecond: 1,
5+
milliseconds: 1,
6+
s: 1000,
7+
sec: 1000,
8+
secs: 1000,
9+
second: 1000,
10+
seconds: 1000,
11+
m: 60000,
12+
min: 60000,
13+
mins: 60000,
14+
minute: 60000,
15+
minutes: 60000,
16+
h: 3600000,
17+
hr: 3600000,
18+
hrs: 3600000,
19+
hour: 3600000,
20+
hours: 3600000,
21+
d: 86400000,
22+
day: 86400000,
23+
days: 86400000,
24+
w: 604800000,
25+
wk: 604800000,
26+
wks: 604800000,
27+
week: 604800000,
28+
weeks: 604800000,
29+
y: 31536000000,
30+
yr: 31536000000,
31+
yrs: 31536000000,
32+
year: 31536000000,
33+
years: 31536000000
34+
};
35+
36+
export default function dehumanizeTime(input) {
37+
if (typeof input !== 'string') {
38+
return NaN;
39+
}
40+
41+
const parts = input.replace(/ /g, '').match(ALPHA_NUMERIC_DOT);
42+
const sets = parts.map(part => part.split(ALPHA_NUMERIC_DOT).filter(o => o));
43+
44+
return sets.reduce((accum, [number, unit]) => {
45+
return accum + parseFloat(number) * TIMES[unit];
46+
}, 0);
47+
}

packages/@pollyjs/adapter/src/utils/human-time-to-ms.js

-41
This file was deleted.

packages/@pollyjs/adapter/src/utils/is-expired.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import calculateTime from './human-time-to-ms';
1+
import dehumanizeTime from './dehumanize-time';
22

33
export default function isExpired(recordedOn, expiresIn) {
44
if (recordedOn && expiresIn) {
55
return (
66
new Date() >
7-
new Date(new Date(recordedOn).getTime() + calculateTime(expiresIn))
7+
new Date(new Date(recordedOn).getTime() + dehumanizeTime(expiresIn))
88
);
99
}
1010

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
import humanTimeToMilliseconds from '../../../src/utils/human-time-to-ms';
1+
import dehumanizeTime from '../../../src/utils/dehumanize-time';
22

3-
describe('Unit | Utils | humanTimeToMilliseconds', function() {
3+
describe('Unit | Utils | dehumanizeTime', function() {
44
it('should exist', function() {
5-
expect(humanTimeToMilliseconds).to.be.a('function');
5+
expect(dehumanizeTime).to.be.a('function');
66
});
77

88
it('should work', function() {
9+
expect(dehumanizeTime(null)).to.be.NaN;
10+
expect(dehumanizeTime(undefined)).to.be.NaN;
11+
expect(dehumanizeTime(true)).to.be.NaN;
12+
expect(dehumanizeTime(false)).to.be.NaN;
13+
914
[
10-
[[undefined, null, ''], 0],
1115
[['1ms', '1millisecond', '1 milliseconds'], 1],
16+
[['10ms', '10millisecond', '10 milliseconds'], 10],
17+
[['100ms', '100millisecond', '100 milliseconds'], 100],
1218
[['1s', '1sec', '1secs', '1 second', '1 seconds'], 1000],
19+
[['1.5s', '1.5sec', '1.5secs', '1.5 second', '1.5 seconds'], 1500],
1320
[['1m', '1min', '1mins', '1 minute', '1 minutes'], 1000 * 60],
1421
[['1h', '1hr', '1hrs', '1 hour', '1 hours'], 1000 * 60 * 60],
1522
[['1d', '1day', '1 days'], 1000 * 60 * 60 * 24],
@@ -23,9 +30,7 @@ describe('Unit | Utils | humanTimeToMilliseconds', function() {
2330
33005045006
2431
]
2532
].forEach(([inputs, value]) => {
26-
inputs.forEach(str =>
27-
expect(humanTimeToMilliseconds(str)).to.equal(value)
28-
);
33+
inputs.forEach(str => expect(dehumanizeTime(str)).to.equal(value));
2934
});
3035
});
3136
});

0 commit comments

Comments
 (0)