UNPKG

style-helper

Version:

Small helpers for working with CSS-in-JS

96 lines 2.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ESCAPES = { '"': '\\"', '\\': '\\\\', '\n': '\\A' }; var ESCAPE_REGEXP = new RegExp("[" + Object.keys(ESCAPES).join('|') + "]", 'g'); /** * Escape a string for use in double quotes. */ function escape(value) { return value.replace(ESCAPE_REGEXP, function (x) { return ESCAPES[x]; }); } exports.escape = escape; /** * Quote a string for use in CSS. */ function quote(value) { return "\"" + escape(value) + "\""; } exports.quote = quote; /** * Wrap a string in a CSS `url()` function. */ function url(value) { return "url(" + quote(value) + ")"; } exports.url = url; /** * Turn a list of styles into an object. */ function objectify() { var styles = []; for (var _i = 0; _i < arguments.length; _i++) { styles[_i] = arguments[_i]; } var obj = Object.create(null); for (var _a = 0, styles_1 = styles; _a < styles_1.length; _a++) { var _b = styles_1[_a], key = _b[0], value = _b[1]; if (Array.isArray(key)) { for (var _c = 0, key_1 = key; _c < key_1.length; _c++) { var k = key_1[_c]; obj[k] = value; } } else { obj[key] = value; } } return obj; } exports.objectify = objectify; /** * Repeats the same style for multiple selectors. * * Reference: https://github.com/blakeembrey/free-style/issues/72. */ function multi(selectors, style) { var obj = Object.create(null); for (var _i = 0, selectors_1 = selectors; _i < selectors_1.length; _i++) { var selector = selectors_1[_i]; obj[selector] = style; } return obj; } exports.multi = multi; /** * Check if an object looks like a style. */ function isStyle(value) { return value !== null && typeof value === 'object' && !Array.isArray(value); } exports.isStyle = isStyle; function merge() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var result = Object.create(null); for (var _a = 0, args_1 = args; _a < args_1.length; _a++) { var style = args_1[_a]; for (var _b = 0, _c = Object.keys(style); _b < _c.length; _b++) { var key = _c[_b]; if (isStyle(result[key]) && isStyle(style[key])) { result[key] = merge(result[key], style[key]); } else { result[key] = style[key]; } } } return result; } exports.merge = merge; //# sourceMappingURL=index.js.map