UNPKG

@boomerang-io/utils

Version:

A library of reusable utilities and hooks for React webapps on the Boomerang platform.

32 lines (31 loc) 1.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _orderBy = _interopRequireDefault(require("lodash/orderBy")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * 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]] */ const sortByProp = function (arr, prop) { let direction = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "asc"; return (0, _orderBy.default)(arr, typeof prop === "string" ? [prop] : prop, [direction]); }; var _default = exports.default = sortByProp;