-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathhandler.js
209 lines (189 loc) · 5.48 KB
/
handler.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
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
/* jshint node:true */
'use strict';
const helper = require('../core/helper');
class Handler {
constructor(options) {
this._options = options;
// an object of {typeName:(value,index,parent)=>any}
this._options.typeHandlers = this._options.typeHandlers || {};
}
/**
* Check if results needing mapping to alternate value
*
* @returns [{item, value}] result
*/
_setHeaders(result, item) {
let self = this;
if (!item) return result;
return result.map(function(element) {
element.item = element.item ? item + self._options.headerPathString + element.item : item;
return element;
});
}
castValue(element, item, index, parent){
//cast by matching constructor
const types = this._options.typeHandlers;
for (let type in types ) {
if( isInstanceOfTypeName(element,type) ){
element = types[type].call(types, element, index, parent);
break;//first match we move on
}
}
return element;
}
checkComplex(element, item){
//Check if element is a Date
if (helper.isDate(element)) {
return [{
item: item,
value: (this._options.handleDate || this._handleDate)(element, item),
}];
}
//Check if element is an Array
else if (helper.isArray(element)) {
var resultArray = this._handleArray(element, item);
return this._setHeaders(resultArray, item);
}
//Check if element is a Object
else if (helper.isObject(element)) {
var resultObject = this._handleObject(element);
return this._setHeaders(resultObject, item);
}
return [{
item: item,
value: '',
}];
}
/**
* Check the element type of the element call the correct handle function
*
* @param element Element that will be checked
* @param item Used to make the headers/path breadcrumb
* @returns [{item, value}] result
*/
check(element, item, index, parent) {
element = this.castValue(element, item, index, parent);
// try simple value by highier performance switch
switch(typeof element){
case 'string':
return [{
item: item,
value: this._handleString(element, item),
}];
case 'number':
return [{
item: item,
value: this._handleNumber(element, item),
}];
case 'boolean':
return [{
item: item,
value: this._handleBoolean.bind(this)(element, item),
}];
}
return this.checkComplex(element, item);
}
/**
* Handle all Objects
*
* @param {Object} obj
* @returns [{item, value}] result
*/
_handleObject(obj) {
var result = [];
//Look every object props
for (var prop in obj) {
var propData = obj[prop];
//Check the propData type
var resultCheckType = this.check(propData, prop, prop, obj);
//Append to results aka merge results aka array-append-array
result = result.concat(resultCheckType);
}
return result;
}
/**
* Handle all Arrays, merges arrays with primitive types in a single value
*
* @param {Array} array
* @returns [{item, value}] result
*/
_handleArray(array) {
let self = this;
let result = [];
var firstElementWithoutItem;
for (let aIndex=0; aIndex < array.length; ++aIndex) {
let element = array[aIndex];
//Check the propData type
var resultCheckType = self.check(element, null, aIndex, array);
//Check for results without itens, merge all itens with the first occurrence
if (resultCheckType.length === 0) continue;
var firstResult = resultCheckType[0];
if (!firstResult.item && firstElementWithoutItem !== undefined) {
firstElementWithoutItem.value += self._options.arrayPathString + firstResult.value;
continue;
} else if (resultCheckType.length > 0 && !firstResult.item && firstElementWithoutItem === undefined) {
firstElementWithoutItem = firstResult;
}
//Append to results
result = result.concat(resultCheckType);
}
return result;
}
/**
* Handle all Boolean variables, can be replaced with options.handleBoolean
*
* @param {Boolean} boolean
* @returns {String} result
*/
_handleBoolean(boolean) {
var result;
//Check for booolean options
if (boolean) {
result = this._options.booleanTrueString || 'true';
} else {
result = this._options.booleanFalseString || 'false';
}
return result;
}
/**
* Handle all String variables, can be replaced with options.handleString
*
* @param {String} string
* @returns {String} string
*/
_handleString(string) {
return string;
}
/**
* Handle all Number variables, can be replaced with options.handleNumber
*
* @param {Number} number
* @returns {Number} number
*/
_handleNumber(number) {
return number;
}
/**
* Handle all Date variables, can be replaced with options.handleDate
*
* @param {Date} number
* @returns {string} result
*/
_handleDate(date) {
return date.toLocaleDateString();
}
}
module.exports = Handler;
const globalScope = typeof(window)==="undefined" ? global : window;
function isInstanceOfTypeName(element, typeName){
if( element instanceof globalScope[typeName] ){
return true;//Buffer and complex objects
}
//literals in javascript cannot be checked by instance of
switch( typeof(element) ){
case 'string':return typeName==="String";
case 'boolean':return typeName==="Boolean";
case 'number':return typeName==="Number";
}
return false;
}