"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/es5-ext/object/serialize.js" (25 Apr 2018, 1181 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   , isValue  = require("../object/is-value")
    6   , isRegExp = require("../reg-exp/is-reg-exp");
    7 
    8 var isArray = Array.isArray
    9   , stringify = JSON.stringify
   10   , objHasOwnProperty = Object.prototype.hasOwnProperty;
   11 var keyValueToString = function (value, key) {
   12     return stringify(key) + ":" + exports(value);
   13 };
   14 
   15 var sparseMap = function (arr) {
   16     var i, length = arr.length, result = new Array(length);
   17     for (i = 0; i < length; ++i) {
   18         if (!objHasOwnProperty.call(arr, i)) continue;
   19         result[i] = exports(arr[i]);
   20     }
   21     return result;
   22 };
   23 
   24 module.exports = exports = function (obj) {
   25     if (!isValue(obj)) return String(obj);
   26     switch (typeof obj) {
   27         case "string":
   28             return stringify(obj);
   29         case "number":
   30         case "boolean":
   31         case "function":
   32             return String(obj);
   33         case "object":
   34             if (isArray(obj)) return "[" + sparseMap(obj) + "]";
   35             if (isRegExp(obj)) return String(obj);
   36             if (isDate(obj)) return "new Date(" + obj.valueOf() + ")";
   37             return "{" + toArray(obj, keyValueToString) + "}";
   38         default:
   39             throw new TypeError("Serialization of " + String(obj) + "is unsupported");
   40     }
   41 };