Skip to content

parse() accepts malformed duration strings and applies leading "-" only to the first component #41

Open

Description

@trackoor

parse() returns numbers for malformed duration strings instead of throwing TypeError, and in negative compound durations like "-1h 30m" the leading - applies only to the first component.

Reproduction (repro.mjs):

import { parse } from "ms-tiny";

for (const s of ["1.2.3s", "1 2h", "1hh", "--1h", "-1h 30m"]) {
  console.log(`${JSON.stringify(s)} -> ${parse(s)}`);
}

Run with:

npm install [email protected] && node repro.mjs

Actual output:

"1.2.3s" -> 2300
"1 2h" -> 7200000
"1hh" -> 3600000
"--1h" -> -3600000
"-1h 30m" -> -1800000

Expected: "1.2.3s", "1 2h", "1hh", and "--1h" throw TypeError (matching the documented invalid-input behavior), and "-1h 30m" returns -5400000 (-90 minutes).

In src/index.ts, when SINGLE_RE does not match, PARSE_RE.exec(value) sums any <number><unit> substrings without checking that they cover the whole input, and evaluates signs per token rather than across the compound duration.

Versions: [email protected] and main (ae715e2d7d), Node.js v26.13.0.

Credit: https://ucsbplse.github.io/

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions