Skip to content

Instantly share code, notes, and snippets.

@teone
Created February 8, 2016 21:19
Show Gist options
  • Save teone/6daff7a8b3145e1384c6 to your computer and use it in GitHub Desktop.
Save teone/6daff7a8b3145e1384c6 to your computer and use it in GitHub Desktop.
Oder Object By Key Filter
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;
}, {});
};
})
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