sort-object
Advanced tools
Comparing version 0.0.5 to 0.0.6
{ | ||
"name": "sort-object", | ||
"description": "Sort the keys in an object", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"homepage": "https://github.com/helpers/sort-object", | ||
@@ -24,2 +24,5 @@ "author": { | ||
"main": "sort-object.js", | ||
"scripts": { | ||
"test": "mocha --reporter list" | ||
}, | ||
"engines": { | ||
@@ -26,0 +29,0 @@ "node": ">= 0.8.0" |
@@ -10,6 +10,15 @@ /**! | ||
function getKeys(obj) { | ||
var inspect = require('util').inspect; | ||
function getKeys (obj) { | ||
return _.keys(obj); | ||
}; | ||
function orderBy(result, opts) { | ||
if (opts.order.toLowerCase() === 'desc') { | ||
return result * -1; | ||
} | ||
return result; | ||
}; | ||
/** | ||
@@ -20,12 +29,35 @@ * Custom sort function to allow sorting by descending order | ||
*/ | ||
function sortBy(opts) { | ||
function sortBy (opts) { | ||
return function (objA, objB) { | ||
var result = 0; | ||
result = objA < objB ? -1 : 1; | ||
return orderBy(result, opts); | ||
}; | ||
}; | ||
if(opts.order.toLowerCase() === 'desc') { | ||
return result * -1; | ||
function sortByProperty (opts) { | ||
return function (objA, objB) { | ||
var result = 0; | ||
var propA = objA[opts.property]; | ||
var propB = objB[opts.property]; | ||
if ((_.isNull(propA) || _.isUndefined(propA)) && | ||
(_.isNull(propB) || _.isUndefined(propB))) { | ||
return result; | ||
} | ||
return result; | ||
if (_.isNull(propA) || _.isUndefined(propA)) { | ||
result = -1; | ||
} else if (_.isNull(propB) || _.isUndefined(propB)) { | ||
result = 1; | ||
} else { | ||
result = objA[opts.property] < objB[opts.property] ? -1 : 1; | ||
} | ||
return orderBy(result, opts); | ||
}; | ||
@@ -44,5 +76,50 @@ }; | ||
function sortProperties (obj, opts) { | ||
var sorted = {}; | ||
var pairs = _.pairs(obj); | ||
if (opts.property === true) { | ||
// sort on the value | ||
var values = []; | ||
for (var i = 0; i < pairs.length; i++) { | ||
var item = { | ||
key: pairs[i][0], | ||
value: pairs[i][1] | ||
}; | ||
values.push(item); | ||
} | ||
values.sort(sortByProperty(_.extend({}, opts, { property: 'value' }))); | ||
for (var i = 0; i < values.length; i++) { | ||
sorted[values[i].key] = values[i].value; | ||
} | ||
} else { | ||
// sort on the value of the property key | ||
var values = []; | ||
for (var i = 0; i < pairs.length; i++) { | ||
var value = pairs[i][1]; | ||
value.__origKey = pairs[i][0]; | ||
values.push(value); | ||
} | ||
values.sort(sortByProperty(opts)); | ||
for (var i = 0; i < values.length; i++) { | ||
var key = values[i].__origKey; | ||
var value = values[i]; | ||
delete value.__origKey; | ||
sorted[key] = value; | ||
} | ||
} | ||
return sorted; | ||
}; | ||
/** | ||
* Sorts the ksys on an object | ||
* Sorts the keys on an object | ||
* @param {Object} obj Object that has keys to be sorted | ||
@@ -62,34 +139,4 @@ * @param {Object} options Optional parameter specifying orders for the function | ||
if(opts.property === true) { | ||
var inverted = _.invert(obj); | ||
keys = sortKeys(inverted, opts); | ||
sorted = sortProperties(obj, opts); | ||
for (var index in keys) { | ||
key = keys[index]; | ||
sorted[inverted[key]] = key; | ||
} | ||
} else { | ||
var pairs = _.pairs(obj); | ||
var expanded = []; | ||
var keys = {}; | ||
for (var i = 0; i < pairs.length; i++) { | ||
key = pairs[i][1][opts.property]; | ||
keys[key] = pairs[i][0]; | ||
expanded.push(pairs[i][1]); | ||
} | ||
expanded = _.sortBy(expanded, opts.property); | ||
if(opts.order.toLowerCase() === 'desc') { | ||
expanded.reverse(); | ||
} | ||
for (var i = 0; i < expanded.length; i++) { | ||
var value = expanded[i][opts.property]; | ||
sorted[keys[value]] = expanded[i]; | ||
} | ||
} | ||
} else { | ||
@@ -96,0 +143,0 @@ |
@@ -93,2 +93,46 @@ /**! | ||
it('should sort the properties on an object with duplicate property values', function() { | ||
var outOfOrder = { | ||
'bar': 3, | ||
'foo': 1, | ||
'baz': 2, | ||
'bang': 2 | ||
}; | ||
var expected = { | ||
'foo': 1, | ||
'baz': 2, | ||
'bang': 2, | ||
'bar': 3 | ||
} | ||
var actual = sort(outOfOrder, { property: true }); | ||
assert.deepEqual(expected, actual); | ||
}); | ||
it('should sort the properties on an object with duplicate property values in decending order', function() { | ||
var outOfOrder = { | ||
'bar': 3, | ||
'foo': 1, | ||
'baz': 2, | ||
'bang': 2 | ||
}; | ||
var expected = { | ||
'bar': 3, | ||
'bang': 2, | ||
'baz': 2, | ||
'foo': 1 | ||
} | ||
var actual = sort(outOfOrder, { order: 'desc', property: true }); | ||
assert.deepEqual(expected, actual); | ||
}); | ||
it('should sort the objects by their properties', function () { | ||
@@ -134,2 +178,46 @@ | ||
it('should sort the objects by their properties on an object with duplicate property values', function() { | ||
var outOfOrder = { | ||
'baz': { 'a': 3, 'b': 2, 'c': 1 }, | ||
'foo': { 'a': 1, 'b': 3, 'c': 2 }, | ||
'bar': { 'a': 2, 'b': 1, 'c': 3 }, | ||
'bang': { 'a': 2, 'b': 1, 'c': 3 } | ||
}; | ||
var expected = { | ||
'foo': { 'a': 1, 'b': 3, 'c': 2 }, | ||
'bar': { 'a': 2, 'b': 1, 'c': 3 }, | ||
'bang': { 'a': 2, 'b': 1, 'c': 3 }, | ||
'baz': { 'a': 3, 'b': 2, 'c': 1 } | ||
}; | ||
var actual = sort(outOfOrder, { property: 'a' }); | ||
assert.deepEqual(expected, actual); | ||
}); | ||
it('should sort the objects by their properties on an object with duplicate property values in decending order', function() { | ||
var outOfOrder = { | ||
'baz': { 'a': 3, 'b': 2, 'c': 1 }, | ||
'foo': { 'a': 1, 'b': 3, 'c': 2 }, | ||
'bar': { 'a': 2, 'b': 1, 'c': 3 }, | ||
'bang': { 'a': 2, 'b': 1, 'c': 3 } | ||
}; | ||
var expected = { | ||
'baz': { 'a': 3, 'b': 2, 'c': 1 }, | ||
'bang': { 'a': 2, 'b': 1, 'c': 3 }, | ||
'bar': { 'a': 2, 'b': 1, 'c': 3 }, | ||
'foo': { 'a': 1, 'b': 3, 'c': 2 } | ||
}; | ||
var actual = sort(outOfOrder, { order: 'desc', property: 'a' }); | ||
assert.deepEqual(expected, actual); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11614
313