]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/vim-mode/node_modules/grim/node_modules/emissary/node_modules/es6-weak-map/node_modules/es5-ext/object/serialize.js
8113b6801dd395470c9428736cf39f4cc4747c52
[rbdr/dotfiles] / atom / packages / vim-mode / node_modules / grim / node_modules / emissary / node_modules / es6-weak-map / node_modules / es5-ext / object / serialize.js
1 'use strict';
2
3 var toArray = require('./to-array')
4 , isDate = require('../date/is-date')
5 , isRegExp = require('../reg-exp/is-reg-exp')
6
7 , isArray = Array.isArray, stringify = JSON.stringify
8 , keyValueToString = function (value, key) { return stringify(key) + ':' + exports(value); };
9
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]);
15 }
16 return result;
17 };
18
19 module.exports = exports = function (obj) {
20 if (obj == null) return String(obj);
21 switch (typeof obj) {
22 case 'string':
23 return stringify(obj);
24 case 'number':
25 case 'boolean':
26 case 'function':
27 return String(obj);
28 case 'object':
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) + '}';
33 default:
34 throw new TypeError("Serialization of " + String(obj) + "is unsupported");
35 }
36 };