@devtanc/daterange
Advanced tools
Comparing version 1.0.4 to 1.1.0
130
index.js
@@ -19,2 +19,8 @@ const { | ||
subYears, | ||
addDays, | ||
addWeeks, | ||
addMonths, | ||
addQuarters, | ||
addYears, | ||
} = require('date-fns') | ||
@@ -41,2 +47,3 @@ | ||
subtract: subDays, | ||
add: addDays, | ||
}, | ||
@@ -47,2 +54,3 @@ week: { | ||
subtract: subWeeks, | ||
add: addWeeks, | ||
}, | ||
@@ -53,2 +61,3 @@ month: { | ||
subtract: subMonths, | ||
add: addMonths, | ||
}, | ||
@@ -59,2 +68,3 @@ quarter: { | ||
subtract: subQuarters, | ||
add: addQuarters, | ||
}, | ||
@@ -65,12 +75,83 @@ year: { | ||
subtract: subYears, | ||
add: addYears, | ||
}, | ||
} | ||
module.exports.getCalendarRange = (unit, shift = 0, date = new Date()) => { | ||
const now = new Date() | ||
now.setMilliseconds(0) | ||
const getCalendarRange = (unit, shift = 0, date = new Date()) => { | ||
const { | ||
now, | ||
beginning, | ||
ending, | ||
subtract, | ||
} = validateParamsAndRetrieveFunctions(date, shift, unit) | ||
// Function logic | ||
const shiftedDate = subtract(date, shift) | ||
const start = beginning(shiftedDate) | ||
const end = ending(shiftedDate) | ||
throwIfAfter(start, now) | ||
return { | ||
start, | ||
end: _capDate(end, now), | ||
} | ||
} | ||
const getCustomRangeEnding = (date, count, unit) => { | ||
const { now, subtract } = validateParamsAndRetrieveFunctions( | ||
date, | ||
count, | ||
unit, | ||
) | ||
const start = subtract(date, count) | ||
throwIfAfter(start, now) | ||
return { | ||
start, | ||
end: date, | ||
} | ||
} | ||
const getCustomRangeStarting = (date, count, unit) => { | ||
const { now, add } = validateParamsAndRetrieveFunctions(date, count, unit) | ||
throwIfAfter(toTheSecond(date), now) | ||
const end = add(date, count) | ||
return { | ||
start: date, | ||
end, | ||
} | ||
} | ||
function validateParamsAndRetrieveFunctions(date, integer, unit) { | ||
const now = toTheSecond(new Date()) | ||
checkIntegerOrString(integer) | ||
checkIsDate(date) | ||
return { now, ...getUnitFunctions(unit) } | ||
} | ||
function throwIfAfter(date1, date2) { | ||
if (date1.valueOf() > date2.valueOf()) { | ||
throw new RangeError( | ||
'Invalied range specified. Calculated range is in the future', | ||
) | ||
} | ||
} | ||
function _capDate(date, cap) { | ||
return date.valueOf() > cap.valueOf() ? cap : date | ||
} | ||
function toTheSecond(date) { | ||
date.setMilliseconds(0) | ||
return date | ||
} | ||
function getUnitFunctions(unit) { | ||
const mappedUnit = unitMap[unit] | ||
checkMappedUnit(mappedUnit) | ||
return functionsByUnit[mappedUnit] | ||
} | ||
// Param checks | ||
if (!mappedUnit) { | ||
function checkMappedUnit(unit) { | ||
if (!unit) { | ||
throw new TypeError( | ||
@@ -82,12 +163,18 @@ `The provided unit [${unit}] is invalid. Expected one of [${Object.keys( | ||
} | ||
if (!Number.isInteger(shift)) { | ||
const parsedValue = parseInt(shift, 10) | ||
} | ||
function checkIntegerOrString(value) { | ||
if (!Number.isInteger(value)) { | ||
const parsedValue = parseInt(value, 10) | ||
if (Number.isNaN(parsedValue)) { | ||
throw new TypeError( | ||
`Invalid value provided. Expected pareseable integer but saw ${typeof shift}`, | ||
`Invalid value provided. Expected pareseable integer but saw ${typeof value}`, | ||
) | ||
} else { | ||
shift = parsedValue | ||
value = parsedValue | ||
} | ||
} | ||
} | ||
function checkIsDate(date) { | ||
if (!(date instanceof Date)) { | ||
@@ -98,22 +185,9 @@ throw new TypeError( | ||
} | ||
} | ||
// Function logic | ||
const { beginning, ending, subtract } = functionsByUnit[mappedUnit] | ||
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', | ||
) | ||
} | ||
if (calculatedEnd.valueOf() > now.valueOf()) { | ||
calculatedEnd = now | ||
} | ||
return { | ||
start, | ||
end: calculatedEnd, | ||
} | ||
module.exports = { | ||
getCalendarRange, | ||
getCustomRangeEnding, | ||
getCustomRangeStarting, | ||
_capDate, | ||
} |
{ | ||
"name": "@devtanc/daterange", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "A date range generating library", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha", | ||
"test": "mocha --reporter=xunit --reporter-options output=test/results/mocha/results.xml", | ||
"precommit": "lint-staged", | ||
@@ -9,0 +9,0 @@ "lint": "xo", |
# daterange | ||
A library for generating date ranges as a simple wrapper around [date-fns](https://date-fns.org/) | ||
A library for generating past date ranges as a simple wrapper around [date-fns](https://date-fns.org/) | ||
@@ -23,3 +23,3 @@ ![npm (scoped)](https://img.shields.io/npm/v/@devtanc/daterange.svg) | ||
This function is for generating ranges that conform to calendar units like days, weeks, months, quarters, or years. The return object is in the form of: | ||
This function is for generating ranges in the past that conform to calendar units like days, weeks, months, quarters, or years. This library does not currently handle ranges in the future. The return object is in the form of: | ||
@@ -34,3 +34,7 @@ ```js | ||
```js | ||
import { getCalendarRange } from '@devtanc/daterange' | ||
import { | ||
getCalendarRange, | ||
getCustomRangeEnding, | ||
getCustomRangeStarting, | ||
} from '@devtanc/daterange' | ||
// Arguments are (unit, shift, date). Only the unit is required (plural or singular are treated the same) | ||
@@ -65,2 +69,16 @@ // A positive shift means going BACK in time (like 6 days ago), negative goes forward | ||
// } | ||
getCalendarRangeStarting(new Date(2018, 0, 1), 5, 'days') | ||
// A range starting on the given date and extending past it x number of units | ||
// { | ||
// start: new Date(2018, 0, 1), | ||
// end: new Date(2018, 0, 6), | ||
// } | ||
getCustomRangeEnding(new Date(2018, 0, 1), 5, 'days') | ||
// A range starting on the given date and extending past it x number of units | ||
// { | ||
// start: new Date(2017, 11, 27), | ||
// end: new Date(2018, 0, 1), | ||
// } | ||
``` |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
14598
301
82
1