-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.js
81 lines (65 loc) · 1.74 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"use strict";
/**
* Add properties to all the objects, `Existing` as well as `To be created`.
* Avoid using this utility in bigger projects
* @memberof func
* @param {Object} obj - Object to properties
* @returns {Object} - Null Object
*/
let addGlobal = (AlterSet, obj)=>{
let x = AlterSet.prototype.__proto__;
Object.assign(x, obj, x);
x = null;
return null;
};
/**
* Convert JSON to Object
* @memberof func
* @param {String} json - JSON representation of the object to retrieve
* @return {Object} - JSON to Object
*/
let fromJson = (json)=>{
return JSON.parse(json);
};
/**
* Sort a given array (passed as argument)
* @param {Array} arr - Array to be sorted
* @param {String} [order=ASC] - Ascending `ASC` | Descending `DESC`
* @memberof func
* @returns {Array} - Sorted Array
*/
let sort = (arr, order = "ASC")=>{
if(typeof order !== "string"){
let e = new Error(`order should be 'string' got ${typeof order}`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}
if(order !== "ASC" && order !== "DESC"){
let e = new Error(`value of order should be 'ASC' or 'DESC' got ${order}`);
e.code = "ERR_INVALID_ARG_VALUE";
throw e;
}
let sortedArr = [];
arr = new Array(...arr);
switch(order){
case "ASC":
sortedArr = arr.sort((p, n)=>{
return p - n;
});
break;
case "DESC":
sortedArr = arr.sort((p, n)=>{
return n - p;
});
break;
}
return sortedArr;
};
//Export functions
module.exports = (AlterSet)=>{
return {
addGlobal: addGlobal.bind(addGlobal, AlterSet),
fromJson,
sort
};
};