-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprototypes.js
More file actions
325 lines (285 loc) · 9.42 KB
/
prototypes.js
File metadata and controls
325 lines (285 loc) · 9.42 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
"use strict";
const sortArray = require("./functions.js")().sort;
/**
* Add values of properties which are common between all objects, others remain as it is.
* @memberof AlterSet
* @instance
* @param {AlterSet[] | Object[]} arr - Array of objects to add properties of
* @returns {Object} - Values will be sum of individual values of passed object
*/
let addProps = function(...arr){
let f = Object.create(this);
return arr.reduce((flag, inst)=>{
if(inst instanceof Object){
Object.keys(inst).forEach((key)=>{
let unit = (typeof inst[key] === "number") ? 0 : "";
flag[key] = (flag[key] || unit) + inst[key];
});
return flag;
} else{
return flag;
}
}, f);
};
/**
* Subtract values of properties which are common between all objects, others remain as it is.
* @memberof AlterSet
* @instance
* @param {AlterSet[] | Object[]} arr - Array of objects to subtract properties of
* @returns {Object} - Values will be `Instance Value` - `Sum of passed object values` of individual values of passed object
*/
let subProps = function(...arr){
let f = Object.create(this);
return arr.reduce((flag, inst)=>{
if(inst instanceof Object){
Object.keys(inst).forEach((key)=>{
flag[key] = (flag[key] || 0) - inst[key];
});
return flag;
} else{
return flag;
}
}, f);
};
/**
* Create an object with values from existing object and properties specified in array.
* @memberof AlterSet
* @instance
* @param {Array} arr - Properties array.
* @param {Object} [options={unref:true, protoLookup:true}] - Object containing control params
* @param {Boolean} [options.unref=true] - `true` to reserve protochain
* @param {Boolean} [options.protoLookup=true] - `true` to look in protochain for value
* @return {Object} - An object with keys which are passed in array.
*/
let intersection = function(arr, options = {}){
if(typeof arr !== "object"){
let e = new Error(`arr should be 'object' got '${typeof options}'`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}
if(typeof options !== "object"){
let e = new Error(`options should be 'object' got '${typeof options}'`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}
let flag = new Object();
let flagObj = new (function(){});
//Specify default values;
options.unref = (typeof options.unref === "undefined") ? true : options.unref;
options.protoLookup = (typeof options.protoLookup === "undefined") ? true : options.protoLookup;
if(options.protoLookup === false){
flag = Object.assign({}, this);
} else if(options.protoLookup === true){
flag = Object.create(this);
} else{
let e = new Error(`options.protoLookup should be 'boolean' got '${typeof options.unref}'`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}
for(let i of arr){
if(typeof flag[i] !== "undefined"){
flagObj[i] = flag[i];
}
}
if(options.unref === true){
return Object.assign({}, flagObj);
} else if(options.unref === false){
flagObj.__proto__ = this.__proto__;
return flagObj;
} else{
let e = new Error(`options.unref should be 'boolean' got '${typeof options.unref}'`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}
};
/**
* Create an object with key, value pairs in both objects, overring with the pairs of passed object
* @memberof AlterSet
* @instance
* @param {AlterSet | Object} obj - Object to append key value paris of
* @param {Object} [options={unref:false}] - Object containing control params
* @param {Boolean} [options.unref=false] - `true` to create new object else modify `this`
* @returns {Object} - An concatinated object `Instance` U `Passed Object`
*/
let union = function(obj, options = {unref:false}){
if(typeof obj !== "object"){
let e = new Error(`obj should be 'object' got '${typeof options}'`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}
if(typeof options !== "object"){
let e = new Error(`options should be 'object' got '${typeof options}'`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}
let x;
if(options.unref === true){
let flag = Object.create(this);
x = Object.assign(flag, obj);
} else if(options.unref === false){
x = Object.assign(this, obj);
} else{
let e = new Error(`options.unref should be 'boolean' got '${typeof options.unref}'`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}
return x;
};
/**
* Return default value if property not found
* @memberof AlterSet
* @instance
* @param {String} key - Property to search for
* @param {*} [d] - Value to return in case of miss
* @returns {*} - Value against a Key in the object
*/
let get = function(key, d = null){
if(typeof key !== "string"){
let e = new Error(`key should be 'string' got '${typeof options.unref}'`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}
let flag = (this.hasOwnProperty(key)) ? this[key] : d;
return flag;
};
/**
* Get JSON representation of the object
* @memberof AlterSet
* @instance
* @returns {String} - Object to JSON
*/
let json = function(){
return JSON.stringify(this);
};
/**
* Intuitive way of checking key existence
* @memberof AlterSet
* @instance
* @param {String} key - Key to check existence of
* @returns {Boolean} - True/False
*/
let hasKey = function(key){
if(typeof key !== "string"){
let e = new Error(`key should be 'string' got ${typeof key}`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}
return this.hasOwnProperty(key);
};
/**
* Find keys having a certain value
* @memberof AlterSet
* @instance
* @param {String | Number} value - Value to search the keys for
* @param {Object} options - Object with control params
* @param {Boolean} options.strict - Flag to switch between strict/unstrict value matching
* @returns {Array} - Array of keys matching a certain value
*/
let withValue = function(value, options = {strict: false}){
if(typeof options !== "object"){
let e = new Error(`options should be'object' got ${typeof options}`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}
if(typeof options.strict !== "boolean"){
let e = new Error(`options.strict should be 'boolean' got ${typeof options.strict}`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}
if(typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean"){
let e = new Error(`value should be 'string | number | boolean' got ${typeof value}`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}
return Object.entries(this).reduce((arr, [key, v])=>{
if((v === value) || (options.strict === false && v == value)){
arr.push(key);
}
return arr;
}, []);
};
/**
* Get array of keys, in Ascending or Descending order of values
* @param {Object} [options] - Object containing control params
* @param {String} [options.order=ASC] - Ascending `ASC` or Descending `DESC`
* @param {String} [options.returns=keys] - Array of Keys || Value
* @memberof AlterSet
* @instance
* @returns {Array} - Either array of `keys` or `values` based on value of returns
*/
let sort = function(options = {}){
if(!options || typeof options !== "object"){
let e = new Error(`options should be 'object' got ${typeof options}`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}
let order = options.order || "ASC";
let returns = options.returns || "keys";
if(typeof order !== "string"){
let e = new Error(`order shoule be 'string' got ${typeof order}`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}
if(typeof returns !== "string"){
let e = new Error(`returns should be 'string' got ${typeof order}`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}
if(returns !== "keys" && returns !== "values"){
let e = new Error(`value of return should be 'keys' or 'values' got ${order}`);
e.code = "ERR_INVALID_ARG_VALUE";
throw e;
}
let values = Object.values(this);
let finalArr = [];
let sortedArr = sortArray(values, order);
if(returns === "values"){
return sortedArr;
}
for(let i in sortedArr){
let v = sortedArr[i];
if((typeof v === "string" || typeof v === "number" || typeof v === "boolean") && v !== sortedArr[i - 1]){
finalArr = finalArr.concat(this.withValue(v));
} else{
continue;
}
}
return finalArr;
};
/**
* Get key of maximum value
* @memberof AlterSet
* @instance
* @returns {Array} - Array of keys with the maximum value
*/
let getMax = function(){
let arr = sortArray(Object.values(this));
let maxVal = arr[arr.length - 1];
let maxKey = this.withValue(maxVal);
return maxKey;
};
/**
* Get key of minimum value
* @memberof AlterSet
* @instance
* @returns {Array} - Array of keys with the maximum value
*/
let getMin = function(){
let arr = sortArray(Object.values(this));
let minKey = this.withValue(arr[0]);
return minKey;
};
//Export functions
module.exports = {
addProps,
subProps,
intersection,
union,
get,
json,
hasKey,
withValue,
sort,
getMax,
getMin
};