1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _findIndex2 = require('lodash/findIndex');
|
8 |
|
9 | var _findIndex3 = _interopRequireDefault(_findIndex2);
|
10 |
|
11 | var _forOwn2 = require('lodash/forOwn');
|
12 |
|
13 | var _forOwn3 = _interopRequireDefault(_forOwn2);
|
14 |
|
15 | var _isEqual2 = require('lodash/isEqual');
|
16 |
|
17 | var _isEqual3 = _interopRequireDefault(_isEqual2);
|
18 |
|
19 | var _differenceWith2 = require('lodash/differenceWith');
|
20 |
|
21 | var _differenceWith3 = _interopRequireDefault(_differenceWith2);
|
22 |
|
23 | var _groupBy2 = require('lodash/groupBy');
|
24 |
|
25 | var _groupBy3 = _interopRequireDefault(_groupBy2);
|
26 |
|
27 | var _indexOf2 = require('lodash/indexOf');
|
28 |
|
29 | var _indexOf3 = _interopRequireDefault(_indexOf2);
|
30 |
|
31 | var _isNil2 = require('lodash/isNil');
|
32 |
|
33 | var _isNil3 = _interopRequireDefault(_isNil2);
|
34 |
|
35 | var _isEmpty2 = require('lodash/isEmpty');
|
36 |
|
37 | var _isEmpty3 = _interopRequireDefault(_isEmpty2);
|
38 |
|
39 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
40 |
|
41 | function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
42 |
|
43 | var fixOrder = function fixOrder() {
|
44 | var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
45 | _ref$idField = _ref.idField,
|
46 | idField = _ref$idField === undefined ? 'id' : _ref$idField,
|
47 | _ref$parentField = _ref.parentField,
|
48 | parentField = _ref$parentField === undefined ? 'parent' : _ref$parentField;
|
49 |
|
50 | return function (rows) {
|
51 | if ((0, _isEmpty3.default)(rows)) {
|
52 | return rows;
|
53 | }
|
54 |
|
55 |
|
56 | var existingIds = rows.reduce(function (ids, row) {
|
57 | if (!(0, _isNil3.default)(row[idField])) {
|
58 | ids.push(row[idField]);
|
59 | }
|
60 | return ids;
|
61 | }, []);
|
62 |
|
63 |
|
64 |
|
65 | var children = rows.filter(function (x) {
|
66 | var hasParentId = !(0, _isNil3.default)(x[parentField]);
|
67 | var parentExists = hasParentId && (0, _indexOf3.default)(existingIds, x[parentField]) !== -1;
|
68 | return hasParentId && parentExists;
|
69 | });
|
70 |
|
71 | if (!children.length) {
|
72 | return rows;
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | var childrenPerParent = (0, _groupBy3.default)(children, function (x) {
|
79 | return x[parentField];
|
80 | });
|
81 |
|
82 |
|
83 |
|
84 | var rowsWithoutChildren = (0, _differenceWith3.default)(rows, children, _isEqual3.default);
|
85 |
|
86 | (0, _forOwn3.default)(childrenPerParent, function (childrenFromParent, parentId) {
|
87 | var parentPosition = (0, _findIndex3.default)(rowsWithoutChildren, function (x) {
|
88 | var hasId = !(0, _isNil3.default)(x[idField]);
|
89 | var matchesParentId = hasId && parentId === x[idField].toString();
|
90 | return hasId && matchesParentId;
|
91 | });
|
92 |
|
93 | rowsWithoutChildren.splice.apply(rowsWithoutChildren, [parentPosition + 1, 0].concat(_toConsumableArray(childrenFromParent)));
|
94 | });
|
95 |
|
96 | return rowsWithoutChildren;
|
97 | };
|
98 | };
|
99 |
|
100 | exports.default = fixOrder; |
\ | No newline at end of file |