-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from tolu/update-deps
Port to TypeScript
- Loading branch information
Showing
15 changed files
with
424 additions
and
445 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name: Run Tests | ||
on: [pull_request] | ||
jobs: | ||
Npm-Install-And-Test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 'lts/*' | ||
- run: npm install | ||
- run: npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lib |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @description A module for parsing ISO8601 durations | ||
*/ | ||
interface Duration { | ||
years?: number; | ||
months?: number; | ||
weeks?: number; | ||
days?: number; | ||
hours?: number; | ||
minutes?: number; | ||
seconds?: number; | ||
} | ||
/** | ||
* The ISO8601 regex for matching / testing durations | ||
*/ | ||
export declare const pattern: RegExp; | ||
/** Parse PnYnMnDTnHnMnS format to object */ | ||
export declare const parse: (durationString: string) => Duration; | ||
/** Convert ISO8601 duration object to an end Date. */ | ||
export declare const end: (durationInput: Duration, startDate?: Date) => Date; | ||
/** Convert ISO8601 duration object to seconds */ | ||
export declare const toSeconds: (durationInput: Duration, startDate?: Date) => number; | ||
declare const _default: { | ||
end: (durationInput: Duration, startDate?: Date) => Date; | ||
toSeconds: (durationInput: Duration, startDate?: Date) => number; | ||
pattern: RegExp; | ||
parse: (durationString: string) => Duration; | ||
}; | ||
export default _default; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,86 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
"use strict"; | ||
/** | ||
* @description A module for parsing ISO8601 durations | ||
*/ | ||
|
||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toSeconds = exports.end = exports.parse = exports.pattern = void 0; | ||
/** | ||
* The pattern used for parsing ISO8601 duration (PnYnMnDTnHnMnS). | ||
* This does not cover the week format PnW. | ||
*/ | ||
|
||
// PnYnMnDTnHnMnS | ||
var numbers = '\\d+(?:[\\.,]\\d+)?'; | ||
var weekPattern = '(' + numbers + 'W)'; | ||
var datePattern = '(' + numbers + 'Y)?(' + numbers + 'M)?(' + numbers + 'D)?'; | ||
var timePattern = 'T(' + numbers + 'H)?(' + numbers + 'M)?(' + numbers + 'S)?'; | ||
|
||
var iso8601 = 'P(?:' + weekPattern + '|' + datePattern + '(?:' + timePattern + ')?)'; | ||
var objMap = ['weeks', 'years', 'months', 'days', 'hours', 'minutes', 'seconds']; | ||
|
||
var numbers = "\\d+(?:[\\.,]\\d+)?"; | ||
var weekPattern = "(".concat(numbers, "W)"); | ||
var datePattern = "(".concat(numbers, "Y)?(").concat(numbers, "M)?(").concat(numbers, "D)?"); | ||
var timePattern = "T(".concat(numbers, "H)?(").concat(numbers, "M)?(").concat(numbers, "S)?"); | ||
var iso8601 = "P(?:".concat(weekPattern, "|").concat(datePattern, "(?:").concat(timePattern, ")?)"); | ||
var objMap = [ | ||
"weeks", | ||
"years", | ||
"months", | ||
"days", | ||
"hours", | ||
"minutes", | ||
"seconds", | ||
]; | ||
var defaultDuration = Object.freeze({ | ||
years: 0, | ||
months: 0, | ||
weeks: 0, | ||
days: 0, | ||
hours: 0, | ||
minutes: 0, | ||
seconds: 0 | ||
years: 0, | ||
months: 0, | ||
weeks: 0, | ||
days: 0, | ||
hours: 0, | ||
minutes: 0, | ||
seconds: 0, | ||
}); | ||
|
||
/** | ||
* The ISO8601 regex for matching / testing durations | ||
*/ | ||
var pattern = exports.pattern = new RegExp(iso8601); | ||
|
||
/** Parse PnYnMnDTnHnMnS format to object | ||
* @param {string} durationString - PnYnMnDTnHnMnS formatted string | ||
* @return {Object} - With a property for each part of the pattern | ||
*/ | ||
var parse = exports.parse = function parse(durationString) { | ||
// Slice away first entry in match-array | ||
return durationString.match(pattern).slice(1).reduce(function (prev, next, idx) { | ||
prev[objMap[idx]] = parseFloat(next) || 0; | ||
return prev; | ||
}, {}); | ||
exports.pattern = new RegExp(iso8601); | ||
/** Parse PnYnMnDTnHnMnS format to object */ | ||
var parse = function (durationString) { | ||
// Slice away first entry in match-array | ||
return durationString | ||
.match(exports.pattern) | ||
.slice(1) | ||
.reduce(function (prev, next, idx) { | ||
prev[objMap[idx]] = parseFloat(next) || 0; | ||
return prev; | ||
}, {}); | ||
}; | ||
|
||
/** | ||
* Convert ISO8601 duration object to an end Date. | ||
* | ||
* @param {Object} duration - The duration object | ||
* @param {Date} startDate - The starting Date for calculating the duration | ||
* @return {Date} - The resulting end Date | ||
*/ | ||
var end = exports.end = function end(duration, startDate) { | ||
duration = Object.assign({}, defaultDuration, duration); | ||
|
||
// Create two equal timestamps, add duration to 'then' and return time difference | ||
var timestamp = startDate ? startDate.getTime() : Date.now(); | ||
var then = new Date(timestamp); | ||
|
||
then.setFullYear(then.getFullYear() + duration.years); | ||
then.setMonth(then.getMonth() + duration.months); | ||
then.setDate(then.getDate() + duration.days); | ||
then.setHours(then.getHours() + duration.hours); | ||
then.setMinutes(then.getMinutes() + duration.minutes); | ||
// Then.setSeconds(then.getSeconds() + duration.seconds); | ||
then.setMilliseconds(then.getMilliseconds() + duration.seconds * 1000); | ||
// Special case weeks | ||
then.setDate(then.getDate() + duration.weeks * 7); | ||
|
||
return then; | ||
exports.parse = parse; | ||
/** Convert ISO8601 duration object to an end Date. */ | ||
var end = function (durationInput, startDate) { | ||
if (startDate === void 0) { startDate = new Date(); } | ||
var duration = Object.assign({}, defaultDuration, durationInput); | ||
// Create two equal timestamps, add duration to 'then' and return time difference | ||
var timestamp = startDate.getTime(); | ||
var then = new Date(timestamp); | ||
then.setFullYear(then.getFullYear() + duration.years); | ||
then.setMonth(then.getMonth() + duration.months); | ||
then.setDate(then.getDate() + duration.days); | ||
then.setHours(then.getHours() + duration.hours); | ||
then.setMinutes(then.getMinutes() + duration.minutes); | ||
// Then.setSeconds(then.getSeconds() + duration.seconds); | ||
then.setMilliseconds(then.getMilliseconds() + duration.seconds * 1000); | ||
// Special case weeks | ||
then.setDate(then.getDate() + duration.weeks * 7); | ||
return then; | ||
}; | ||
|
||
/** | ||
* Convert ISO8601 duration object to seconds | ||
* | ||
* @param {Object} duration - The duration object | ||
* @param {Date} startDate - The starting point for calculating the duration | ||
* @return {Number} | ||
*/ | ||
var toSeconds = exports.toSeconds = function toSeconds(duration, startDate) { | ||
duration = Object.assign({}, defaultDuration, duration); | ||
|
||
var timestamp = startDate ? startDate.getTime() : Date.now(); | ||
var now = new Date(timestamp); | ||
var then = end(duration, now); | ||
|
||
var seconds = (then.getTime() - now.getTime()) / 1000; | ||
return seconds; | ||
exports.end = end; | ||
/** Convert ISO8601 duration object to seconds */ | ||
var toSeconds = function (durationInput, startDate) { | ||
if (startDate === void 0) { startDate = new Date(); } | ||
var duration = Object.assign({}, defaultDuration, durationInput); | ||
var timestamp = startDate.getTime(); | ||
var now = new Date(timestamp); | ||
var then = (0, exports.end)(duration, now); | ||
var seconds = (then.getTime() - now.getTime()) / 1000; | ||
return seconds; | ||
}; | ||
|
||
exports.toSeconds = toSeconds; | ||
exports.default = { | ||
end: end, | ||
toSeconds: toSeconds, | ||
pattern: pattern, | ||
parse: parse | ||
}; | ||
end: exports.end, | ||
toSeconds: exports.toSeconds, | ||
pattern: exports.pattern, | ||
parse: exports.parse, | ||
}; |
Oops, something went wrong.