diff --git a/lib/index.js b/lib/index.js index 782781f..9b12854 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,9 +1,23 @@ -var ASSERT = require('assert'); var list = require('./list'); -var props = require('css-shorthand-properties').shorthandProperties; +var shorthandProps = require('css-shorthand-properties').shorthandProperties; exports.isShorthand = isShorthand; exports.expand = expand; +exports.isCompactable = isCompactable; +exports.compact = compact; + +var invertedShorthandProps = Object.keys(shorthandProps) + .reduce(function(inverted, shorthand) { + expandAsArray(shorthand, false).forEach(function(key) { + inverted[key] = shorthand; + }); + return inverted; + }, {}) + +var errors = { + noPropertyArg: 'property argument is required', + propsArentCompactable: 'properties aren\'t compactable' +} /** * Expand a property to an array of parts or property and value to object @@ -14,7 +28,7 @@ exports.expand = expand; * @returns {Array|Object} */ function expand(property, value, recurse) { - ASSERT(arguments.length, 'property argument is required'); + if (!arguments.length) throw new Error(erros.noPropertyArg); if (arguments.length < 3) { if (typeof value === 'boolean') { @@ -33,11 +47,11 @@ function expand(property, value, recurse) { } function expandAsArray(property, recurse) { - if (!props.hasOwnProperty(property)) { + if (!shorthandProps.hasOwnProperty(property)) { return [property]; } - return props[property] + return shorthandProps[property] .map(function (p) { var longhand = p.substr(0, 1) === '-' ? property + p : p; return recurse ? expand(longhand, recurse) : longhand; @@ -57,7 +71,7 @@ function expandAsArray(property, recurse) { */ function expandAsObject(property, value, recurse) { var res = {}; - if (!props.hasOwnProperty(property)) { + if (!shorthandProps.hasOwnProperty(property)) { res[property] = value; return res; } @@ -84,9 +98,37 @@ function expandAsObject(property, value, recurse) { return res; } +function compact(longhand) { + if (longhand.length) return compactProperties(longhand) +} + +/** + * compactProperties — returns shorthand property from array of longhands. + * + * @param string[] props — longhand props + * @returns string — shorthand property + */ +function compactProperties(props) { + if (!isCompactable(props)) throw new Error(erros.propsArentCompactable); + return invertedShorthandProps[props[0]]; +} + function isShorthand(property) { - if (props.hasOwnProperty(property)) { + if (shorthandProps.hasOwnProperty(property)) { return true; } return false; } + +/** + * isCompactable — check if array of longhands could be compacted + * + * @param string[] longhands — longhand props + * @returns Boolean + */ +function isCompactable(longhands) { + return longhands.every(function(prop) { + return invertedShorthandProps.hasOwnProperty(prop) && + invertedShorthandProps[prop] === invertedShorthandProps[longhands[0]]; + }); +} diff --git a/test/04.compact-object.js b/test/04.compact-object.js new file mode 100644 index 0000000..fb20957 --- /dev/null +++ b/test/04.compact-object.js @@ -0,0 +1,10 @@ +describe('compact object', function () { + it('should compact an Array of props', function () { + SC.compact([ + 'border-top-left-radius', + 'border-top-right-radius', + 'border-bottom-right-radius', + 'border-bottom-left-radius' + ]).should.eql('border-radius'); + }); +});