"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/cson-parser/lib/stringify.js" (8 Mar 2017, 4990 Bytes) of archive /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 // Generated by CoffeeScript 1.9.0
    2 
    3 /*
    4 Copyright (c) 2014, Groupon, Inc.
    5 All rights reserved.
    6 
    7 Redistribution and use in source and binary forms, with or without
    8 modification, are permitted provided that the following conditions
    9 are met:
   10 
   11 Redistributions of source code must retain the above copyright notice,
   12 this list of conditions and the following disclaimer.
   13 
   14 Redistributions in binary form must reproduce the above copyright
   15 notice, this list of conditions and the following disclaimer in the
   16 documentation and/or other materials provided with the distribution.
   17 
   18 Neither the name of GROUPON nor the names of its contributors may be
   19 used to endorse or promote products derived from this software without
   20 specific prior written permission.
   21 
   22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
   23 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   24 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
   25 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   26 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   27 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
   28 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   30 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   31 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   33  */
   34 var SPACES, isObject, jsIdentifierRE, newlineWrap, tripleQuotesRE,
   35   __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
   36 
   37 jsIdentifierRE = /^[a-z_$][a-z0-9_$]*$/i;
   38 
   39 tripleQuotesRE = new RegExp("'''", 'g');
   40 
   41 SPACES = '          ';
   42 
   43 newlineWrap = function(str) {
   44   return str && ("\n" + str + "\n");
   45 };
   46 
   47 isObject = function(obj) {
   48   return typeof obj === 'object' && obj !== null && !Array.isArray(obj);
   49 };
   50 
   51 module.exports = function(data, visitor, indent) {
   52   var indentLine, indentLines, n, normalized, out, visitArray, visitNode, visitObject, visitString, _ref;
   53   if ((_ref = typeof data) === 'undefined' || _ref === 'function') {
   54     return void 0;
   55   }
   56   indent = (function() {
   57     switch (typeof indent) {
   58       case 'string':
   59         return indent.slice(0, 10);
   60       case 'number':
   61         n = Math.min(10, Math.floor(indent));
   62         if (__indexOf.call([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], n) < 0) {
   63           n = 0;
   64         }
   65         return SPACES.slice(0, n);
   66       default:
   67         return 0;
   68     }
   69   })();
   70   if (!indent) {
   71     return JSON.stringify(data, visitor, indent);
   72   }
   73   indentLine = function(line) {
   74     return indent + line;
   75   };
   76   indentLines = function(str) {
   77     if (str === '') {
   78       return str;
   79     }
   80     return str.split('\n').map(indentLine).join('\n');
   81   };
   82   normalized = JSON.parse(JSON.stringify(data, visitor));
   83   visitString = function(str) {
   84     var string;
   85     if (str.indexOf('\n') === -1) {
   86       return JSON.stringify(str);
   87     } else {
   88       string = str.replace(/\\/g, '\\\\').replace(tripleQuotesRE, "\\'''");
   89       return "'''" + (newlineWrap(indentLines(string))) + "'''";
   90     }
   91   };
   92   visitArray = function(arr) {
   93     var array, items;
   94     items = arr.map(function(value) {
   95       var serializedValue;
   96       serializedValue = visitNode(value);
   97       if (isObject(value)) {
   98         return "{" + (newlineWrap(indentLines(serializedValue))) + "}";
   99       } else {
  100         return serializedValue;
  101       }
  102     });
  103     array = items.join('\n');
  104     return "[" + (newlineWrap(indentLines(array))) + "]";
  105   };
  106   visitObject = function(obj) {
  107     var key, keypairs, serializedValue, value;
  108     keypairs = (function() {
  109       var _results;
  110       _results = [];
  111       for (key in obj) {
  112         value = obj[key];
  113         if (!key.match(jsIdentifierRE)) {
  114           key = JSON.stringify(key);
  115         }
  116         serializedValue = visitNode(value);
  117         if (isObject(value)) {
  118           if (serializedValue === '') {
  119             _results.push(key + ": {}");
  120           } else {
  121             _results.push(key + ":\n" + (indentLines(serializedValue)));
  122           }
  123         } else {
  124           _results.push(key + ": " + serializedValue);
  125         }
  126       }
  127       return _results;
  128     })();
  129     return keypairs.join('\n');
  130   };
  131   visitNode = function(node) {
  132     switch (typeof node) {
  133       case 'boolean':
  134         return "" + node;
  135       case 'number':
  136         if (isFinite(node)) {
  137           return "" + node;
  138         } else {
  139           return 'null';
  140         }
  141         break;
  142       case 'string':
  143         return visitString(node);
  144       case 'object':
  145         if (node === null) {
  146           return 'null';
  147         } else if (Array.isArray(node)) {
  148           return visitArray(node);
  149         } else {
  150           return visitObject(node);
  151         }
  152     }
  153   };
  154   out = visitNode(normalized);
  155   if (out === '') {
  156     return '{}';
  157   } else {
  158     return out;
  159   }
  160 };