UNPKG

1.76 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.cssValue = exports.parseLengthAndUnit = void 0;
4var cssUnit = {
5 cm: true,
6 mm: true,
7 in: true,
8 px: true,
9 pt: true,
10 pc: true,
11 em: true,
12 ex: true,
13 ch: true,
14 rem: true,
15 vw: true,
16 vh: true,
17 vmin: true,
18 vmax: true,
19 "%": true
20};
21/**
22 * If size is a number, append px to the value as default unit.
23 * If size is a string, validate against list of valid units.
24 * If unit is valid, return size as is.
25 * If unit is invalid, console warn issue, replace with px as the unit.
26 *
27 * @param {(number | string)} size
28 * @return {LengthObject} LengthObject
29 */
30function parseLengthAndUnit(size) {
31 if (typeof size === "number") {
32 return {
33 value: size,
34 unit: "px"
35 };
36 }
37 var value;
38 var valueString = (size.match(/^[0-9.]*/) || "").toString();
39 if (valueString.includes(".")) {
40 value = parseFloat(valueString);
41 }
42 else {
43 value = parseInt(valueString, 10);
44 }
45 var unit = (size.match(/[^0-9]*$/) || "").toString();
46 if (cssUnit[unit]) {
47 return {
48 value: value,
49 unit: unit
50 };
51 }
52 console.warn("React Spinners: " + size + " is not a valid css value. Defaulting to " + value + "px.");
53 return {
54 value: value,
55 unit: "px"
56 };
57}
58exports.parseLengthAndUnit = parseLengthAndUnit;
59/**
60 * Take value as an input and return valid css value
61 *
62 * @param {(number | string)} value
63 * @return {string} valid css value
64 */
65function cssValue(value) {
66 var lengthWithunit = parseLengthAndUnit(value);
67 return "" + lengthWithunit.value + lengthWithunit.unit;
68}
69exports.cssValue = cssValue;