Total Complexity | 9 |
Complexity/F | 2.25 |
Lines of Code | 43 |
Function Count | 4 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 1 |
1 | export default { |
||
2 | /** |
||
3 | * Sort an array of objects by one prop of objects |
||
4 | * @param {Array} data |
||
5 | * @param {String} prop |
||
6 | * @param {String} direction defines if sort should be asc or desc |
||
7 | */ |
||
8 | sortByObjectKey: function (data, prop, direction = "asc") { |
||
9 | if (["asc", "desc"].indexOf(direction) === -1) { |
||
10 | throw new Error("Direction should be asc or desc"); |
||
11 | } |
||
12 | |||
13 | return data.sort((a, b) => { |
||
14 | let response = 0, |
||
15 | ap = a[prop], |
||
16 | bp = b[prop]; |
||
17 | |||
18 | if (ap < bp) { |
||
19 | response = direction === "asc" ? -1 : 1; |
||
20 | } else if (ap > bp) { |
||
21 | response = direction === "asc" ? 1 : -1; |
||
22 | } |
||
23 | |||
24 | return response; |
||
25 | }); |
||
26 | }, |
||
27 | |||
28 | /** |
||
29 | * Filter objects from array that has key value in values |
||
30 | * Example: |
||
31 | * var arr = [{a: 1}, {a: 2}, {a: 3}] |
||
32 | * App.helpers.array.filterBy("a", [1, 3], arr); |
||
33 | * |
||
34 | * The output will be: |
||
35 | * [{a: 1}, {a: 3}] |
||
36 | * @param key |
||
37 | * @param values |
||
38 | * @param items |
||
39 | */ |
||
40 | filterBy: function (key, values, items) { |
||
41 | return items.filter(item => values.indexOf(item[key]) > -1); |
||
42 | } |
||
43 | } |
||
44 |