Socket
Socket
Sign inDemoInstall

tinyduration

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tinyduration

ISO-8601 duration parsing and serialization


Version published
Weekly downloads
97K
increased by51.2%
Maintainers
1
Weekly downloads
 
Created
Source

TinyDuration

A small javascript package to parse and serialize ISO-8601 durations. This package does only 2 things:

  1. It parses a duration string to an object
    • (e.g. P1DT12H to { days: 1, hours: 12 })
  2. The reverse, i.e. serialize an object to a string.

Node.js CI

Installation

  • NPM: npm install --save tinyduration
  • Yarn: yarn add tinyduration

Usage

import { parse, toString } from 'tinyduration';

// Basic parsing
const durationObj = parse('P1Y2M3DT4H5M6S');
assert(durationObj, {
    years: 1,
    months: 2,
    days: 3,
    hours: 4,
    minutes: 5,
    seconds: 6
});

// Serialization
assert(toString(durationObj), 'P1Y2M3DT4H5M6S');

Development

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 tests
  • yarn lint to run the linter
  • yarn prettify to auto-fix the indenting issues
  • yarn ci to run coverage and linting

API

Type: Duration

PropertyTypeDescription
negative`booleanundefined`
years`numberundefined`
months`numberundefined`
weeks`numberundefined`
days`numberundefined`
hours`numberundefined`
minutes`numberundefined`
seconds`numberundefined`

Function: parse(string): Duration

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')
}

Function: toString(Duration): string

toString 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.toString({ weeks: 1 });
assert(durationStr, 'P1W')

const durationStr = Duration.toString({});
assert(durationStr, 'PT0S')

License

MIT

FAQs

Package last updated on 17 Mar 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc