UNPKG

3.38 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 _isEqual2 = require('lodash/isEqual');
16
17var _isEqual3 = _interopRequireDefault(_isEqual2);
18
19var _differenceWith2 = require('lodash/differenceWith');
20
21var _differenceWith3 = _interopRequireDefault(_differenceWith2);
22
23var _groupBy2 = require('lodash/groupBy');
24
25var _groupBy3 = _interopRequireDefault(_groupBy2);
26
27var _indexOf2 = require('lodash/indexOf');
28
29var _indexOf3 = _interopRequireDefault(_indexOf2);
30
31var _isNil2 = require('lodash/isNil');
32
33var _isNil3 = _interopRequireDefault(_isNil2);
34
35var _isEmpty2 = require('lodash/isEmpty');
36
37var _isEmpty3 = _interopRequireDefault(_isEmpty2);
38
39function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
40
41function _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
43var 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 // collect all IDs from rows so we can validate parent IDs
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 // collect all rows which are children,
64 // meaning they have "parent" property and it points to existing row
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 // groups all children per parent into a object,
76 // which looks like that:
77 // { parent_id1 : [children of parent id1], parent_id2: [... ] }
78 var childrenPerParent = (0, _groupBy3.default)(children, function (x) {
79 return x[parentField];
80 });
81
82 // removes all children from rows so we can start putting them
83 // into right place
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 // puts children right after their parent so correct order will be kept
93 rowsWithoutChildren.splice.apply(rowsWithoutChildren, [parentPosition + 1, 0].concat(_toConsumableArray(childrenFromParent)));
94 });
95
96 return rowsWithoutChildren;
97 };
98};
99
100exports.default = fixOrder;
\No newline at end of file