-
Notifications
You must be signed in to change notification settings - Fork 10
/
propertyFormatter.js
64 lines (56 loc) · 2.57 KB
/
propertyFormatter.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
var propertyFormatter = (function() {
"use strict";
var extractDataPropertiesForDisplayAsArray = function(source, ignoreId) {
var propertiesForDisplay = [];
if (_.isNull(source) || _.isUndefined(source) || _.isEmpty(source) || (!ignoreId && !_.isNumber(source.id))) {
return propertiesForDisplay;
}
_.each(_.pairs(source), function(keyValue) {
var key = keyValue[0];
var value = keyValue[1];
if (_.isDate(value) || _.isBoolean(value) || _.isNumber(value) || _.isString(value)) {
propertiesForDisplay.push("Property '" + key + "' of type: " + typeof value + " has value: " + value);
} else if (!_.isFunction(value)) {
propertiesForDisplay.push("Property: " + keyValue[0] + " cannot be displayed.");
}
});
return propertiesForDisplay;
};
var extractDataPropertiesForDisplayForAnyObject = _.partial(extractDataPropertiesForDisplayAsArray, _, true);
return {
extractPropertiesForDisplayAsArray: function(source, ignoreId) {
var propertiesForDisplay = [];
if (_.isNull(source) || _.isUndefined(source) || _.isEmpty(source) || (!ignoreId && !_.isNumber(source.id))) {
return propertiesForDisplay;
}
_.each(_.pairs(source), function(keyValue) {
var key = keyValue[0];
var value = keyValue[1];
if (_.isDate(value) || _.isBoolean(value) || _.isNumber(value) || _.isString(value)) {
propertiesForDisplay.push("Property '" + key + "' of type: " + typeof value + " has value: " + value);
} else {
propertiesForDisplay.push("Property '" + key + "' cannot be displayed.");
}
});
return propertiesForDisplay;
},
extractDataPropertiesForDisplayAsArray: extractDataPropertiesForDisplayAsArray,
extractDataPropertiesForDisplayForAnyObject: extractDataPropertiesForDisplayForAnyObject,
extractPropertiesForDisplayAsString: function(source, ignoreId) {
if (_.isNull(source) || _.isUndefined(source) || _.isEmpty(source) || (!ignoreId && !_.isNumber(source.id))) {
return [];
}
return _.reduce(source, function(memo, value, key) {
if (memo && memo !== "") {
memo += "<br/>";
}
var isDate = typeof value === 'object' && value instanceof Date;
if (_.isDate(value) || _.isBoolean(value) || _.isNumber(value) || _.isString(value)) {
return memo + "Property: " + key + " of type: " + typeof value + " has value: " + value;
}
return memo + "Property: " + key + " cannot be displayed.";
},
"");
}
};
}());