mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-25 09:55:15 +00:00
70 lines
1.7 KiB
JavaScript
70 lines
1.7 KiB
JavaScript
|
"use strict";
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.cssValue = exports.parseLengthAndUnit = void 0;
|
||
|
var cssUnit = {
|
||
|
cm: true,
|
||
|
mm: true,
|
||
|
in: true,
|
||
|
px: true,
|
||
|
pt: true,
|
||
|
pc: true,
|
||
|
em: true,
|
||
|
ex: true,
|
||
|
ch: true,
|
||
|
rem: true,
|
||
|
vw: true,
|
||
|
vh: true,
|
||
|
vmin: true,
|
||
|
vmax: true,
|
||
|
"%": true
|
||
|
};
|
||
|
/**
|
||
|
* If size is a number, append px to the value as default unit.
|
||
|
* If size is a string, validate against list of valid units.
|
||
|
* If unit is valid, return size as is.
|
||
|
* If unit is invalid, console warn issue, replace with px as the unit.
|
||
|
*
|
||
|
* @param {(number | string)} size
|
||
|
* @return {LengthObject} LengthObject
|
||
|
*/
|
||
|
function parseLengthAndUnit(size) {
|
||
|
if (typeof size === "number") {
|
||
|
return {
|
||
|
value: size,
|
||
|
unit: "px"
|
||
|
};
|
||
|
}
|
||
|
var value;
|
||
|
var valueString = (size.match(/^[0-9.]*/) || "").toString();
|
||
|
if (valueString.includes(".")) {
|
||
|
value = parseFloat(valueString);
|
||
|
}
|
||
|
else {
|
||
|
value = parseInt(valueString, 10);
|
||
|
}
|
||
|
var unit = (size.match(/[^0-9]*$/) || "").toString();
|
||
|
if (cssUnit[unit]) {
|
||
|
return {
|
||
|
value: value,
|
||
|
unit: unit
|
||
|
};
|
||
|
}
|
||
|
console.warn("React Spinners: " + size + " is not a valid css value. Defaulting to " + value + "px.");
|
||
|
return {
|
||
|
value: value,
|
||
|
unit: "px"
|
||
|
};
|
||
|
}
|
||
|
exports.parseLengthAndUnit = parseLengthAndUnit;
|
||
|
/**
|
||
|
* Take value as an input and return valid css value
|
||
|
*
|
||
|
* @param {(number | string)} value
|
||
|
* @return {string} valid css value
|
||
|
*/
|
||
|
function cssValue(value) {
|
||
|
var lengthWithunit = parseLengthAndUnit(value);
|
||
|
return "" + lengthWithunit.value + lengthWithunit.unit;
|
||
|
}
|
||
|
exports.cssValue = cssValue;
|