react-toolbox/components/utils/prefixer.js

45 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2015-09-05 21:16:15 +03:00
const WEBKIT = 'Webkit';
const MICROSOFT = 'Ms';
const properties = {
2017-01-26 20:05:32 +03:00
transform: [WEBKIT, MICROSOFT],
2015-09-05 21:16:15 +03:00
};
2017-01-26 20:05:32 +03:00
function capitalize(string) {
2015-09-05 21:16:15 +03:00
return string.charAt(0).toUpperCase() + string.substr(1);
}
2017-01-26 20:05:32 +03:00
function getPrefixes(property, value) {
return properties[property].reduce((acc, item) => {
acc[`${item}${capitalize(property)}`] = value; // eslint-disable-line no-param-reassign
2015-09-05 21:16:15 +03:00
return acc;
}, {});
}
2017-01-26 20:05:32 +03:00
function addPrefixesTo(style, property, value) {
const vendor = getPrefixes(property, value);
2017-01-26 20:05:32 +03:00
for (const prefix in vendor) { // eslint-disable-line no-restricted-syntax
if ({}.hasOwnProperty.call(vendor, prefix)) {
style[prefix] = vendor[prefix]; // eslint-disable-line no-param-reassign
}
}
2015-09-05 21:16:15 +03:00
return style;
}
2017-01-26 20:05:32 +03:00
function prefixer(style, defaultValue = {}) {
2015-11-21 14:23:24 +03:00
const _style = defaultValue;
2017-01-26 20:05:32 +03:00
for (const property in style) { // eslint-disable-line no-restricted-syntax
if ({}.hasOwnProperty.call(style, property)) {
_style[property] = style[property];
if (properties[property]) {
addPrefixesTo(_style, property, style[property]);
}
2015-09-05 21:16:15 +03:00
}
}
return _style;
}
export default prefixer;