Skip to content

Commit

Permalink
Merge pull request #4 from Wildhoney/master
Browse files Browse the repository at this point in the history
Allowed the passing of a map function to apply to each property
  • Loading branch information
kvnneff authored Jan 25, 2017
2 parents 1a19fb9 + 76b537e commit e5209d0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
33 changes: 25 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
var objectPath = require('object-path');
var sortBy;
var sort;
var type;

/**
* Filters args based on their type
* @param {String} type Type of property to filter by
* @return {Function}
*/
type = function(type) {
return function(arg) {
return typeof arg === type;
};
};

/**
* Return a comparator function
* @param {String} property The key to sort by
* @param {Function} map Function to apply to each property
* @return {Function} Returns the comparator function
*/
sort = function sort(property) {
sort = function sort(property, map) {
var sortOrder = 1;
var fn;
var apply = map || function(_, value) { return value };

if (property[0] === "-") {
sortOrder = -1;
Expand All @@ -18,9 +31,11 @@ sort = function sort(property) {

return function fn(a,b) {
var result;
if (objectPath.get(a, property) < objectPath.get(b, property)) result = -1;
if (objectPath.get(a, property) > objectPath.get(b, property)) result = 1;
if (objectPath.get(a, property) === objectPath.get(b, property)) result = 0;
var am = apply(property, objectPath.get(a, property));
var bm = apply(property, objectPath.get(b, property));
if (am < bm) result = -1;
if (am > bm) result = 1;
if (am === bm) result = 0;
return result * sortOrder;
}
};
Expand All @@ -30,8 +45,10 @@ sort = function sort(property) {
* @return {Function} Returns the comparator function
*/
sortBy = function sortBy() {
var properties = arguments;
var fn;

var args = Array.prototype.slice.call(arguments);
var properties = args.filter(type('string'));
var map = args.filter(type('function'))[0];

return function fn(obj1, obj2) {
var numberOfProperties = properties.length,
Expand All @@ -42,7 +59,7 @@ sortBy = function sortBy() {
* as long as we have extra properties to compare
*/
while(result === 0 && i < numberOfProperties) {
result = sort(properties[i])(obj1, obj2);
result = sort(properties[i], map)(obj1, obj2);
i++;
}
return result;
Expand Down
28 changes: 28 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ describe('Sort(prop)', function () {
assert(array[3].x === 4);
assert(array[3].y === 1);
});
it('sorts an array case-insensitively by applying a function', function() {

var array = [
{ name: 'Hummingbird' },
{ name: 'swallow' },
{ name: 'Finch' },
{ name: 'Sparrow' },
{ name: 'cuckoos' }
];

array.sort(sortBy('name'));
assert(array[0].name === 'Finch');
assert(array[1].name === 'Hummingbird');
assert(array[2].name === 'Sparrow');
assert(array[3].name === 'cuckoos');
assert(array[4].name === 'swallow');

array.sort(sortBy('name', function(key, value) {
return key === 'name' ? value.toLowerCase() : value;
}));

assert(array[0].name === 'cuckoos');
assert(array[1].name === 'Finch');
assert(array[2].name === 'Hummingbird');
assert(array[3].name === 'Sparrow');
assert(array[4].name === 'swallow');

});
});

describe('Sort(prop, prop)', function () {
Expand Down

0 comments on commit e5209d0

Please sign in to comment.