-
Notifications
You must be signed in to change notification settings - Fork 627
/
Copy pathfunctions.js
41 lines (36 loc) · 1.15 KB
/
functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// @flow
exports.dynamicSort = function(property: string) : Function {
var sortOrder = 1;
if (property[0] === '-') {
sortOrder = -1;
property = property.substr(1);
}
return function(a, b) {
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
};
};
exports.removeObjectfromArray = function(array: Array<Object>, key: string, value: string | number) : Array<Object> {
return array.filter((el) => el[key] !== value);
};
exports.changeObjectinArray = function(array: Array<Object>, key: string, oldValue: string | number, newValue: string | number) : Array<Object> {
array.forEach((item) => {
if (item[key] === oldValue) {
item[key] = newValue;
}
});
return array;
};
exports.moveObjectinArray = function(array: Array<Object>, key: string, step: number) : Array<Object> {
let index = array.map((item) => item.symbol).indexOf(key);
let value = array[index];
let newPos = index + step;
if (newPos < 0) {
newPos = 0;
} else if (newPos > array.length) {
newPos = array.length;
}
array.splice(index,1);
array.splice(newPos, 0, value);
return array;
};