-
Notifications
You must be signed in to change notification settings - Fork 1
/
get.js
182 lines (168 loc) · 4.33 KB
/
get.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
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
'use strict';
/* **
* @description A minimalist port of `lodash.get`
* without the dependency of `lodash.get` & `lodash.memoize`
*/
const EVENT_TYPES = {
DATA_MISSING: 'dataMissing',
TYPE_MISMATCH: 'typeMismatch'
};
let logger;
/**
* Checks if an object is truly empty
* Using Object.enrties().length breaks for older browsers on older
* versions of iOS.
* @param {Object} obj
* @returns {Boolean}
*/
function isEmpty(obj) {
if (obj) {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
return false;
}
}
return true;
}
}
/**
* Retrieve Object Types
* @param {*} val
* @returns {*} type
*/
function getType(val) {
let type;
if (Array.isArray(val)) {
type = 'array';
} else if (val === null) {
type = 'null';
} else {
type = typeof val;
}
return type;
}
/**
* Normalizes the path String sent, to array types
* @param {String} path
* @returns {String}
*/
function normalize(path) {
if (typeof path === 'string') {
path = path.split('.').join(',').replace(/\[\d\]/g, (match) => {
match = match.replace(/[\[\]']+/g, '');
match = `,${match}`;
return match;
}).split(',');
}
return path;
}
/**
* Log the event
* @param path - a string representation of the lookup
* @param eventType - event type from EVENT_TYPES enum
* @param defaultValue - default when data is absent, also used to check type
* @param logType - logger method to use
*/
function log(eventType, path, defaultValue, logType) {
if (logger[logType]) {
logger[logType]('event: %s, path: %s, default: %s', eventType, path, defaultValue);
}
}
/**
* Claws through Nested Data
* @param {Object} obj
* @param {Array} path
* @returns {*}
*/
function claw(obj, path) {
return path.reduce((acc, currVal) => (acc && typeof acc[currVal] !== 'undefined') ? acc[currVal] : undefined, obj);
}
/**
* Access Nested Data safely
* @param {Object} obj
* @param {String} path
* @param {*} defaultVal
* @param {String} logType - WARN | INFO | DEBUG | ERROR | LOG
* @returns {*} result
*/
function access(obj, path, defaultVal, logType = 'warn') {
let eventType;
path = normalize(path);
let result = claw(obj, path);
const typeofResult = getType(result);
let typeofDefaultValue = getType(defaultVal);
if (typeofDefaultValue === 'undefined') {
defaultVal = '';
typeofDefaultValue = 'string';
} else if (typeofDefaultValue === 'object' && isEmpty(defaultVal)) {
defaultVal = {
__isEmpty: true
};
}
if (typeofResult !== 'undefined') {
if (typeofResult !== typeofDefaultValue) {
eventType = EVENT_TYPES.TYPE_MISMATCH;
result = defaultVal;
}
} else {
eventType = EVENT_TYPES.DATA_MISSING;
result = defaultVal;
}
if (logger && logType && eventType) {
log(eventType, path, defaultVal, logType);
}
return result;
}
/**
* Checks if an object has a specific path
* Does not return a value
* @param {Object} obj
* @param {String} path
* @returns {Boolean} result
*/
function has(obj, path) {
path = normalize(path);
let result = claw(obj, path);
const typeofResult = getType(result);
result = !(typeofResult === 'undefined' || typeofResult === 'null');
return result;
}
/**
* Exported Function to claw through nested data safely
* Just returns the value without logging
* @param {Object} obj
* @param {String} path
* @param {*} defaultVal - if nothing provided, defaulst to ''
* @returns {*} result
*/
function get(obj, path, defaultVal) {
return access(obj, path, defaultVal);
}
/**
* Exported Function to claw through nested data safely
* Logs the value & returns it
* @param {Object} obj
* @param {String} path
* @param {*} defaultVal - if nothing provided, defaulst to ''
* @param {String} logType - WARN | INFO | DEBUG | ERROR | LOG
* @returns {*} result
*/
function need(obj, path, defaultVal, logType) {
return access(obj, path, defaultVal, logType);
}
/**
* Set logger to be used for all future usage
* @param object l - the logger with a warn function
*/
function setLogger(l) {
logger = l;
}
module.exports = {
has,
get,
need,
setLogger
};
module.exports.privates = {
EVENT_TYPES: EVENT_TYPES
};