Completed
Push — master ( e65e46...a3e79e )
by Pieter Epeüs
32s queued 11s
created

Arr.update   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
1
import multisort from './modules/multisort';
2
import multifilter from './modules/multifilter';
3
import multikey from './modules/multikey';
4
import intersect from './modules/intersect';
5
import min from './modules/min';
6
import max from './modules/max';
7
import diff from './modules/diff';
8
import unique from './modules/unique';
9
import summ from './modules/summ';
10
import average from './modules/average';
11
import random from './modules/random';
12
import getByKey from './modules/getByKey';
13
import first from './modules/first';
14
import last from './modules/last';
15
import update from './modules/update';
16
17
/**
18
 * Object helper
19
 */
20
class Arr extends Array {
21
    static get [Symbol.species]() {
22
        return Array;
23
    }
24
25
    /**
26
     * Sort a multiarray.
27
     *
28
     * @param {string} key
29
     * @param {string} direction
30
     *
31
     * @return {array}
32
     */
33
    multisort(key, direction) {
34
        return multisort(this[0], key, direction);
35
    }
36
37
    /**
38
     * Filter a multi array.
39
     *
40
     * @param {string}  key
41
     * @param {string}  find
42
     * @param {boolean} operator
43
     *
44
     * @return {array}
45
     */
46
    multifilter(key, find, operator) {
47
        return multifilter(this[0], key, find, operator);
48
    }
49
50
    /**
51
     * Only get some keys of a multi array.
52
     *
53
     * @param {string} key
54
     *
55
     * @return {array}
56
     */
57
    multikey(key) {
58
        return multikey(this[0], key);
59
    }
60
61
    /**
62
     * Get the intersection of arrays.
63
     *
64
     * @param {string}  array
65
     * @param {boolean} multi
66
     *
67
     * @return {array}
68
     */
69
    intersect(array, multi) {
70
        return intersect(this[0], array, multi);
71
    }
72
73
    /**
74
     * Get the difference of arrays.
75
     *
76
     * @param {string}  array
77
     * @param {boolean} total
78
     *
79
     * @return {array}
80
     */
81
    diff(array, total) {
82
        return diff(this[0], array, total);
83
    }
84
85
    /**
86
     * Get the unique values of an array.
87
     *
88
     * @return {array}
89
     */
90
    get unique() {
91
        return unique(this[0]);
92
    }
93
94
    /**
95
     * Only add the value if the value isnt in the array.
96
     *
97
     * @param {string} newValue
98
     *
99
     * @return {int}
100
     */
101
    pushIfNotExists(newValue) {
102
        if (this.indexOf(newValue) < 0) {
103
            this.push(newValue);
104
        }
105
106
        return this.length;
107
    }
108
109
    /**
110
     * Add multiple values to an array.
111
     *
112
     * @param {array} newValues
113
     *
114
     * @return {int}
115
     */
116
    pushMultiple(newValues) {
117
        this.push(...newValues);
118
119
        return this.length;
120
    }
121
122
    /**
123
     * Add multiple values to an array.
124
     * Only add the value if the value isnt in the array.
125
     *
126
     * @param {array} newValues
127
     *
128
     * @return {int}
129
     */
130
    pushMultipleIfNotExists(newValues) {
131
        const array = this;
132
133
        newValues.forEach((value) => {
134
            array.pushIfNotExists(value);
135
        });
136
137
        return array.length;
138
    }
139
140
    /**
141
     * The largest of the given numbers.
142
     * If at least one of the arguments cannot be converted to a number,
143
     * NaN is returned.
144
     *
145
     * @return {int}
146
     */
147
    get max() {
148
        return max(this[0]);
149
    }
150
151
    /**
152
     * The smallest of the given numbers.
153
     * If at least one of the arguments cannot be converted to a number,
154
     * NaN is returned.
155
     *
156
     * @return {int}
157
     */
158
    get min() {
159
        return min(this[0]);
160
    }
161
162
    /**
163
     * Get a random value of an array.
164
     *
165
     * @return {string}
166
     */
167
    get random() {
168
        return random(this);
169
    }
170
171
    /**
172
     * The summ of all values.
173
     *
174
     * @return {int}
175
     */
176
    get summ() {
177
        return summ(this[0]);
178
    }
179
180
    /**
181
     * Get the average of all values.
182
     *
183
     * @return {int}
184
     */
185
    get average() {
186
        return average(this[0]);
187
    }
188
189
    /**
190
     * Javascript implementation of Arr::get
191
     *
192
     * @param {string} key
193
     * @param {object|null} defaultValue
194
     *
195
     * @return {object|null}
196
     */
197
    getByKey(key, defaultValue) {
198
        return getByKey(this[0], key, defaultValue);
199
    }
200
201
    /**
202
     * Javascript implementation of Arr::first
203
     *
204
     * @return {object|null}
205
     */
206
    get first() {
207
        return first(this[0]);
208
    }
209
210
    /**
211
     * Javascript implementation of Arr::last
212
     *
213
     * @return {object|null}
214
     */
215
    get last() {
216
        return last(this[0]);
217
    }
218
219
    /**
220
     * Update multiple items in an array
221
     *
222
     * @param {array} newValues
223
     * @param {array} keys
224
     *
225
     * @return {array}
226
     */
227
    update(newValues, keys) {
228
        return update(this[0], newValues, keys);
229
    }
230
}
231
232
export {
233
    Arr,
234
    multisort,
235
    multifilter,
236
    multikey,
237
    intersect,
238
    min,
239
    max,
240
    diff,
241
    unique,
242
    summ,
243
    average,
244
    random,
245
    getByKey,
246
    first,
247
    last,
248
    update,
249
};
250