Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
tinyduration
Advanced tools
A small javascript package to parse and serialize ISO-8601 durations. This package does only 2 things:
P1DT12H
to { days: 1, hours: 12 }
)This lib has 0 dependencies.
npm install --save tinyduration
yarn add tinyduration
import { parse, serialize } from 'tinyduration';
// Basic parsing
const durationObj = parse('P1Y2M3DT4H5M6S');
assert(durationObj, {
years: 1,
months: 2,
days: 3,
hours: 4,
minutes: 5,
seconds: 6
});
// Serialization
assert(serialize(durationObj), 'P1Y2M3DT4H5M6S');
This library is written in TypeScript.
During publication of the package, the code is transpiled to javascript and put into the dist
folder.
The tests can be found the src
folder under *.test.ts
, testing is done using Jest
Additional commands you'll need for development:
yarn test
to run all testsyarn lint
to run the linteryarn prettify
to auto-fix the indenting issuesyarn ci
to run coverage and lintingProperty | Type | Description |
---|---|---|
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 |
parse
accepts a string and returns a Duration
object.
No attempt is made to change lower units into higher ones, e.g. to change 120 minutes into 2 hours.
Throws InvalidDurationError
if an invalid duration string is supplied.
import { parse } from 'tinyduraion';
const duration = parse('P1W');
assert(duration, { weeks: 1 })
try {
parse('invalid-duration');
} catch (e) {
assert(e.message === 'Invalid duration')
}
serialize
accepts a Duration object and returns a serialized duration according to ISO-8601.
If the duration is empty (i.e. all values are 0), PT0S
is returned.
import * as Duration from 'tinyduraion';
const durationStr = Duration.serialize({ weeks: 1 });
assert(durationStr, 'P1W')
const durationStr = Duration.serialize({});
assert(durationStr, 'PT0S')
MIT
3.2.1
FAQs
ISO-8601 duration parsing and serialization
The npm package tinyduration receives a total of 30,815 weekly downloads. As such, tinyduration popularity was classified as popular.
We found that tinyduration demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.