3 var toArray = require('./to-array')
4 , isDate = require('../date/is-date')
5 , isRegExp = require('../reg-exp/is-reg-exp')
7 , isArray = Array.isArray, stringify = JSON.stringify
8 , keyValueToString = function (value, key) { return stringify(key) + ':' + exports(value); };
10 var sparseMap = function (arr) {
11 var i, l = arr.length, result = new Array(l);
12 for (i = 0; i < l; ++i) {
13 if (!arr.hasOwnProperty(i)) continue;
14 result[i] = exports(arr[i]);
19 module.exports = exports = function (obj) {
20 if (obj == null) return String(obj);
23 return stringify(obj);
29 if (isArray(obj)) return '[' + sparseMap(obj) + ']';
30 if (isRegExp(obj)) return String(obj);
31 if (isDate(obj)) return 'new Date(' + obj.valueOf() + ')';
32 return '{' + toArray(obj, keyValueToString) + '}';
34 throw new TypeError("Serialization of " + String(obj) + "is unsupported");