@boomerang-io/utils
Version:
A library of reusable utilities and hooks for React webapps on the Boomerang platform.
37 lines (32 loc) • 1.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _orderBy = _interopRequireDefault(require("lodash/orderBy"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Creates array with the objects of given array whose elements
* are sorted with chosen property.
*
* @param {Array} arr - The array to iterate over
* @param {String} prop - The iteratees to sort by
* @param {String} direction - The sort orders of `iteratees`
* @returns {Array} Returns the new sorted array.
* @example
* const users = [
* { 'user': 'fred', 'age': 48 },
* { 'user': 'barney', 'age': 34 },
* { 'user': 'alexander', 'age': 40 }
* ]
*
* // Sort by `user` in ascending order.
* sortByProp(users, "user")
* // => objects for [['barney', 34], ['fred', 48], ['alexander', 40]]
*/
var sortByProp = function sortByProp(arr, prop) {
var direction = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "asc";
return (0, _orderBy.default)(arr, typeof prop === "string" ? [prop] : prop, [direction]);
};
var _default = sortByProp;
exports.default = _default;