tinyduration
Advanced tools
Comparing version 3.1.1 to 3.2.0
# CHANGELOG | ||
## 3.2.0 | ||
- Introduce property based tests (Thanks @Djaler) | ||
- Bump versions of dev dependencies | ||
## 3.1.0 | ||
@@ -5,0 +9,0 @@ - Fix type generation, closes #1 (Thanks @paddlefish) |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.serialize = exports.parse = exports.InvalidDurationError = void 0; | ||
const units = [ | ||
@@ -56,3 +57,14 @@ { unit: 'years', symbol: 'Y' }, | ||
exports.parse = parse; | ||
const s = (n, s) => (n ? n + s : undefined); | ||
const s = (number, component) => { | ||
if (!number) { | ||
return undefined; | ||
} | ||
let numberAsString = number.toString(); | ||
const exponentIndex = numberAsString.indexOf('e'); | ||
if (exponentIndex > -1) { | ||
const magnitude = parseInt(numberAsString.slice(exponentIndex + 2), 10); | ||
numberAsString = number.toFixed(magnitude + exponentIndex - 2); | ||
} | ||
return numberAsString + component; | ||
}; | ||
function serialize(duration) { | ||
@@ -79,5 +91,7 @@ if (!duration.years && | ||
s(duration.seconds, 'S'), | ||
].join(''); | ||
] | ||
.filter(Boolean) | ||
.join(''); | ||
} | ||
exports.serialize = serialize; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "tinyduration", | ||
"version": "3.1.1", | ||
"version": "3.2.0", | ||
"description": "ISO-8601 duration parsing and serialization", | ||
@@ -8,3 +8,9 @@ "main": "dist/index.js", | ||
"author": "MelleB <npm@melleboersma.nl>", | ||
"keywords": ["iso-8601", "duration", "period", "date", "time"], | ||
"keywords": [ | ||
"iso-8601", | ||
"duration", | ||
"period", | ||
"date", | ||
"time" | ||
], | ||
"repository": { | ||
@@ -28,2 +34,3 @@ "type": "git", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"fast-check": "^2.4.0", | ||
"jest": "^25.1.0", | ||
@@ -46,4 +53,7 @@ "prettier": "^1.19.1", | ||
"coverageDirectory": "coverage", | ||
"testPathIgnorePatterns": ["<rootDir>/dist/", "<rootDir>/node_modules/"] | ||
"testPathIgnorePatterns": [ | ||
"<rootDir>/dist/", | ||
"<rootDir>/node_modules/" | ||
] | ||
} | ||
} |
@@ -1,3 +0,5 @@ | ||
import { parse, serialize, InvalidDurationError } from '.' | ||
import * as fc from 'fast-check' | ||
import { Duration, InvalidDurationError, parse, serialize } from '.' | ||
describe('valid test cases', () => { | ||
@@ -29,2 +31,28 @@ const testCasesValid = [ | ||
}) | ||
test('vice versa', () => { | ||
const durationPartArb = fc.option( | ||
fc.float().filter(value => value >= 0), | ||
{ nil: undefined }, | ||
) | ||
const durationArb: fc.Arbitrary<Duration> = fc.record({ | ||
negative: fc.option(fc.boolean(), { nil: undefined }), | ||
years: durationPartArb, | ||
months: durationPartArb, | ||
weeks: durationPartArb, | ||
days: durationPartArb, | ||
hours: durationPartArb, | ||
minutes: durationPartArb, | ||
seconds: durationPartArb, | ||
}) | ||
fc.assert( | ||
fc.property(durationArb, duration => { | ||
const iso8601 = serialize(duration) | ||
expect(serialize(parse(iso8601))).toEqual(iso8601) | ||
}), | ||
) | ||
}) | ||
}) | ||
@@ -31,0 +59,0 @@ |
@@ -79,3 +79,17 @@ interface DurationValues { | ||
const s = (n: number | undefined, s: string): string | undefined => (n ? n + s : undefined) | ||
const s = (number: number | undefined, component: string): string | undefined => { | ||
if (!number) { | ||
return undefined | ||
} | ||
let numberAsString = number.toString() | ||
const exponentIndex = numberAsString.indexOf('e') | ||
if (exponentIndex > -1) { | ||
const magnitude = parseInt(numberAsString.slice(exponentIndex + 2), 10) | ||
numberAsString = number.toFixed(magnitude + exponentIndex - 2) | ||
} | ||
return numberAsString + component | ||
} | ||
export function serialize(duration: Duration): string { | ||
@@ -105,3 +119,5 @@ if ( | ||
s(duration.seconds, 'S'), | ||
].join('') | ||
] | ||
.filter(Boolean) | ||
.join('') | ||
} |
Sorry, the diff of this file is not supported yet
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
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
355
74634
13