tinyduration
Advanced tools
Comparing version 1.0.1 to 2.0.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// Construction of the duration regex | ||
var r = function (name, unit) { return "((?<" + name + ">-?\\d*[\\.,]?\\d+)" + unit + ")?"; }; | ||
var durationRegex = new RegExp([ | ||
const r = (name, unit) => `((?<${name}>-?\\d*[\\.,]?\\d+)${unit})?`; | ||
const durationRegex = new RegExp([ | ||
'(?<negativeStr>-)?P', | ||
@@ -25,7 +25,7 @@ r('yearStr', 'Y'), | ||
function parse(durationStr) { | ||
var match = durationRegex.exec(durationStr); | ||
const match = durationRegex.exec(durationStr); | ||
if (!match || !match.groups) { | ||
throw exports.InvalidDurationError; | ||
} | ||
var _a = match.groups, negativeStr = _a.negativeStr, yearStr = _a.yearStr, monthStr = _a.monthStr, weekStr = _a.weekStr, hourStr = _a.hourStr, dayStr = _a.dayStr, minuteStr = _a.minuteStr, secondStr = _a.secondStr; | ||
const { negativeStr, yearStr, monthStr, weekStr, hourStr, dayStr, minuteStr, secondStr } = match.groups; | ||
if (!yearStr && !monthStr && !weekStr && !hourStr && !dayStr && !minuteStr && !secondStr) { | ||
@@ -46,3 +46,3 @@ throw exports.InvalidDurationError; | ||
exports.parse = parse; | ||
function toString(duration) { | ||
function serialize(duration) { | ||
if (!duration.years && | ||
@@ -70,3 +70,3 @@ !duration.months && | ||
} | ||
exports.toString = toString; | ||
exports.serialize = serialize; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "tinyduration", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "ISO-8601 duration parsing and serialization", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -17,3 +17,3 @@ | ||
```js | ||
import { parse, toString } from 'tinyduration'; | ||
import { parse, serialize } from 'tinyduration'; | ||
@@ -32,3 +32,3 @@ // Basic parsing | ||
// Serialization | ||
assert(toString(durationObj), 'P1Y2M3DT4H5M6S'); | ||
assert(serialize(durationObj), 'P1Y2M3DT4H5M6S'); | ||
``` | ||
@@ -53,10 +53,10 @@ | ||
|-|-|-| | ||
|negative|`boolean|undefined`|Duration is positive if undefined | ||
|years|`number|undefined`|| | ||
|months|`number|undefined`|| | ||
|weeks|`number|undefined`|| | ||
|days|`number|undefined`|| | ||
|hours|`number|undefined`|| | ||
|minutes|`number|undefined`|| | ||
|seconds|`number|undefined`|| | ||
|negative|`boolean` or `undefined`|Duration is positive if undefined | ||
|years|`number` or `undefined`|| | ||
|months|`number` or `undefined`|| | ||
|weeks|`number` or `undefined`|| | ||
|days|`number` or `undefined`|| | ||
|hours|`number` or `undefined`|| | ||
|minutes|`number` or `undefined`|| | ||
|seconds|`number` or `undefined`|| | ||
@@ -84,4 +84,4 @@ | ||
## *Function:* toString(Duration): string | ||
`toString` accepts a Duration object and returns a serialized duration according to ISO-8601. | ||
## *Function:* serialize(Duration): string | ||
`serialize` accepts a Duration object and returns a serialized duration according to ISO-8601. | ||
@@ -93,6 +93,6 @@ If the duration is empty (i.e. all values are 0), `PT0S` is returned. | ||
const durationStr = Duration.toString({ weeks: 1 }); | ||
const durationStr = Duration.serialize({ weeks: 1 }); | ||
assert(durationStr, 'P1W') | ||
const durationStr = Duration.toString({}); | ||
const durationStr = Duration.serialize({}); | ||
assert(durationStr, 'PT0S') | ||
@@ -99,0 +99,0 @@ ``` |
@@ -1,2 +0,2 @@ | ||
import { parse, toString, InvalidDurationError } from '.' | ||
import { parse, serialize, InvalidDurationError } from '.' | ||
@@ -26,7 +26,7 @@ describe('valid test cases', () => { | ||
expect(parse(input)).toEqual(parsed) | ||
expect(toString(parsed)).toEqual(serialized || input) | ||
expect(serialize(parsed)).toEqual(serialized || input) | ||
}) | ||
test('serialize empty object', () => { | ||
expect(toString({})).toEqual('PT0S') | ||
expect(serialize({})).toEqual('PT0S') | ||
}) | ||
@@ -33,0 +33,0 @@ }) |
@@ -62,3 +62,3 @@ export interface Duration { | ||
export function toString(duration: Duration): string { | ||
export function serialize(duration: Duration): string { | ||
if ( | ||
@@ -65,0 +65,0 @@ !duration.years && |
@@ -5,3 +5,3 @@ { | ||
"incremental": true, | ||
"target": "es5", | ||
"target": "es6", | ||
"module": "commonjs", | ||
@@ -8,0 +8,0 @@ "strict": true, |
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
13697
12
253
1