Skip to content

Commit

Permalink
fix: Config expiresIn can contain periods. i.e, 1.5 weeks
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmit committed Jun 9, 2018
1 parent 9193d1a commit e9c7aaa
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 50 deletions.
47 changes: 47 additions & 0 deletions packages/@pollyjs/adapter/src/utils/dehumanize-time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const ALPHA_NUMERIC_DOT = /([0-9.]+)([a-zA-Z]+)/g;
const TIMES = {
ms: 1,
millisecond: 1,
milliseconds: 1,
s: 1000,
sec: 1000,
secs: 1000,
second: 1000,
seconds: 1000,
m: 60000,
min: 60000,
mins: 60000,
minute: 60000,
minutes: 60000,
h: 3600000,
hr: 3600000,
hrs: 3600000,
hour: 3600000,
hours: 3600000,
d: 86400000,
day: 86400000,
days: 86400000,
w: 604800000,
wk: 604800000,
wks: 604800000,
week: 604800000,
weeks: 604800000,
y: 31536000000,
yr: 31536000000,
yrs: 31536000000,
year: 31536000000,
years: 31536000000
};

export default function dehumanizeTime(input) {
if (typeof input !== 'string') {
return NaN;
}

const parts = input.replace(/ /g, '').match(ALPHA_NUMERIC_DOT);
const sets = parts.map(part => part.split(ALPHA_NUMERIC_DOT).filter(o => o));

return sets.reduce((accum, [number, unit]) => {
return accum + parseFloat(number) * TIMES[unit];
}, 0);
}
41 changes: 0 additions & 41 deletions packages/@pollyjs/adapter/src/utils/human-time-to-ms.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/@pollyjs/adapter/src/utils/is-expired.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import calculateTime from './human-time-to-ms';
import dehumanizeTime from './dehumanize-time';

export default function isExpired(recordedOn, expiresIn) {
if (recordedOn && expiresIn) {
return (
new Date() >
new Date(new Date(recordedOn).getTime() + calculateTime(expiresIn))
new Date(new Date(recordedOn).getTime() + dehumanizeTime(expiresIn))
);
}

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

describe('Unit | Utils | humanTimeToMilliseconds', function() {
describe('Unit | Utils | dehumanizeTime', function() {
it('should exist', function() {
expect(humanTimeToMilliseconds).to.be.a('function');
expect(dehumanizeTime).to.be.a('function');
});

it('should work', function() {
expect(dehumanizeTime(null)).to.be.NaN;
expect(dehumanizeTime(undefined)).to.be.NaN;
expect(dehumanizeTime(true)).to.be.NaN;
expect(dehumanizeTime(false)).to.be.NaN;

[
[[undefined, null, ''], 0],
[['1ms', '1millisecond', '1 milliseconds'], 1],
[['10ms', '10millisecond', '10 milliseconds'], 10],
[['100ms', '100millisecond', '100 milliseconds'], 100],
[['1s', '1sec', '1secs', '1 second', '1 seconds'], 1000],
[['1.5s', '1.5sec', '1.5secs', '1.5 second', '1.5 seconds'], 1500],
[['1m', '1min', '1mins', '1 minute', '1 minutes'], 1000 * 60],
[['1h', '1hr', '1hrs', '1 hour', '1 hours'], 1000 * 60 * 60],
[['1d', '1day', '1 days'], 1000 * 60 * 60 * 24],
Expand All @@ -23,9 +30,7 @@ describe('Unit | Utils | humanTimeToMilliseconds', function() {
33005045006
]
].forEach(([inputs, value]) => {
inputs.forEach(str =>
expect(humanTimeToMilliseconds(str)).to.equal(value)
);
inputs.forEach(str => expect(dehumanizeTime(str)).to.equal(value));
});
});
});

0 comments on commit e9c7aaa

Please sign in to comment.