UNPKG

@platform/css

Version:

Helpers for working with inline CSS.

98 lines (97 loc) 2.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var common_1 = require("../common"); var isBlank = function (value) { if (value === undefined || value === null) { return true; } if (typeof value === 'string' && common_1.valueUtil.isBlank(value)) { return true; } if (Array.isArray(value) && common_1.valueUtil.compact(value).length === 0) { return true; } return false; }; exports.toEdges = function (input, options) { if (options === void 0) { options = {}; } if (isBlank(input)) { var defaultValue = options.defaultValue; if (defaultValue && !isBlank(defaultValue)) { input = defaultValue; } else { return {}; } } input = input || 0; if (!Array.isArray(input)) { input = input.toString().split(' '); } var edges = input .map(function (item) { return (typeof item === 'string' && item.endsWith('px') ? item.replace(/px$/, '') : item); }) .map(function (item) { return common_1.valueUtil.toNumber(item); }); var top; var right; var bottom; var left; var getEdge = function (index) { var edge = edges[index]; if (edge === null || edge === 'null' || edge === '') { return undefined; } return edge; }; switch (edges.length) { case 1: top = getEdge(0); bottom = getEdge(0); left = getEdge(0); right = getEdge(0); break; case 2: top = getEdge(0); bottom = getEdge(0); left = getEdge(1); right = getEdge(1); break; case 3: top = getEdge(0); left = getEdge(1); right = getEdge(1); bottom = getEdge(2); break; default: top = getEdge(0); right = getEdge(1); bottom = getEdge(2); left = getEdge(3); } if (top === undefined && right === undefined && bottom === undefined && left === undefined) { return {}; } return { top: top, right: right, bottom: bottom, left: left, }; }; function prefixEdges(prefix, edges) { return Object.keys(edges).reduce(function (acc, key) { var _a; var value = edges[key]; key = "" + prefix + key[0].toUpperCase() + key.substr(1); return tslib_1.__assign(tslib_1.__assign({}, acc), (_a = {}, _a[key] = value, _a)); }, {}); } exports.prefixEdges = prefixEdges; exports.toMargins = function (input, options) { if (options === void 0) { options = {}; } return prefixEdges('margin', exports.toEdges(input, options)); }; exports.toPadding = function (input, options) { if (options === void 0) { options = {}; } return prefixEdges('padding', exports.toEdges(input, options)); };