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 _keys2 = require('lodash/keys');
|
16 |
|
17 | var _keys3 = _interopRequireDefault(_keys2);
|
18 |
|
19 | var _isEqual2 = require('lodash/isEqual');
|
20 |
|
21 | var _isEqual3 = _interopRequireDefault(_isEqual2);
|
22 |
|
23 | var _differenceWith2 = require('lodash/differenceWith');
|
24 |
|
25 | var _differenceWith3 = _interopRequireDefault(_differenceWith2);
|
26 |
|
27 | var _groupBy2 = require('lodash/groupBy');
|
28 |
|
29 | var _groupBy3 = _interopRequireDefault(_groupBy2);
|
30 |
|
31 | var _indexOf2 = require('lodash/indexOf');
|
32 |
|
33 | var _indexOf3 = _interopRequireDefault(_indexOf2);
|
34 |
|
35 | var _isNil2 = require('lodash/isNil');
|
36 |
|
37 | var _isNil3 = _interopRequireDefault(_isNil2);
|
38 |
|
39 | var _isEmpty2 = require('lodash/isEmpty');
|
40 |
|
41 | var _isEmpty3 = _interopRequireDefault(_isEmpty2);
|
42 |
|
43 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
44 |
|
45 | 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); } }
|
46 |
|
47 | var fixOrder = function fixOrder() {
|
48 | var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
49 | _ref$idField = _ref.idField,
|
50 | idField = _ref$idField === undefined ? 'id' : _ref$idField,
|
51 | _ref$parentField = _ref.parentField,
|
52 | parentField = _ref$parentField === undefined ? 'parent' : _ref$parentField;
|
53 |
|
54 | return function (rows) {
|
55 | if ((0, _isEmpty3.default)(rows)) {
|
56 | return rows;
|
57 | }
|
58 |
|
59 |
|
60 | var existingIds = rows.reduce(function (ids, row) {
|
61 | if (!(0, _isNil3.default)(row[idField])) {
|
62 | ids.push(row[idField]);
|
63 | }
|
64 | return ids;
|
65 | }, []);
|
66 |
|
67 |
|
68 |
|
69 | var children = rows.filter(function (x) {
|
70 | var hasParentId = !(0, _isNil3.default)(x[parentField]);
|
71 | var parentExists = hasParentId && (0, _indexOf3.default)(existingIds, x[parentField]) !== -1;
|
72 | return hasParentId && parentExists;
|
73 | });
|
74 |
|
75 | if (!children.length) {
|
76 | return rows;
|
77 | }
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | var childrenPerParent = (0, _groupBy3.default)(children, function (x) {
|
83 | return x[parentField];
|
84 | });
|
85 |
|
86 |
|
87 |
|
88 | var rowsWithoutChildren = (0, _differenceWith3.default)(rows, children, _isEqual3.default);
|
89 | var childrenCount = (0, _keys3.default)(childrenPerParent).length;
|
90 | var prevChildrenCount = void 0;
|
91 | do {
|
92 | prevChildrenCount = childrenCount;
|
93 | childrenPerParent = attachChildren(rowsWithoutChildren, childrenPerParent, idField);
|
94 | childrenCount = (0, _keys3.default)(childrenPerParent).length;
|
95 | } while (childrenCount < prevChildrenCount);
|
96 |
|
97 | return rowsWithoutChildren;
|
98 | };
|
99 | };
|
100 |
|
101 | function attachChildren(rowsWithoutChildren, childrenPerParent, idField) {
|
102 | var remainingChildrenPerParent = childrenPerParent;
|
103 | (0, _forOwn3.default)(childrenPerParent, function (childrenFromParent, parentId) {
|
104 | var parentPosition = (0, _findIndex3.default)(rowsWithoutChildren, function (x) {
|
105 | var hasId = !(0, _isNil3.default)(x[idField]);
|
106 | var matchesParentId = hasId && parentId === x[idField].toString();
|
107 | return hasId && matchesParentId;
|
108 | });
|
109 |
|
110 | if (parentPosition !== -1) {
|
111 | rowsWithoutChildren.splice.apply(rowsWithoutChildren, [parentPosition + 1, 0].concat(_toConsumableArray(childrenFromParent)));
|
112 | delete remainingChildrenPerParent[parentId];
|
113 | }
|
114 | });
|
115 | return remainingChildrenPerParent;
|
116 | }
|
117 |
|
118 | exports.default = fixOrder; |
\ | No newline at end of file |