ramda-adjunct
Version:
Ramda Adjunct is the most popular and most comprehensive set of utilities for use with Ramda, providing a variety of useful, well tested functions with excellent documentation.
58 lines (46 loc) • 2.77 kB
JavaScript
exports.__esModule = true;
exports["default"] = void 0;
var _ramda = require("ramda");
function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
/**
* Sort a list of objects by a list of props (if first prop value is equivalent, sort by second, etc).
*
* @func sortByProps
* @memberOf RA
* @since {@link https://char0n.github.io/ramda-adjunct/2.26.0|v2.26.0}
* @category List
* @sig [k] -> [{k: v}] -> [{k: v}]
* @param {Array.<string>} props A list of properties in the list param to sort by
* @param {Array.<object>} list A list of objects to be sorted
* @return {Array.<object>} A new list sorted by the properties in the props param
* @example
*
* sortByProps(['num'], [{num: 3}, {num: 2}, {num: 1}])
* //=> [{num: 1}, {num: 2} {num: 3}]
* sortByProps(['letter', 'num'], [{num: 3, letter: 'a'}, {num: 2, letter: 'a'} {num: 1, letter: 'z'}])
* //=> [ {num: 2, letter: 'a'}, {num: 3, letter: 'a'}, {num: 1, letter: 'z'}]
* sortByProps(['name', 'num'], [{num: 3}, {num: 2}, {num: 1}])
* //=> [{num: 1}, {num: 2}, {num: 3}]
*/
var sortByProps = (0, _ramda.curry)(function (props, list) {
var firstTruthy = function firstTruthy(_ref) {
var _ref2 = _toArray(_ref),
head = _ref2[0],
tail = _ref2.slice(1);
return (0, _ramda.reduce)(_ramda.either, head, tail);
};
var makeComparator = function makeComparator(propName) {
return (0, _ramda.comparator)(function (a, b) {
return (0, _ramda.lt)((0, _ramda.prop)(propName, a), (0, _ramda.prop)(propName, b));
});
};
return (0, _ramda.sort)(firstTruthy((0, _ramda.map)(makeComparator, props)), list);
});
var _default = sortByProps;
exports["default"] = _default;
;