UNPKG

4.07 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _findIndex2 = require('lodash/findIndex');
8
9var _findIndex3 = _interopRequireDefault(_findIndex2);
10
11var _forOwn2 = require('lodash/forOwn');
12
13var _forOwn3 = _interopRequireDefault(_forOwn2);
14
15var _keys2 = require('lodash/keys');
16
17var _keys3 = _interopRequireDefault(_keys2);
18
19var _isEqual2 = require('lodash/isEqual');
20
21var _isEqual3 = _interopRequireDefault(_isEqual2);
22
23var _differenceWith2 = require('lodash/differenceWith');
24
25var _differenceWith3 = _interopRequireDefault(_differenceWith2);
26
27var _groupBy2 = require('lodash/groupBy');
28
29var _groupBy3 = _interopRequireDefault(_groupBy2);
30
31var _indexOf2 = require('lodash/indexOf');
32
33var _indexOf3 = _interopRequireDefault(_indexOf2);
34
35var _isNil2 = require('lodash/isNil');
36
37var _isNil3 = _interopRequireDefault(_isNil2);
38
39var _isEmpty2 = require('lodash/isEmpty');
40
41var _isEmpty3 = _interopRequireDefault(_isEmpty2);
42
43function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
44
45function _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
47var 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 // collect all IDs from rows so we can validate parent IDs
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 // collect all rows which are children,
68 // meaning they have "parent" property and it points to existing row
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 // groups all children per parent into a object,
80 // which looks like that:
81 // { parent_id1 : [children of parent id1], parent_id2: [... ] }
82 var childrenPerParent = (0, _groupBy3.default)(children, function (x) {
83 return x[parentField];
84 });
85
86 // removes all children from rows so we can start putting them
87 // into right place
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
101function 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 // puts children right after their parent so correct order will be kept
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
118exports.default = fixOrder;
\No newline at end of file