-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
73 lines (71 loc) · 1.61 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
var objToString = Object.prototype.toString;
var objKeys = Object.keys || function(obj) {
var keys = [];
for (var name in obj) {
keys.push(name);
}
return keys;
};
function stringify(val, isArrayProp) {
var i, max, str, keys, key, propVal, toStr;
if (val === true) {
return "true";
}
if (val === false) {
return "false";
}
switch (typeof val) {
case "object":
if (val === null) {
return null;
} else if (val.toJSON && typeof val.toJSON === "function") {
return stringify(val.toJSON(), isArrayProp);
} else {
toStr = objToString.call(val);
if (toStr === "[object Array]") {
str = '[';
max = val.length - 1;
for(i = 0; i < max; i++) {
str += stringify(val[i], true) + ',';
}
if (max > -1) {
str += stringify(val[i], true);
}
return str + ']';
} else if (toStr === "[object Object]") {
// only object is left
keys = objKeys(val).sort();
max = keys.length;
str = "";
i = 0;
while (i < max) {
key = keys[i];
propVal = stringify(val[key], false);
if (propVal !== undefined) {
if (str) {
str += ',';
}
str += JSON.stringify(key) + ':' + propVal;
}
i++;
}
return '{' + str + '}';
} else {
return JSON.stringify(val);
}
}
case "function":
case "undefined":
return isArrayProp ? null : undefined;
case "string":
return JSON.stringify(val);
default:
return isFinite(val) ? val : null;
}
}
module.exports = function(val) {
var returnVal = stringify(val, false);
if (returnVal !== undefined) {
return ''+ returnVal;
}
};