@gudhub/core 1.0.39 → 1.0.40

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.
@@ -127,8 +127,8 @@ export class Utils {
127
127
  return mergeObjects(target, source, optionsArgument);
128
128
  }
129
129
 
130
- makeNestedList(arr, id, parent_id, children_property) {
131
- return makeNestedList(arr, id, parent_id, children_property);
130
+ makeNestedList(arr, id, parent_id, children_property, priority_property) {
131
+ return makeNestedList(arr, id, parent_id, children_property, priority_property);
132
132
  }
133
133
 
134
134
  mergeChunks(chunks) {
@@ -1,6 +1,7 @@
1
- export function makeNestedList(arr, id, parent_id, children_property) {
1
+ export function makeNestedList(arr, id, parent_id, children_property, priority_property) {
2
2
  let array = JSON.parse(JSON.stringify(arr));
3
3
  let children = Boolean(children_property) ? children_property : 'children';
4
+ let priority = Boolean(priority_property) ? priority_property : false;
4
5
  let i;
5
6
  for (i = 0; i < array.length; i++) {
6
7
  if (array[i][parent_id] && array[i][parent_id] !== 0) {
@@ -34,5 +35,22 @@ export function makeNestedList(arr, id, parent_id, children_property) {
34
35
  });
35
36
  }
36
37
 
38
+ function sortChildrensByPriority(unsorted_array) {
39
+ unsorted_array.forEach(item => {
40
+ if(item[children]) {
41
+ item[children].sort((a, b) => a[priority] - b[priority]);
42
+ item[children].forEach(child => {
43
+ if(child[children]) {
44
+ sortChildrensByPriority(child[children]);
45
+ }
46
+ })
47
+ }
48
+ });
49
+ }
50
+
51
+ if(priority) {
52
+ sortChildrensByPriority(array);
53
+ }
54
+
37
55
  return array;
38
56
  }
@@ -19,6 +19,11 @@ describe("NESTED LIST", function () {
19
19
  nestedList[0].custom_children[0].should.have.property('custom_children');
20
20
  })
21
21
 
22
+ it('Should sort nested list childrens by priority', function () {
23
+ let nestedList = gudhub.makeNestedList(complicatedInput, 'id', 'parent_id', 'custom_children_array_name', 'priority');
24
+ nestedList[0].custom_children_array_name[1].title.should.equal('Child 1');
25
+ })
26
+
22
27
  });
23
28
 
24
29
  let input = [
@@ -67,4 +72,77 @@ let input = [
67
72
  id: 9,
68
73
  parent_id: 8
69
74
  },
70
- ];
75
+ ];
76
+
77
+ let complicatedInput = [
78
+ {
79
+ id: "26553.2884361",
80
+ parent_id: "26553.2904452",
81
+ title: "Second parent",
82
+ priority: 3
83
+ },
84
+ {
85
+ id: "26553.2904452",
86
+ parent_id: "",
87
+ title: "Parent"
88
+ },
89
+ {
90
+ id: "26553.2904533",
91
+ parent_id: "26553.2904452",
92
+ title: "Child 2",
93
+ priority: 5
94
+ },
95
+ {
96
+ id: "26553.2904534",
97
+ parent_id: "26553.2904452",
98
+ title: "Child 3",
99
+ priority: 4
100
+ },
101
+ {
102
+ id: "26553.2904535",
103
+ parent_id: "26553.2884361",
104
+ title: "Child of child 1"
105
+ },
106
+ {
107
+ id: "26553.2904536",
108
+ parent_id: "26553.2884361",
109
+ title: "Child of child 2"
110
+ },
111
+ {
112
+ id: "26553.2904538",
113
+ parent_id: "26553.2884361",
114
+ title: "Third parent"
115
+ },
116
+ {
117
+ id: "26553.2904539",
118
+ parent_id: "26553.2904538",
119
+ title: "Child of child of child"
120
+ },
121
+ {
122
+ id: "26553.2904540",
123
+ parent_id: "",
124
+ title: "Another parent"
125
+ },
126
+ {
127
+ id: "26553.2906081",
128
+ parent_id: "26553.2904452",
129
+ title: "Child 1",
130
+ priority: 2
131
+ },
132
+ {
133
+ id: "26553.2914316",
134
+ parent_id: "26553.2904540",
135
+ title: "Child of another parent"
136
+ },
137
+ {
138
+ id: "26553.2914491",
139
+ parent_id: "26553.2904452",
140
+ title: "Child of first parent",
141
+ priority: 1
142
+ },
143
+ {
144
+ id: "26553.2914500",
145
+ parent_id: "26553.2884361",
146
+ title: "Child of second parent"
147
+ }
148
+ ]
package/GUDHUB/gudhub.js CHANGED
@@ -216,8 +216,8 @@ export class GudHub {
216
216
  return this.util.mergeObjects(sourceObject, destinationObject);
217
217
  }
218
218
 
219
- makeNestedList(arr, id, parent_id, children_property) {
220
- return this.util.makeNestedList(arr, id, parent_id, children_property);
219
+ makeNestedList(arr, id, parent_id, children_property, priority_property) {
220
+ return this.util.makeNestedList(arr, id, parent_id, children_property, priority_property);
221
221
  }
222
222
 
223
223
  jsonConstructor(scheme, items, variables){