Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 49 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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') {
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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]];
});
}
10 changes: 10 additions & 0 deletions test/04.compact-object.js
Original file line number Diff line number Diff line change
@@ -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');
});
});