Comparing version 1.1.1 to 1.2.0
{ | ||
"name": "expired", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Calculate when HTTP responses expire from the cache headers", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -75,9 +75,24 @@ # expired | ||
## Pure Usage | ||
You can make the functions pure by passing in a JavaScript `Date` object to compare to instead of depending on `new Date()`. This isn't necessary for `expired.on` as it doesn't compare dates and is already pure. | ||
The following are all pure functions: | ||
```js | ||
const headers = `...`; | ||
const date = new Date(); | ||
expired(headers, date) | ||
expired.in(headers, date) | ||
expired.on(headers) | ||
``` | ||
## API | ||
### expired(headers) | ||
### expired(headers, [date]) | ||
Returns a boolean relating to whether the resource has expired or not. `true` means it's expired, `false` means it's fresh. | ||
### expired.in(headers) | ||
### expired.in(headers, [date]) | ||
@@ -84,0 +99,0 @@ Returns the amount of milliseconds from the current date until the resource will expire. If the resource has already expired it will return a negative integer. |
@@ -1,2 +0,2 @@ | ||
const isPast = require('date-fns/is_past'); | ||
const isBefore = require('date-fns/is_before'); | ||
const differenceInMilliseconds = require('date-fns/difference_in_milliseconds'); | ||
@@ -7,6 +7,6 @@ const addSeconds = require('date-fns/add_seconds'); | ||
// Returns boolean for whether or not the cache has expired | ||
const expired = headers => isPast(expired.on(headers)); | ||
const expired = (headers, date = new Date()) => isBefore(expired.on(headers), date); | ||
// Return ms until cache expires | ||
expired.in = headers => differenceInMilliseconds(expired.on(headers), new Date()); | ||
expired.in = (headers, date = new Date()) => differenceInMilliseconds(expired.on(headers), date); | ||
@@ -18,9 +18,7 @@ // Returns date when cache will expire | ||
let expiredOn = new Date(); | ||
// Default to Date header | ||
let expiredOn = new Date(headers.date); | ||
// Prefer Cache-Control | ||
if (headers['cache-control']) { | ||
// Date from headers | ||
const originDate = new Date(headers.date); | ||
// Get max age ms | ||
@@ -36,3 +34,3 @@ let maxAge = headers['cache-control'].match(/max-age=(\d+)/); | ||
// Calculate expirey date | ||
expiredOn = addSeconds(originDate, maxAge); | ||
expiredOn = addSeconds(expiredOn, maxAge); | ||
@@ -39,0 +37,0 @@ // Fall back to Expires if it exists |
@@ -54,1 +54,13 @@ import test from 'ava'; | ||
}); | ||
test('expired.in accepts currentDate argument', t => { | ||
const date = new Date(new Date().toUTCString()); | ||
const headers = { | ||
date: date.toUTCString(), | ||
age: 0, | ||
'cache-control': 'public, max-age=300' | ||
}; | ||
t.is(expired.in(headers, date), 300000); | ||
t.is(expired.in(headers, addSeconds(date, 500)), -200000); | ||
}); |
import test from 'ava'; | ||
import subSeconds from 'date-fns/sub_seconds'; | ||
import addSeconds from 'date-fns/add_seconds'; | ||
import expired from '../'; | ||
@@ -28,1 +29,13 @@ | ||
}); | ||
test('expired accepts currentDate argument', t => { | ||
const date = new Date(); | ||
const headers = { | ||
date: date.toUTCString(), | ||
age: 0, | ||
'cache-control': 'public, max-age=300' | ||
}; | ||
t.false(expired(headers, date)); | ||
t.true(expired(headers, addSeconds(date, 500))); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12418
219
107