-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
👋 Hi!
I was using this package for a personal project and I encountered the following bug:
Issue
If a value contains a decimal point but is larger than 1 the parsed unit becomes undefined.
How to reproduce
I wrote a jest test-case
const parseUnit = require("unit-parse");
test("can convert values with decimal point preceded by digit other than 0", () => {
expect(parseUnit("1.5s")).toEqual({
value: 1.5,
unit: "s",
});
});Output:
● can convert values with decimal point preceded by digit other than 0
expect(received).toEqual(expected) // deep equality
- Expected - 2
+ Received + 2
Object {
- "unit": "s",
- "value": 1.5,
+ "unit": undefined,
+ "value": "1.5s",
}Suggestion for a fix
I am no RegEx wizard but in my project I fixed the issue by changing by changing index.js
from:
// index.js
const match = value.match(/^(0?[-.]?\d+)(r?e[m|x]|v[h|w|min|max]+|p[x|t|c]|[c|m]m|%|s|in|ch)$/);to:
// index.js
const match = value.match(/^([0-9]\d*[-.]?\d*|[-.]?\d*)(r?e[m|x]|v[h|w|min|max]+|p[x|t|c]|[c|m]m|%|s|in|ch)$/);This will also cover the following test cases:
const parseUnit = require("unit-parse");
test("can convert values with decimal point preceded by multiple digits", () => {
expect(parseUnit("11.5s")).toEqual({
value: 11.5,
unit: "s",
});
});
test("can convert values with decimal where 0 is omitted", () => {
expect(parseUnit(".5s")).toEqual({
value: 0.5,
unit: "s",
});
});I can turn this into a PR if necessary.
Thanks for writing this package
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels