1 | "use strict";
|
---|
2 |
|
---|
3 | exports.__esModule = true;
|
---|
4 | exports["default"] = void 0;
|
---|
5 | var _ramda = require("ramda");
|
---|
6 | function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest(); }
|
---|
7 | 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."); }
|
---|
8 | 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); }
|
---|
9 | 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; }
|
---|
10 | function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
---|
11 | function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
---|
12 | /**
|
---|
13 | * Sort a list of objects by a list of props (if first prop value is equivalent, sort by second, etc).
|
---|
14 | *
|
---|
15 | * @func sortByProps
|
---|
16 | * @memberOf RA
|
---|
17 | * @since {@link https://char0n.github.io/ramda-adjunct/2.26.0|v2.26.0}
|
---|
18 | * @category List
|
---|
19 | * @sig [k] -> [{k: v}] -> [{k: v}]
|
---|
20 | * @param {Array.<string>} props A list of properties in the list param to sort by
|
---|
21 | * @param {Array.<object>} list A list of objects to be sorted
|
---|
22 | * @return {Array.<object>} A new list sorted by the properties in the props param
|
---|
23 | * @example
|
---|
24 | *
|
---|
25 | * sortByProps(['num'], [{num: 3}, {num: 2}, {num: 1}])
|
---|
26 | * //=> [{num: 1}, {num: 2} {num: 3}]
|
---|
27 | * sortByProps(['letter', 'num'], [{num: 3, letter: 'a'}, {num: 2, letter: 'a'} {num: 1, letter: 'z'}])
|
---|
28 | * //=> [ {num: 2, letter: 'a'}, {num: 3, letter: 'a'}, {num: 1, letter: 'z'}]
|
---|
29 | * sortByProps(['name', 'num'], [{num: 3}, {num: 2}, {num: 1}])
|
---|
30 | * //=> [{num: 1}, {num: 2}, {num: 3}]
|
---|
31 | */
|
---|
32 |
|
---|
33 | var sortByProps = (0, _ramda.curry)(function (props, list) {
|
---|
34 | var firstTruthy = function firstTruthy(_ref) {
|
---|
35 | var _ref2 = _toArray(_ref),
|
---|
36 | head = _ref2[0],
|
---|
37 | tail = _ref2.slice(1);
|
---|
38 | return (0, _ramda.reduce)(_ramda.either, head, tail);
|
---|
39 | };
|
---|
40 | var makeComparator = function makeComparator(propName) {
|
---|
41 | return (0, _ramda.comparator)(function (a, b) {
|
---|
42 | return (0, _ramda.lt)((0, _ramda.prop)(propName, a), (0, _ramda.prop)(propName, b));
|
---|
43 | });
|
---|
44 | };
|
---|
45 | return (0, _ramda.sort)(firstTruthy((0, _ramda.map)(makeComparator, props)), list);
|
---|
46 | });
|
---|
47 | var _default = sortByProps;
|
---|
48 | exports["default"] = _default; |
---|