source: node_modules/ramda-adjunct/es/sortByProps.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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