"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/es5-ext/object/serialize.js" (11 Apr 2017, 1035 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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 };