Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dhms

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dhms - npm Package Compare versions

Comparing version 0.0.1 to 1.0.0

17

index.js

@@ -1,8 +0,9 @@

module.exports = str => (str.replace(/\s/g, '').match(/\d+(\D+|$)/g) || [])
.map(v => +v.replace(/\D+/g, '') * ({
s: 1000,
m: 60000,
h: 3600000,
d: 86400000,
}[v.replace(/\d+/g, '')] || 1))
.reduce((a, b) => a + b, 0);
module.exports = (str, sec = false) => {
const x = sec ? 1 : 1000;
if(typeof str !== 'string') return 0;
const fixed = str.replace(/\s/g, '');
const tail = +fixed.match(/-?\d+$/g) || 0;
const parts = (fixed.match(/-?\d+[^-0-9]+/g) || [])
.map(v => +v.replace(/[^-0-9]+/g, '') * ({s: x, m: 60 * x, h: 3600 * x, d: 86400 * x}[v.replace(/[-0-9]+/g, '')] || 0));
return [tail, ...parts].reduce((a, b) => a + b, 0);
};
{
"name": "dhms",
"version": "0.0.1",
"version": "1.0.0",
"description": "Parses `dhms` string to number of milliseconds",

@@ -5,0 +5,0 @@ "main": "index.js",

# dhms
Parses `dhms` string to number of milliseconds
Parses `dhms` string to number of milliseconds or seconds

@@ -18,3 +18,56 @@ [![Build Status][travis-image]][travis-url]

const dhms = require('dhms');
// milliseconds
dhms('123'); // 123
dhms('1s'); // 1000
dhms('1m'); // 60000
dhms('1h'); // 3600000
dhms('1d'); // 86400000
dhms('1d2h30m45s123'); // 95445123
dhms('2h 30 m4 5s12 3') === dhms('2h30m45s123'); // true
dhms('1h1h1h') === dhms('3h'); // true
dhms('-123'); // -123
dhms('-1h'); // -3600000
dhms('1h-30m') === dhms('30m'); // true
dhms('1s-400') === dhms('600'); // true
// seconds
dhms('123', true); // 123
dhms('1s', true); // 1
dhms('1m', true); // 60
dhms('1h', true); // 3600
dhms('1d', true); // 86400
dhms('1d2h30m45s123', true); // 95568
dhms('2h 30 m4 5s12 3', true) === dhms('2h30m45s123', true); // true
dhms('1h1h1h', true) === dhms('3h', true)); // true
dhms('-123', true); // -123
dhms('-1h', true); // -3600
dhms('1h-30m', true) === dhms('30m', true); // true
dhms('1m-40', true) === dhms('20', true); // true
// zero
dhms('000'); // 0
dhms('bad'); // 0
dhms('dhms'); // 0
dhms('123x'); // 0
dhms(''); // 0
dhms(true); // 0
dhms(false); // 0
dhms(null); // 0
dhms(); // 0
dhms(0); // 0
dhms(123); // 0
dhms([123]); // 0
dhms('000', true); // 0
dhms('bad', true); // 0
dhms('dhms', true); // 0
dhms('123x', true); // 0
dhms('', true); // 0
dhms(true, true); // 0
dhms(false, true); // 0
dhms(null, true); // 0
dhms(undefined, true); // 0
dhms(0, true); // 0
dhms(123, true); // 0
dhms([123], true); // 0
```

@@ -21,0 +74,0 @@

@@ -5,8 +5,32 @@ const test = require('ava');

test('zero', t => {
// ms
t.is(dhms('000'), 0);
t.is(dhms('bad'), 0);
t.is(dhms('dhms'), 0);
t.is(dhms('123x'), 0);
t.is(dhms(''), 0);
t.is(dhms(true), 0);
t.is(dhms(false), 0);
t.is(dhms(null), 0);
t.is(dhms(), 0);
t.is(dhms(0), 0);
t.is(dhms(123), 0);
t.is(dhms([123]), 0);
// sec
t.is(dhms('000', true), 0);
t.is(dhms('bad', true), 0);
t.is(dhms('dhms', true), 0);
t.is(dhms('123x', true), 0);
t.is(dhms('', true), 0);
t.is(dhms(true, true), 0);
t.is(dhms(false, true), 0);
t.is(dhms(null, true), 0);
t.is(dhms(undefined, true), 0);
t.is(dhms(0, true), 0);
t.is(dhms(123, true), 0);
t.is(dhms([123], true), 0);
});
test('base', t => {
// ms
t.is(dhms('123'), 123);

@@ -17,6 +41,32 @@ t.is(dhms('1s'), 1000);

t.is(dhms('1d'), 86400000);
// sec
t.is(dhms('123', true), 123);
t.is(dhms('1s', true), 1);
t.is(dhms('1m', true), 60);
t.is(dhms('1h', true), 3600);
t.is(dhms('1d', true), 86400);
});
test('complicated', t => {
// ms
t.is(dhms('1d2h30m45s123'), 95445123);
t.is(dhms('2h 30 m4 5s12 3'), dhms('2h30m45s123'));
t.is(dhms('1h1h1h'), dhms('3h'));
// sec
t.is(dhms('1d2h30m45s123', true), 95568);
t.is(dhms('2h 30 m4 5s12 3', true), dhms('2h30m45s123', true));
t.is(dhms('1h1h1h', true), dhms('3h', true));
});
test('negative', t => {
// ms
t.is(dhms('-123'), -123);
t.is(dhms('-1h'), -3600000);
t.is(dhms('1h-30m'), dhms('30m'));
t.is(dhms('1s-400'), dhms('600'));
// sec
t.is(dhms('-123', true), -123);
t.is(dhms('-1h', true), -3600);
t.is(dhms('1h-30m', true), dhms('30m', true));
t.is(dhms('1m-40', true), dhms('20', true));
});
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