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 - npm Package Compare versions

Comparing version 2.0.1 to 3.0.0

3

CHANGELOG.md
# CHANGELOG
## 3.0.0
- **BREAKING:** Removed undefined props from output
## 2.0.1

@@ -5,0 +8,0 @@ - Fix serialization bug serializing object with 0 units

51

dist/index.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const units = [
{ unit: 'years', symbol: 'Y' },
{ unit: 'months', symbol: 'M' },
{ unit: 'weeks', symbol: 'W' },
{ unit: 'days', symbol: 'D' },
{ unit: 'hours', symbol: 'H' },
{ unit: 'minutes', symbol: 'M' },
{ unit: 'seconds', symbol: 'S' },
];
// Construction of the duration regex
const r = (name, unit) => `((?<${name}>-?\\d*[\\.,]?\\d+)${unit})?`;
const durationRegex = new RegExp([
'(?<negativeStr>-)?P',
r('yearStr', 'Y'),
r('monthStr', 'M'),
r('weekStr', 'W'),
r('dayStr', 'D'),
'(?<negative>-)?P',
r('years', 'Y'),
r('months', 'M'),
r('weeks', 'W'),
r('days', 'D'),
'(T',
r('hourStr', 'H'),
r('minuteStr', 'M'),
r('secondStr', 'S'),
r('hours', 'H'),
r('minutes', 'M'),
r('seconds', 'S'),
')?',

@@ -29,16 +38,18 @@ ].join(''));

}
const { negativeStr, yearStr, monthStr, weekStr, hourStr, dayStr, minuteStr, secondStr } = match.groups;
if (!yearStr && !monthStr && !weekStr && !hourStr && !dayStr && !minuteStr && !secondStr) {
let empty = true;
const values = {};
for (const { unit } of units) {
if (match.groups[unit]) {
empty = false;
values[unit] = parseNum(match.groups[unit]);
}
}
if (empty) {
throw exports.InvalidDurationError;
}
return {
negative: negativeStr === '-' || undefined,
years: parseNum(yearStr),
months: parseNum(monthStr),
weeks: parseNum(weekStr),
days: parseNum(dayStr),
hours: parseNum(hourStr),
minutes: parseNum(minuteStr),
seconds: parseNum(secondStr),
};
const duration = values;
if (match.groups.negative) {
duration.negative = true;
}
return duration;
}

@@ -45,0 +56,0 @@ exports.parse = parse;

{
"name": "tinyduration",
"version": "2.0.1",
"version": "3.0.0",
"description": "ISO-8601 duration parsing and serialization",

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

@@ -29,10 +29,2 @@ import { parse, serialize, InvalidDurationError } from '.'

})
test('serialize empty object', () => {
expect(serialize({})).toEqual('PT0S')
})
test('serialize 0 units', () => {
expect(serialize({ years: 12, days: 0 })).toEqual('P12Y')
})
})

@@ -49,1 +41,17 @@

})
describe('parsing tests', () => {
test('do not set undefined units', () => {
expect(Object.keys(parse('PT0S'))).toEqual(['seconds'])
})
})
describe('serialzation tests', () => {
test('serialize empty object', () => {
expect(serialize({})).toEqual('PT0S')
})
test('serialize 0 units', () => {
expect(serialize({ years: 12, days: 0 })).toEqual('P12Y')
})
})

@@ -1,3 +0,2 @@

export interface Duration {
negative?: boolean
interface DurationValues {
years?: number

@@ -12,2 +11,16 @@ months?: number

export type Duration = {
negative?: boolean
} & DurationValues
const units: Array<{ unit: keyof DurationValues; symbol: string }> = [
{ unit: 'years', symbol: 'Y' },
{ unit: 'months', symbol: 'M' },
{ unit: 'weeks', symbol: 'W' },
{ unit: 'days', symbol: 'D' },
{ unit: 'hours', symbol: 'H' },
{ unit: 'minutes', symbol: 'M' },
{ unit: 'seconds', symbol: 'S' },
]
// Construction of the duration regex

@@ -17,11 +30,11 @@ const r = (name: string, unit: string): string => `((?<${name}>-?\\d*[\\.,]?\\d+)${unit})?`

[
'(?<negativeStr>-)?P',
r('yearStr', 'Y'),
r('monthStr', 'M'),
r('weekStr', 'W'),
r('dayStr', 'D'),
'(?<negative>-)?P',
r('years', 'Y'),
r('months', 'M'),
r('weeks', 'W'),
r('days', 'D'),
'(T',
r('hourStr', 'H'),
r('minuteStr', 'M'),
r('secondStr', 'S'),
r('hours', 'H'),
r('minutes', 'M'),
r('seconds', 'S'),
')?', // end optional time

@@ -47,17 +60,21 @@ ].join(''),

const { negativeStr, yearStr, monthStr, weekStr, hourStr, dayStr, minuteStr, secondStr } = match.groups
if (!yearStr && !monthStr && !weekStr && !hourStr && !dayStr && !minuteStr && !secondStr) {
let empty = true
const values: DurationValues = {}
for (const { unit } of units) {
if (match.groups[unit]) {
empty = false
values[unit] = parseNum(match.groups[unit])
}
}
if (empty) {
throw InvalidDurationError
}
return {
negative: negativeStr === '-' || undefined,
years: parseNum(yearStr),
months: parseNum(monthStr),
weeks: parseNum(weekStr),
days: parseNum(dayStr),
hours: parseNum(hourStr),
minutes: parseNum(minuteStr),
seconds: parseNum(secondStr),
const duration: Duration = values
if (match.groups.negative) {
duration.negative = true
}
return duration
}

@@ -64,0 +81,0 @@

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