Skip to content

Commit 5944862

Browse files
committed
update parse value util and test
1 parent cf1869a commit 5944862

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

__tests__/parseValueAndUnit.test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ test('Parse a string to get the value and unit in either pixels or percent', ()
44
expect(parseValueAndUnit('5px')).toEqual({ unit: 'px', value: 5 });
55
expect(parseValueAndUnit('52%')).toEqual({ unit: '%', value: 52 });
66
expect(parseValueAndUnit(13.333)).toEqual({ unit: '%', value: 13.333 });
7-
expect(parseValueAndUnit('75.8%')).toEqual({ unit: '%', value: 75 });
8-
expect(parseValueAndUnit('23.1px')).toEqual({ unit: 'px', value: 23 });
7+
expect(parseValueAndUnit('75.8%')).toEqual({ unit: '%', value: 75.8 });
8+
expect(parseValueAndUnit('23.1px')).toEqual({ unit: 'px', value: 23.1 });
99
expect(() => parseValueAndUnit(false)).toThrow();
10+
expect(() => parseValueAndUnit(() => {})).toThrow();
11+
expect(() => parseValueAndUnit({ foo: 'bar' })).toThrow();
12+
expect(() => parseValueAndUnit('100%%')).toThrow();
13+
expect(() => parseValueAndUnit('100px%')).toThrow();
14+
expect(() => parseValueAndUnit('100vh')).toThrow();
1015
});

src/utils/parseValueAndUnit.js

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
11
/**
22
* Determines the unit of a string and parses the value
33
*
4-
* @param {string} value
4+
* @param {string} str
5+
* @param {object} out
56
* @return {object} The parsed value and the unit if any
67
*/
7-
export default function parseValueAndUnit(value) {
8-
const isBool = typeof value === 'boolean';
9-
const isObject = typeof value === 'object';
10-
const isString = typeof value === 'string';
11-
const isNumb = typeof value === 'number';
8+
export default function parseValueAndUnit(str, out = { value: 0, unit: 'px' }) {
9+
const isValid = typeof str === 'number' || typeof str === 'string';
1210

13-
if (isBool || isObject) {
11+
if (!isValid) {
1412
throw new Error(
15-
'Ivalid value provided. Must provide a value as a string with % or px units.'
13+
'Invalid value provided. Must provide a value as a string or number'
1614
);
1715
}
1816

19-
if (isNumb) {
20-
return {
21-
value,
22-
unit: '%', // defaults to percent if not unit is passed
23-
};
24-
} else if (isString && value.slice(-1) === '%') {
25-
// remove % then parse
26-
value = parseInt(value.slice(0, -1), 10);
17+
str = String(str);
18+
out.value = parseFloat(str, 10);
19+
out.unit = str.match(/[\d.\-\+]*\s*(.*)/)[1] || '%'; // default to percent
2720

28-
return {
29-
value,
30-
unit: '%',
31-
};
32-
} else if (isString && value.slice(-2) === 'px') {
33-
// remove px then parse
34-
value = parseInt(value.slice(0, -2), 10);
21+
const validUnits = ['px', '%'];
22+
const isValidUnit = validUnits.find(unit => unit === out.unit);
3523

36-
return {
37-
value,
38-
unit: 'px',
39-
};
24+
if (!isValidUnit) {
25+
throw new Error(
26+
'Invalid unit provided. Must provide a unit of px in or %'
27+
);
4028
}
29+
30+
return out;
4131
}

0 commit comments

Comments
 (0)