easy-css-transform-builder
Version:
Easily build CSS transform values with JavaScript.
39 lines (38 loc) • 1.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var invariant = require("invariant");
var properties_1 = require("./properties");
var defaultUnits = {
length: 'px',
angle: 'deg',
};
var isArray = function (v) { return Array.isArray(v); };
var hasProp = function (o, p) { return o.hasOwnProperty(p); };
var isValidProp = function (value) { return (typeof value === 'number' ||
typeof value === 'string' ||
isArray(value)); };
var createUnit = function (units, unitType) { return (unitType === properties_1.UnitTypes.NONE ? '' : units[unitType]); };
var createValue = function (value, unit) { return (typeof value === 'number' ? "" + value + unit : value); };
var normalizeValue = function (prop, value, units) {
if (hasProp(prop, 'units')) {
if (typeof value === 'string') {
return value;
}
var unitTypes = prop.units;
invariant(isArray(value), "Should be " + prop.name + " is a array");
invariant(value.length === unitTypes.length, "Should be " + prop.name + " is " + unitTypes.length + " values.");
return unitTypes
.map(function (unit, i) { return createValue(value[i], createUnit(units, unit)); })
.join(', ');
}
return createValue(value, createUnit(units, prop.unit));
};
exports.createCSSTransformBuilder = function (units) {
if (units === void 0) { units = defaultUnits; }
return (function (styles) { return (properties_1.transformProperties
.map(function (prop) { return (!hasProp(styles, prop.name) || !isValidProp(styles[prop.name])
? null
: prop.name + "(" + normalizeValue(prop, styles[prop.name], units) + ")"); })
.filter(function (value) { return value !== null; })
.join(' ')); });
};