diff --git a/@vates/parse-duration/index.js b/@vates/parse-duration/index.js index 66783a9def..4d534d93aa 100644 --- a/@vates/parse-duration/index.js +++ b/@vates/parse-duration/index.js @@ -6,9 +6,11 @@ exports.parseDuration = value => { if (typeof value === 'number') { return value } - const duration = ms(value) - if (duration === undefined) { - throw new TypeError(`not a valid duration: ${value}`) + if (typeof value === 'string' && value !== '') { + const duration = ms(value) + if (duration !== undefined) { + return duration + } } - return duration + throw new TypeError(`not a valid duration: ${value}`) } diff --git a/@vates/parse-duration/index.test.mjs b/@vates/parse-duration/index.test.mjs new file mode 100644 index 0000000000..dd873fd937 --- /dev/null +++ b/@vates/parse-duration/index.test.mjs @@ -0,0 +1,22 @@ +import { describe, it } from 'node:test' +import { parseDuration } from '@vates/parse-duration' +import assert from 'node:assert/strict' + +describe('parseDuration()', () => { + it('should parse string', () => { + const input = '2 days' + const expected = 172800000 + assert.strictEqual(parseDuration(input), expected) + }) + + it('should return its input if already a number', () => { + const input = 172800000 + assert.strictEqual(parseDuration(input), input) + }) + + for (const input of [undefined, '', 'invalid duration']) { + it('should throw an error for ' + input, () => { + assert.throws(() => parseDuration(input), { message: `not a valid duration: ${input}` }) + }) + } +}) diff --git a/@vates/parse-duration/package.json b/@vates/parse-duration/package.json index ebe0062a76..4dd15ead04 100644 --- a/@vates/parse-duration/package.json +++ b/@vates/parse-duration/package.json @@ -27,6 +27,7 @@ "ms": "^2.1.2" }, "scripts": { - "postversion": "npm publish --access public" + "postversion": "npm publish --access public", + "test": "node --test" } }