@gudhub/core 1.2.4-beta.37 → 1.2.4-beta.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -443,7 +443,14 @@ export default class AppsTemplateService {
443
443
  });
444
444
 
445
445
  self.crawling(app.field_list, function (prop, value, parent) {
446
- if (prop.indexOf("field_id") !== -1 || prop.indexOf("FieldId") !== -1 || prop.indexOf("destination_field") !== -1) {
446
+ const fieldProps = ["field_id", "FieldId", "destination_field", "element_id"];
447
+ const appProps = ["app_id", "AppId", "destination_app"];
448
+ const viewProps = ["view_id"];
449
+ const srcProps = ["src"];
450
+
451
+ const hasProp = (prop, propArray) => propArray.some(key => prop.indexOf(key) !== -1);
452
+
453
+ if (hasProp(prop, fieldProps)) {
447
454
  let fieldsArr = String(value).split(','), newFieldsArr = [];
448
455
 
449
456
  appsConnectingMap.fields.forEach(field => {
@@ -459,7 +466,7 @@ export default class AppsTemplateService {
459
466
  }
460
467
  }
461
468
 
462
- if (prop.indexOf("app_id") != -1 || prop.indexOf("AppId") != -1 || prop.indexOf("destination_app") != -1) {
469
+ if (hasProp(prop, appProps)) {
463
470
  appsConnectingMap.apps.forEach(app => {
464
471
  if (value == app.old_app_id) {
465
472
  parent[prop] = app.new_app_id;
@@ -467,7 +474,7 @@ export default class AppsTemplateService {
467
474
  })
468
475
  }
469
476
 
470
- if (prop.indexOf("view_id") !== -1) {
477
+ if (hasProp(prop, viewProps)) {
471
478
  appsConnectingMap.views.forEach(view => {
472
479
  if (value == view.old_view_id) {
473
480
  parent[prop] = view.new_view_id;
@@ -475,7 +482,7 @@ export default class AppsTemplateService {
475
482
  })
476
483
  }
477
484
 
478
- if (prop.indexOf("src") !== -1 && isFinite(value)) {
485
+ if (hasProp(prop, srcProps) && isFinite(value)) {
479
486
  const pron_name = 'container_id';
480
487
  const old_app_id = appsConnectingMap.apps.find(appMap => appMap.new_app_id === app.app_id).old_app_id;
481
488
  const path = self.findPath(source_apps[old_app_id].views_list, pron_name, value);
@@ -53,7 +53,7 @@ describe("GET DATE", function () {
53
53
  day.getDay().should.equal(6);
54
54
  });
55
55
 
56
- it("CHECK IF DATE TODAY", function() {
56
+ it("CHECK RECURRING DATE / today", function() {
57
57
  let today = new Date();
58
58
  let result = gudhub.checkRecurringDate(today, 'day');
59
59
  result.should.equal(true);
@@ -64,7 +64,7 @@ describe("GET DATE", function () {
64
64
  result.should.equal(false);
65
65
  })
66
66
 
67
- it("CHECK IF DATE IN THIS WEEK", function() {
67
+ it("CHECK RECURRING DATE / week", function() {
68
68
  let today = new Date();
69
69
  let result = gudhub.checkRecurringDate(today, 'week');
70
70
  result.should.equal(true);
@@ -75,16 +75,7 @@ describe("GET DATE", function () {
75
75
  result.should.equal(false);
76
76
  });
77
77
 
78
- it('CHECK IF DATE IN THIS WEEK / Compare this wednesday with wednesday two years ago', function(){
79
- let day = gudhub.getDate('this_wednesday');
80
- let dayTwoYearsBefore = subYears(day, 2);
81
-
82
- // console.log(dayTwoYearsBefore);
83
- let result = gudhub.checkRecurringDate(dayTwoYearsBefore, 'week');
84
- result.should.equal(true);
85
- });
86
-
87
- it("CHECK IF DATE IN THIS MONTH", function() {
78
+ it("CHECK RECURRING DATE / month", function() {
88
79
  let today = new Date();
89
80
  let result = gudhub.checkRecurringDate(today, 'month');
90
81
  result.should.equal(true);
@@ -1,52 +1,55 @@
1
- export function makeNestedList(arr, id, parent_id, children_property, priority_property) {
2
- let array = JSON.parse(JSON.stringify(arr));
3
- let children = Boolean(children_property) ? children_property : 'children';
4
- let priority = Boolean(priority_property) ? priority_property : false;
5
- let i;
6
- for (i = 0; i < array.length; i++) {
7
- if (array[i][parent_id] && array[i][parent_id] !== 0) {
8
- array.forEach((parent) => {
9
- if (array[i][parent_id] == parent[id]) {
10
- if (!parent[children]) {
11
- parent[children] = [];
12
- }
13
- parent[children].push(array[i]);
14
- array.splice(i, 1);
15
- i == 0 ? i = 0 : i--;
16
- } else if (parent[children]) {
17
- findIdsOfChildren(parent);
18
- }
19
- });
20
- }
21
- }
22
-
23
- function findIdsOfChildren(parent) {
24
- parent[children].forEach((child) => {
25
- if (child[id] == array[i][parent_id]) {
26
- if (!child[children]) {
27
- child[children] = [];
1
+ export function makeNestedList(
2
+ arr,
3
+ id,
4
+ parent_id,
5
+ children_property = "children",
6
+ priority_property = null
7
+ ) {
8
+ let map = {};
9
+ let roots = [];
10
+
11
+ arr.forEach((item) => {
12
+ map[item[id]] = { ...item, [children_property]: [] };
13
+ });
14
+
15
+ arr.forEach((item) => {
16
+ const parentId = item[parent_id];
17
+ if (parentId && map[parentId]) {
18
+ map[parentId][children_property].push(map[item[id]]);
19
+ } else {
20
+ roots.push(map[item[id]]);
28
21
  }
29
- child[children].push(array[i]);
30
- array.splice(i, 1);
31
- i == 0 ? i = 0 : i--;
32
- } else if (child[children]) {
33
- findIdsOfChildren(child);
34
- }
35
22
  });
36
- }
37
-
38
- function sortChildrensByPriority(unsorted_array) {
39
- unsorted_array.sort( (a, b) => a[priority] - b[priority])
40
- unsorted_array.forEach(element => {
41
- if(element[children]) {
42
- sortChildrensByPriority(element[children])
43
- }
44
- })
45
- }
46
-
47
- if(priority) {
48
- sortChildrensByPriority(array);
49
- }
50
-
51
- return array;
52
- }
23
+
24
+ const sortTreeByPriority = (nodes) => {
25
+ nodes.forEach((node) => {
26
+ if (node[children_property].length > 0) {
27
+ sortTreeByPriority(node[children_property]);
28
+ }
29
+ });
30
+
31
+ if (priority_property) {
32
+ nodes.sort((a, b) => {
33
+ const priorityA = a[priority_property] ?? Infinity;
34
+ const priorityB = b[priority_property] ?? Infinity;
35
+ return priorityA - priorityB;
36
+ });
37
+ }
38
+ };
39
+
40
+ sortTreeByPriority(roots);
41
+
42
+ const cleanEmptyChildren = (nodes) => {
43
+ nodes.forEach((node) => {
44
+ if (node[children_property].length === 0) {
45
+ delete node[children_property];
46
+ } else {
47
+ cleanEmptyChildren(node[children_property]);
48
+ }
49
+ });
50
+ };
51
+
52
+ cleanEmptyChildren(roots);
53
+
54
+ return roots;
55
+ };
@@ -29,6 +29,35 @@ describe("NESTED LIST", function () {
29
29
  nestedList[0].title.should.equal('Nineth');
30
30
  })
31
31
 
32
+ it("Should return correct nested structure with children", function () {
33
+ let nestedList = gudhub.makeNestedList(nestedComplicatedListWithDocs, 'id', 'parent_id', 'children');
34
+
35
+ nestedList.should.be.an.Array().and.have.length(1);
36
+ nestedList[0].should.have.property('children');
37
+ nestedList[0].children.should.be.an.Array().and.have.length(4);
38
+ });
39
+
40
+ it("Should correctly sort children by priority", function () {
41
+ let nestedList = gudhub.makeNestedList(nestedComplicatedListWithDocs, 'id', 'parent_id', 'children', 'priority');
42
+
43
+ nestedList[0].children[0].title.should.equal('Intro');
44
+ nestedList[0].children[1].title.should.equal('Getting Started');
45
+ nestedList[0].children[2].title.should.equal('Node Installing');
46
+ });
47
+
48
+ it("Should correctly handle deeply nested structures", function () {
49
+ let nestedList = gudhub.makeNestedList(nestedComplicatedListWithDocs, 'id', 'parent_id', 'children', 'priority');
50
+ let featuresChildren = nestedList[0].children[3].children[4].children;
51
+
52
+ featuresChildren.should.be.an.Array().and.have.length(8);
53
+ featuresChildren[0].title.should.equal("Bundler");
54
+ featuresChildren[7].title.should.equal("SSR-Menu");
55
+ });
56
+
57
+ it("Should return empty array for items without parents when parent_id is not found", function () {
58
+ let nestedList = gudhub.makeNestedList([], 'id', 'parent_id', 'children');
59
+ nestedList.should.be.an.Array().and.have.length(0);
60
+ });
32
61
  });
33
62
 
34
63
  let input = [
@@ -150,7 +179,8 @@ let complicatedInput = [
150
179
  parent_id: "26553.2884361",
151
180
  title: "Child of second parent"
152
181
  }
153
- ]
182
+ ];
183
+
154
184
  let parentInput = [
155
185
  {
156
186
  "id": "27026.2937695",
@@ -182,4 +212,112 @@ let parentInput = [
182
212
  "title": "Nineth",
183
213
  "priority": "0"
184
214
  }
185
- ]
215
+ ];
216
+
217
+ let nestedComplicatedListWithDocs = [
218
+ {
219
+ id: "123001.1237001",
220
+ parent_id: "",
221
+ title: "SSR/SSG - Parent"
222
+ },
223
+ {
224
+ id: "123002.1237002",
225
+ parent_id: "123001.1237001",
226
+ title: "Intro",
227
+ priority: 1
228
+ },
229
+ {
230
+ id: "123003.1237003",
231
+ parent_id: "123001.1237001",
232
+ title: "Getting Started",
233
+ priority: 2
234
+ },
235
+ {
236
+ id: "123004.1237004",
237
+ parent_id: "123001.1237001",
238
+ title: "Node Installing",
239
+ priority: 3
240
+ },
241
+ {
242
+ id: "123005.1237005",
243
+ parent_id: "123001.1237001",
244
+ title: "Server - Parent 2"
245
+ },
246
+ {
247
+ id: "123006.1237006",
248
+ parent_id: "123005.1237005",
249
+ title: "Server",
250
+ priority: 4
251
+ },
252
+ {
253
+ id: "123007.1237007",
254
+ parent_id: "123005.1237005",
255
+ title: "Install",
256
+ priority: 5
257
+ },
258
+ {
259
+ id: "123008.1237008",
260
+ parent_id: "123005.1237005",
261
+ title: "Endpoints",
262
+ priority: 6
263
+ },
264
+ {
265
+ id: "123009.1237009",
266
+ parent_id: "123005.1237005",
267
+ title: "Configure",
268
+ priority: 7
269
+ },
270
+ {
271
+ id: "123010.1237010",
272
+ parent_id: "123005.1237005",
273
+ title: "Features - Parent 3"
274
+ },
275
+ {
276
+ id: "123011.1237011",
277
+ parent_id: "123010.1237010",
278
+ title: "Bundler",
279
+ priority: 8
280
+ },
281
+ {
282
+ id: "123012.1237012",
283
+ parent_id: "123010.1237010",
284
+ title: "RedirectsHandler",
285
+ priority: 9
286
+ },
287
+ {
288
+ id: "123013.1237013",
289
+ parent_id: "123010.1237010",
290
+ title: "SSG",
291
+ priority: 10
292
+ },
293
+ {
294
+ id: "123014.1237014",
295
+ parent_id: "123010.1237010",
296
+ title: "SSR",
297
+ priority: 11
298
+ },
299
+ {
300
+ id: "123015.1237015",
301
+ parent_id: "123010.1237010",
302
+ title: "RouteHandler",
303
+ priority: 12
304
+ },
305
+ {
306
+ id: "123016.1237016",
307
+ parent_id: "123010.1237010",
308
+ title: "SitemapGenerator",
309
+ priority: 13
310
+ },
311
+ {
312
+ id: "123017.1237017",
313
+ parent_id: "123010.1237010",
314
+ title: "ImageHandler",
315
+ priority: 14
316
+ },
317
+ {
318
+ id: "123018.1237018",
319
+ parent_id: "123010.1237010",
320
+ title: "SSR-Menu",
321
+ priority: 15
322
+ },
323
+ ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gudhub/core",
3
- "version": "1.2.4-beta.37",
3
+ "version": "1.2.4-beta.38",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {