d3plus-common
Version:
Common functions and methods used across D3plus modules.
15 lines • 707 B
JavaScript
/**
@function parseSides
@desc Converts a string of directional CSS shorthand values into an object with the values expanded.
@param {String|Number} sides The CSS shorthand string to expand.
*/
export default function (sides) {
var values;
if (typeof sides === "number") values = [sides];else values = sides.split(/\s+/);
if (values.length === 1) values = [values[0], values[0], values[0], values[0]];else if (values.length === 2) values = values.concat(values);else if (values.length === 3) values.push(values[1]);
return ["top", "right", "bottom", "left"].reduce(function (acc, direction, i) {
var value = parseFloat(values[i]);
acc[direction] = value || 0;
return acc;
}, {});
}