Skip to content

Commit 8b39222

Browse files
author
Evgeny Poberezkin
committed
[evgeny, pawel] compareProperty function
1 parent 91e3424 commit 8b39222

File tree

6 files changed

+50
-2
lines changed

6 files changed

+50
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,5 @@ Functions
135135
* [result](http://mailonline.github.io/proto/proto_util.js.html#result)
136136
* [identity](http://mailonline.github.io/proto/proto_util.js.html#identity)
137137
* [property](http://mailonline.github.io/proto/proto_util.js.html#property)
138+
* [compareProperty](http://mailonline.github.io/proto/proto_util.js.html#compareProperty)
138139
* [noop](http://mailonline.github.io/proto_util.js.html#noop)

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "proto",
3-
"version": "0.1.10",
3+
"version": "0.1.11",
44
"homepage": "https://github.com/MailOnline/proto",
55
"authors": [
66
"MOL Technical <[email protected]>"

lib/proto.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ var numberMethods = require('./proto_number');
118118
* - [result](proto_util.js.html#result)
119119
* - [identity](proto_util.js.html#identity)
120120
* - [property](proto_util.js.html#property)
121+
* - [compareProperty](proto_util.js.html#compareProperty)
121122
* - [noop](proto_util.js.html#noop)
122123
*/
123124
var utilMethods = require('./proto_util');

lib/proto_util.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* - [result](#result)
88
* - [identity](#identity)
99
* - [property](#property)
10+
* - [compareProperty](#compareProperty)
1011
* - [noop](#noop)
1112
*/
1213
var utilMethods = module.exports = {
@@ -16,6 +17,7 @@ var utilMethods = module.exports = {
1617
result: result,
1718
identity: identity,
1819
property: property,
20+
compareProperty: compareProperty,
1921
noop: noop
2022
};
2123

@@ -104,6 +106,24 @@ function property() {
104106
}
105107

106108

109+
/**
110+
* Returns function that can be used in array sort to sort by a given property
111+
*
112+
* @param {String} self
113+
* @return {Function}
114+
*/
115+
function compareProperty() {
116+
var key = this;
117+
return function(a, b) {
118+
return a[key] < b[key]
119+
? -1
120+
: a[key] > b[key]
121+
? 1
122+
: 0;
123+
};
124+
}
125+
126+
107127
/**
108128
* Function that does nothing
109129
*/

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mol-proto",
3-
"version": "0.1.10",
3+
"version": "0.1.11",
44
"description": "ES5 object manipulation library for node and modern browsers",
55
"main": "lib/proto.js",
66
"scripts": {

test/proto_util_test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,30 @@ describe('Utility functions', function() {
7070
assert.equal(p1(obj), 1);
7171
assert.equal(p2(obj), 2);
7272
});
73+
74+
75+
it('should define compareProperty function', function() {
76+
var arr = [
77+
{ value: 1, label: 'a'},
78+
{ value: 4, label: 'c'},
79+
{ value: 3, label: 'd'},
80+
{ value: 2, label: 'b'}
81+
];
82+
83+
arr.sort(_.compareProperty('value'));
84+
assert.deepEqual(arr, [
85+
{ value: 1, label: 'a'},
86+
{ value: 2, label: 'b'},
87+
{ value: 3, label: 'd'},
88+
{ value: 4, label: 'c'}
89+
]);
90+
91+
arr.sort(_.compareProperty('label'));
92+
assert.deepEqual(arr, [
93+
{ value: 1, label: 'a'},
94+
{ value: 2, label: 'b'},
95+
{ value: 4, label: 'c'},
96+
{ value: 3, label: 'd'}
97+
]);
98+
});
7399
});

0 commit comments

Comments
 (0)