Created
February 8, 2016 21:19
-
-
Save teone/6daff7a8b3145e1384c6 to your computer and use it in GitHub Desktop.
Oder Object By Key Filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('myModule', []) | |
.filter('orderObjectByKey', function(lodash) { | |
return function(items) { | |
if(!items){ | |
return; | |
} | |
return lodash.reduce(Object.keys(items).reverse(), (list, key) => { | |
list[key] = items[key]; | |
return list; | |
}, {}); | |
}; | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('The orderObjectByKey filter', () => { | |
var $filter; | |
beforeEach(function () { | |
module('myModule'); | |
inject(function (_$filter_) { | |
$filter = _$filter_; | |
}); | |
}); | |
it('should order an object by the key value', function () { | |
// Arrange. | |
const list = {c: 3, b: 2, a: 1}; | |
// call the filter function | |
const result = $filter('orderObjectByKey')(list); | |
// Assert. | |
expect(result).toEqual({a: 1, b: 2, c: 3}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment