1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | module.exports = class ArrayHelper {
|
7 |
|
8 | static includes () {
|
9 | return this.indexOf(...arguments) !== -1;
|
10 | }
|
11 |
|
12 | static indexOf (target, values) {
|
13 | if (!Array.isArray(values)) {
|
14 | return -1;
|
15 | }
|
16 | target = JSON.stringify(target);
|
17 | for (let i = 0; i < values.length; ++i) {
|
18 | if (target === JSON.stringify(values[i])) {
|
19 | return i;
|
20 | }
|
21 | }
|
22 | return -1;
|
23 | }
|
24 |
|
25 | static hasDiff (targets, sources, indexOf) {
|
26 | for (const source of sources) {
|
27 | const index = indexOf ? indexOf(source, targets) : targets.indexOf(source);
|
28 | if (index === -1) {
|
29 | return true;
|
30 | }
|
31 | }
|
32 | return false;
|
33 | }
|
34 |
|
35 | static diff (targets, sources, indexOf) {
|
36 | const result = this.exclude(sources, targets, indexOf);
|
37 | result.push(...this.exclude(targets, sources, indexOf));
|
38 | return result;
|
39 | }
|
40 |
|
41 | static exclude (targets, sources, indexOf) {
|
42 | const result = [];
|
43 | for (const source of sources) {
|
44 | const index = indexOf ? indexOf(source, targets) : targets.indexOf(source);
|
45 | if (index === -1) {
|
46 | result.push(source);
|
47 | }
|
48 | }
|
49 | return result;
|
50 | }
|
51 |
|
52 | static intersect (targets, sources, indexOf) {
|
53 | const result = [];
|
54 | for (const target of targets) {
|
55 | const index = indexOf ? indexOf(target, sources) : sources.indexOf(target);
|
56 | if (index !== -1) {
|
57 | result.push(target);
|
58 | }
|
59 | }
|
60 | return result;
|
61 | }
|
62 |
|
63 | static unique (values) {
|
64 | return Object.keys(this.flip(values));
|
65 | }
|
66 |
|
67 | static uniqueStrict (values, indexOf) {
|
68 | const result = [];
|
69 | for (let i = 0; i < values.length; ++i) {
|
70 | const index = indexOf ? indexOf(values[i], values) : values.indexOf(values[i]);
|
71 | if (index === i) {
|
72 | result.push(values[i]);
|
73 | }
|
74 | }
|
75 | return result;
|
76 | }
|
77 |
|
78 | static flip (values) {
|
79 | const result = {};
|
80 | if (Array.isArray(values)) {
|
81 | for (let i = 0; i < values.length; ++i) {
|
82 | result[values[i]] = i;
|
83 | }
|
84 | }
|
85 | return result;
|
86 | }
|
87 |
|
88 | static concat (values) {
|
89 | return Array.isArray(values) ? [].concat(...values) : values;
|
90 | }
|
91 |
|
92 | static join (values, separator = ', ') {
|
93 | return Array.isArray(values) ? values.join(separator) : values;
|
94 | }
|
95 |
|
96 | static remove (value, values) {
|
97 | if (!Array.isArray(values)) {
|
98 | return false;
|
99 | }
|
100 | value = values.indexOf(value);
|
101 | if (value === -1) {
|
102 | return false;
|
103 | }
|
104 | values.splice(value, 1);
|
105 | return true;
|
106 | }
|
107 |
|
108 | static getPropertyValues (key, items) {
|
109 | const values = [];
|
110 | if (Array.isArray(items)) {
|
111 | for (const item of items) {
|
112 | if (item && Object.prototype.hasOwnProperty.call(item, key)) {
|
113 | values.push(item[key]);
|
114 | }
|
115 | }
|
116 | }
|
117 | return values;
|
118 | }
|
119 |
|
120 | static searchByProperty (value, searchKey, items, returnKey) {
|
121 | if (Array.isArray(items)) {
|
122 | for (const item of items) {
|
123 | if (item && item[searchKey] === value) {
|
124 | return returnKey === undefined ? item : item[returnKey];
|
125 | }
|
126 | }
|
127 | }
|
128 | }
|
129 |
|
130 | static filterByClassProperty (items, Base, key = 'Class') {
|
131 | const result = [];
|
132 | if (Array.isArray(items)) {
|
133 | for (const item of items) {
|
134 | if (item && item[key] && (item[key] === Base || item[key].prototype instanceof Base)) {
|
135 | result.push(item);
|
136 | }
|
137 | }
|
138 | }
|
139 | return result;
|
140 | }
|
141 |
|
142 |
|
143 |
|
144 | static getRandom (values) {
|
145 | if (Array.isArray(values) && values.length) {
|
146 | return values[Math.floor(Math.random() * values.length)];
|
147 | }
|
148 | }
|
149 |
|
150 | static shuffle (values) {
|
151 | if (Array.isArray(values)) {
|
152 | let i = values.length;
|
153 | while (i) {
|
154 | const j = Math.floor((i--) * Math.random());
|
155 | const t = values[i];
|
156 | values[i] = values[j];
|
157 | values[j] = t;
|
158 | }
|
159 | return values;
|
160 | }
|
161 | }
|
162 |
|
163 |
|
164 |
|
165 | static sortHierarchy (items, keyProperty, parentProperty) {
|
166 | let result = [], data = {};
|
167 | for (const item of items) {
|
168 | if (!item[parentProperty]) {
|
169 | result.push(item);
|
170 | } else if (Array.isArray(data[item[parentProperty]])) {
|
171 | data[item[parentProperty]].push(item);
|
172 | } else {
|
173 | data[item[parentProperty]] = [item];
|
174 | }
|
175 | }
|
176 | items = result;
|
177 | while (items.length) {
|
178 | const children = [];
|
179 | for (const item of items) {
|
180 | const key = item[keyProperty];
|
181 | if (Array.isArray(data[key])) {
|
182 | children.push(...data[key]);
|
183 | result.push(...data[key]);
|
184 | delete data[key];
|
185 | }
|
186 | }
|
187 | items = children;
|
188 | }
|
189 | return result;
|
190 | }
|
191 | }; |
\ | No newline at end of file |