-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (62 loc) · 2.03 KB
/
index.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
'use strict';
/**
* Verifies that arguments passed to be evaluated are of correct types.
* @param {object} objectToTest Object which is to be tested to ensure needed props are st
* @param {string|array} propsToIgnore property(ies) of Object to be tested that should be ignored
* @return {bool} result of performing set property checks on the object to be tested
*/
function areObjectPropsSet(objectToTest, propsToIgnore){
if (typeof objectToTest === 'object' && !Array.isArray(objectToTest)) {
if (typeof propsToIgnore === 'undefined') {
return allPropSet(objectToTest);
}else {
if(typeof propsToIgnore === 'string' || Array.isArray(propsToIgnore)){
return somePropSet(objectToTest, propsToIgnore);
}else{
throw new Error('Object properties to ignore must be a string or array.');
}
}
}else{
throw new Error('Argument passed is not an object.');
}
}
/**
* Check that all properties of the object are set
* @param {object} objectToTest
* @return {bool}
*/
function allPropSet(objectToTest) {
let isPropSet = false;
for (let property in objectToTest) {
if (objectToTest[property] !== null && objectToTest !== undefined) {
isPropSet = true;
}else{
//property is null or undefined. kill execution
return false;
}
}
//all props are set, carry on
return isPropSet;
}
/**
* Check that properties of the object are set while ignoring those that should be ignored
* @param {object} objectToTest
* @param {string | array} propsToIgnore
* @return {bool}
*/
function somePropSet(objectToTest, propsToIgnore) {
let isPropSet = false;
for (let property in objectToTest) {
//move on to checking next property if current one is to be ignored
if (property === propsToIgnore || propsToIgnore.indexOf(property) !== -1) continue;
if (objectToTest[property] !== null && objectToTest !== undefined) {
isPropSet = true;
}else{
//property is null or undefined. kill execution
return false;
}
}
//all props are set, carry on
return isPropSet;
}
module.exports = areObjectPropsSet;