'use strict';
/**
* @name keta.filters.OrderObjectBy
* @author Marco Lehmann <marco.lehmann@kiwigrid.com>
* @copyright Kiwigrid GmbH 2014-2015
* @module keta.filters.OrderObjectBy
* @description
* <p>
* A filter to extract <code>limit</code> elements beginning at <code>offset</code>
* out of an array.
* </p>
* @example
* {{ row | orderObjectBy:['col1', 'col2'] }}
* {{ row | orderObjectBy:['col1', 'col2']:true }}
* @example
* angular.module('exampleApp', ['keta.filters.OrderObjectBy'])
* .controller('ExampleController', function($scope) {
*
* // return object values in given order (all other values are dismissed)
* $scope.orderedProps = $filter('ketaOrderObjectBy')($scope.row, ['col1', 'col2']);
*
* // return object keys in given order (all other keys are dismissed)
* $scope.orderedProps = $filter('ketaOrderObjectBy')($scope.row, ['col1', 'col2'], true);
*
* });
*/
angular.module('keta.filters.OrderObjectBy', [])
.filter('ketaOrderObjectBy', function() {
return function(input, order, returnKey) {
if (!angular.isObject(input)) {
return input;
}
returnKey = returnKey || false;
var fields = [];
// default sort order
if (!angular.isDefined(order) || order === null) {
angular.forEach(input, function(value, key) {
if (key.indexOf('$') !== 0) {
fields.push(key);
}
});
} else {
fields = order;
}
// sort by fields
var sorted = [];
angular.forEach(fields, function(field) {
if (angular.isDefined(input[field])) {
sorted.push(returnKey ? field : input[field]);
}
});
return sorted;
};
});