| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|---|
| 4 |
|
|---|
| 5 | const compareValues = require('../_internal/compareValues.js');
|
|---|
| 6 | const isKey = require('../_internal/isKey.js');
|
|---|
| 7 | const toPath = require('../util/toPath.js');
|
|---|
| 8 |
|
|---|
| 9 | function orderBy(collection, criteria, orders, guard) {
|
|---|
| 10 | if (collection == null) {
|
|---|
| 11 | return [];
|
|---|
| 12 | }
|
|---|
| 13 | orders = guard ? undefined : orders;
|
|---|
| 14 | if (!Array.isArray(collection)) {
|
|---|
| 15 | collection = Object.values(collection);
|
|---|
| 16 | }
|
|---|
| 17 | if (!Array.isArray(criteria)) {
|
|---|
| 18 | criteria = criteria == null ? [null] : [criteria];
|
|---|
| 19 | }
|
|---|
| 20 | if (criteria.length === 0) {
|
|---|
| 21 | criteria = [null];
|
|---|
| 22 | }
|
|---|
| 23 | if (!Array.isArray(orders)) {
|
|---|
| 24 | orders = orders == null ? [] : [orders];
|
|---|
| 25 | }
|
|---|
| 26 | orders = orders.map(order => String(order));
|
|---|
| 27 | const getValueByNestedPath = (object, path) => {
|
|---|
| 28 | let target = object;
|
|---|
| 29 | for (let i = 0; i < path.length && target != null; ++i) {
|
|---|
| 30 | target = target[path[i]];
|
|---|
| 31 | }
|
|---|
| 32 | return target;
|
|---|
| 33 | };
|
|---|
| 34 | const getValueByCriterion = (criterion, object) => {
|
|---|
| 35 | if (object == null || criterion == null) {
|
|---|
| 36 | return object;
|
|---|
| 37 | }
|
|---|
| 38 | if (typeof criterion === 'object' && 'key' in criterion) {
|
|---|
| 39 | if (Object.hasOwn(object, criterion.key)) {
|
|---|
| 40 | return object[criterion.key];
|
|---|
| 41 | }
|
|---|
| 42 | return getValueByNestedPath(object, criterion.path);
|
|---|
| 43 | }
|
|---|
| 44 | if (typeof criterion === 'function') {
|
|---|
| 45 | return criterion(object);
|
|---|
| 46 | }
|
|---|
| 47 | if (Array.isArray(criterion)) {
|
|---|
| 48 | return getValueByNestedPath(object, criterion);
|
|---|
| 49 | }
|
|---|
| 50 | if (typeof object === 'object') {
|
|---|
| 51 | return object[criterion];
|
|---|
| 52 | }
|
|---|
| 53 | return object;
|
|---|
| 54 | };
|
|---|
| 55 | const preparedCriteria = criteria.map((criterion) => {
|
|---|
| 56 | if (Array.isArray(criterion) && criterion.length === 1) {
|
|---|
| 57 | criterion = criterion[0];
|
|---|
| 58 | }
|
|---|
| 59 | if (criterion == null || typeof criterion === 'function' || Array.isArray(criterion) || isKey.isKey(criterion)) {
|
|---|
| 60 | return criterion;
|
|---|
| 61 | }
|
|---|
| 62 | return { key: criterion, path: toPath.toPath(criterion) };
|
|---|
| 63 | });
|
|---|
| 64 | const preparedCollection = collection.map(item => ({
|
|---|
| 65 | original: item,
|
|---|
| 66 | criteria: preparedCriteria.map((criterion) => getValueByCriterion(criterion, item)),
|
|---|
| 67 | }));
|
|---|
| 68 | return preparedCollection
|
|---|
| 69 | .slice()
|
|---|
| 70 | .sort((a, b) => {
|
|---|
| 71 | for (let i = 0; i < preparedCriteria.length; i++) {
|
|---|
| 72 | const comparedResult = compareValues.compareValues(a.criteria[i], b.criteria[i], orders[i]);
|
|---|
| 73 | if (comparedResult !== 0) {
|
|---|
| 74 | return comparedResult;
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 | return 0;
|
|---|
| 78 | })
|
|---|
| 79 | .map(item => item.original);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | exports.orderBy = orderBy;
|
|---|