wilderness-core
Version:
The SVG animation engine behind Wilderness
76 lines (63 loc) • 1.83 kB
JavaScript
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
/**
* A tweenable unit.
*
* @typedef {Object} Unit
*
* @property {string} middleware - The name of this middleware.
* @property {string} values - The type of color string to output.
*/
var name = 'unit';
var units = ['ch', 'cm', 'em', 'ex', 'in', 'mm', 'pc', 'pt', 'px', 'rem', 'vh', 'vmax', 'vmin', 'vw', '%'];
/**
* Converts a unit string to a Unit.
*
* @param {*} x - A potential unit string.
*
* @returns {*}
*
* @example
* input('20px')
*/
var input = function input(x) {
if (typeof x === 'string') {
var parts = x.split(' ');
var values = [];
for (var i = 0, l = parts.length; i < l; i++) {
var part = parts[i];
var number = parseFloat(part);
var unit = part.replace(number, '');
if (!isNaN(number) && (unit === '' || units.indexOf(unit) !== -1)) {
values.push([number, unit]);
} else {
values.push(part);
}
}
if (values.toString() !== parts.toString()) {
return { middleware: name, values: values };
}
}
return x;
};
/**
* Converts a Unit to a unit string.
*
* @param {*} x - A potential Unit.
*
* @returns {*}
*
* @example
* output(unit)
*/
var output = function output(x) {
if ((typeof x === 'undefined' ? 'undefined' : _typeof(x)) === 'object' && x.middleware === name) {
var values = x.values;
var result = [];
for (var i = 0, l = values.length; i < l; i++) {
result.push(values[i].join(''));
}
return result.join(' ');
}
return x;
};
export default { name: name, input: input, output: output };