@devtanc/daterange
Advanced tools
Comparing version 1.0.2 to 1.0.3
26
index.js
@@ -70,8 +70,14 @@ const { | ||
if (!mappedUnit) { | ||
throw new TypeError(`The provided unit [${unit}] is invalid. Expected one of [${Object.keys(unitMap).join(',')}]`) | ||
throw new TypeError( | ||
`The provided unit [${unit}] is invalid. Expected one of [${Object.keys( | ||
unitMap, | ||
).join(',')}]`, | ||
) | ||
} | ||
if (!Number.isInteger(shift)) { | ||
parsedValue = parseInt(shift, 10) | ||
const parsedValue = parseInt(shift, 10) | ||
if (Number.isNaN(parsedValue)) { | ||
throw new TypeError(`Invalid value provided. Expected pareseable integer but saw ${ typeof shift }`) | ||
throw new TypeError( | ||
`Invalid value provided. Expected pareseable integer but saw ${typeof shift}`, | ||
) | ||
} else { | ||
@@ -82,3 +88,5 @@ shift = parsedValue | ||
if (!(date instanceof Date)) { | ||
throw new TypeError('Invalid type provided for end. Expected (end instanceof Date) to be true') | ||
throw new TypeError( | ||
'Invalid type provided for end. Expected (end instanceof Date) to be true', | ||
) | ||
} | ||
@@ -88,8 +96,10 @@ | ||
const { beginning, ending, subtract } = functionsByUnit[mappedUnit] | ||
let shiftedDate = subtract(date, shift) | ||
let start = beginning(shiftedDate) | ||
const shiftedDate = subtract(date, shift) | ||
const start = beginning(shiftedDate) | ||
let calculatedEnd = ending(shiftedDate) | ||
if (start.valueOf() > now.valueOf()) { | ||
throw new RangeError('Invalied range specified. Calculated range is in the future') | ||
throw new RangeError( | ||
'Invalied range specified. Calculated range is in the future', | ||
) | ||
} | ||
@@ -102,4 +112,4 @@ if (calculatedEnd.valueOf() > now.valueOf()) { | ||
start, | ||
end: calculatedEnd | ||
end: calculatedEnd, | ||
} | ||
} |
{ | ||
"name": "@devtanc/daterange", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A date range generating library", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha", | ||
"precommit": "lint-staged", | ||
"lint": "xo", | ||
"lint:fix": "npm run lint -- --fix" | ||
}, | ||
"keywords": [ | ||
"date", | ||
"date-fns" | ||
], | ||
"keywords": ["date", "date-fns"], | ||
"lint-staged": { | ||
"*.js": ["npm run lint:fix --", "git add"], | ||
"*.{js,css,md}": ["prettier --write", "git add"], | ||
"*.json": ["prettier --write --print-width=120", "git add"] | ||
}, | ||
"xo": { | ||
"prettier": true, | ||
"env": ["node", "mocha"], | ||
"plugins": ["node", "unicorn"] | ||
}, | ||
"author": "devtanc", | ||
@@ -23,4 +33,8 @@ "license": "MIT", | ||
"devDependencies": { | ||
"mocha": "^5.2.0" | ||
"husky": "^0.14.3", | ||
"lint-staged": "^7.1.2", | ||
"mocha": "^5.2.0", | ||
"prettier": "^1.12.1", | ||
"xo": "^0.21.1" | ||
} | ||
} |
59
test.js
@@ -1,11 +0,12 @@ | ||
const assert = require('assert'); | ||
const assert = require('assert') | ||
const { startOfDay } = require('date-fns') | ||
const { getCalendarRange, unitMap } = require('./') | ||
const { getCalendarRange } = require('.') | ||
const units = [ | ||
[ 'day', 'days' ], | ||
[ 'week', 'weeks' ], | ||
[ 'month', 'months' ], | ||
[ 'quarter', 'quarters' ], | ||
[ 'year', 'years' ], | ||
['day', 'days'], | ||
['week', 'weeks'], | ||
['month', 'months'], | ||
['quarter', 'quarters'], | ||
['year', 'years'], | ||
] | ||
@@ -18,5 +19,5 @@ | ||
for (let [ single, plural ] of units) { | ||
let rangeSingle = getCalendarRange(single, 0, now) | ||
let rangePlural = getCalendarRange(plural, 0, now) | ||
for (const [single, plural] of units) { | ||
const rangeSingle = getCalendarRange(single, 0, now) | ||
const rangePlural = getCalendarRange(plural, 0, now) | ||
@@ -47,6 +48,6 @@ assert.deepEqual(rangeSingle, rangePlural) | ||
const range = getCalendarRange('day', 0) | ||
assert.deepEqual(range, { | ||
start: startOfDay(now), | ||
end: now | ||
end: now, | ||
}) | ||
@@ -60,3 +61,3 @@ }) | ||
const range = getCalendarRange('day', 0, date) | ||
assert.deepEqual(range, { | ||
@@ -67,7 +68,7 @@ start: new Date(2018, 0, 2), | ||
}) | ||
it('should give the correct date range (beginning to end of unit) when shift is a number in string form', () => { | ||
const date = new Date(2018, 0, 2, 7, 23, 64, 765) | ||
const range = getCalendarRange('day', '0', date) | ||
assert.deepEqual(range, { | ||
@@ -78,7 +79,7 @@ start: new Date(2018, 0, 2), | ||
}) | ||
it('should give the correct date range (beginning to end of unit) when shift > 0', () => { | ||
const date = new Date(2018, 0, 2, 7, 23, 64, 765) | ||
const range = getCalendarRange('day', 1, date) | ||
assert.deepEqual(range, { | ||
@@ -95,3 +96,7 @@ start: new Date(2018, 0, 1), | ||
const unit = 'yargle' | ||
assert.throws(() => getCalendarRange(unit, 0, new Date()), TypeError, /The provided unit \[yargle\] is invalid.*/) | ||
assert.throws( | ||
() => getCalendarRange(unit, 0, new Date()), | ||
TypeError, | ||
/The provided unit \[yargle\] is invalid.*/, | ||
) | ||
}) | ||
@@ -101,13 +106,25 @@ | ||
const shift = 'invalid' | ||
assert.throws(() => getCalendarRange('day', shift, new Date()), TypeError, /Invalid value provided. Expected pareseable integer but saw string/) | ||
assert.throws( | ||
() => getCalendarRange('day', shift, new Date()), | ||
TypeError, | ||
/Invalid value provided. Expected pareseable integer but saw string/, | ||
) | ||
}) | ||
it('should throw when invalid end type is provided', () => { | ||
assert.throws(() => getCalendarRange('day', 0, 'not a date'), TypeError, /Invalid type provided for end. Expected \(end instanceof Date\) to be true/) | ||
assert.throws( | ||
() => getCalendarRange('day', 0, 'not a date'), | ||
TypeError, | ||
/Invalid type provided for end. Expected \(end instanceof Date\) to be true/, | ||
) | ||
}) | ||
it('should throw when date range start is in the future', () => { | ||
assert.throws(() => getCalendarRange('day', -3, new Date()), RangeError, /Invalied range specified. Calculated range is in the future/) | ||
assert.throws( | ||
() => getCalendarRange('day', -3, new Date()), | ||
RangeError, | ||
/Invalied range specified. Calculated range is in the future/, | ||
) | ||
}) | ||
}) | ||
}) |
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
11196
7
205
5