Skip to content

Instantly share code, notes, and snippets.

@summivox
Forked from JeffJacobson/tocsv.js
Last active January 4, 2016 21:49

Revisions

  1. summivox revised this gist Jan 29, 2014. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions tocsv.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,7 @@
    /*!
    * https://gist.github.com/JeffJacobson/2770509
    * https://gist.github.com/smilekzs/8683955
    * Licensed under The MIT License
    *
    * modified:
    *
    */

    /**
  2. summivox revised this gist Jan 29, 2014. 1 changed file with 52 additions and 61 deletions.
    113 changes: 52 additions & 61 deletions tocsv.js
    Original file line number Diff line number Diff line change
    @@ -1,72 +1,63 @@
    /*!
    * https://gist.github.com/JeffJacobson/2770509
    * Licensed under The MIT License
    *
    * modified:
    *
    */

    /**
    * Converts a value to a string appropriate for entry into a CSV table. E.g., a string value will be surrounded by quotes.
    * @param {string|number|object} theValue
    * @param {string} sDelimiter The string delimiter. Defaults to a double quote (") if omitted.
    */
    function toCsvValue(theValue, sDelimiter) {
    var t = typeof (theValue), output;

    if (typeof (sDelimiter) === "undefined" || sDelimiter === null) {
    sDelimiter = '"';
    }

    if (t === "undefined" || t === null) {
    output = "";
    } else if (t === "string") {
    output = sDelimiter + theValue + sDelimiter;
    } else {
    output = String(theValue);
    }

    return output;
    function toCsvValue(theValue) {
    var t = typeof (theValue), output;

    if (t === "undefined" || t === null) {
    output = "";
    } else {
    output = '"' + theValue.toString().replace(/"/g, '""') + '"';
    }

    return output;
    }

    /**
    * Converts an array of objects (with identical schemas) into a CSV table.
    * @param {Array} objArray An array of objects. Each object in the array must have the same property list.
    * @param {string} sDelimiter The string delimiter. Defaults to a double quote (") if omitted.
    * @param {string} cDelimiter The column delimiter. Defaults to a comma (,) if omitted.
    * @return {string} The CSV equivalent of objArray.
    */
    function toCsv(objArray, sDelimiter, cDelimiter) {
    var i, l, names = [], name, value, obj, row, output = "", n, nl;

    // Initialize default parameters.
    if (typeof (sDelimiter) === "undefined" || sDelimiter === null) {
    sDelimiter = '"';
    }
    if (typeof (cDelimiter) === "undefined" || cDelimiter === null) {
    cDelimiter = ",";
    }

    for (i = 0, l = objArray.length; i < l; i += 1) {
    // Get the names of the properties.
    obj = objArray[i];
    row = "";
    if (i === 0) {
    // Loop through the names
    for (name in obj) {
    if (obj.hasOwnProperty(name)) {
    names.push(name);
    row += [sDelimiter, name, sDelimiter, cDelimiter].join("");
    }
    }
    row = row.substring(0, row.length - 1);
    output += row;
    }

    output += "\n";
    row = "";
    for (n = 0, nl = names.length; n < nl; n += 1) {
    name = names[n];
    value = obj[name];
    if (n > 0) {
    row += ","
    }
    row += toCsvValue(value, '"');
    }
    output += row;
    }

    return output;
    function toCsv(objArray) {
    var i, l, names = [], name, value, obj, row, output = "", n, nl;

    for (i = 0, l = objArray.length; i < l; i += 1) {
    // Get the names of the properties.
    obj = objArray[i];
    row = "";
    if (i === 0) {
    // Loop through the names
    for (name in obj) {
    if (obj.hasOwnProperty(name)) {
    names.push(name);
    row += ['"', name, '"', ","].join("");
    }
    }
    row = row.substring(0, row.length - 1);
    output += row;
    }

    output += "\n";
    row = "";
    for (n = 0, nl = names.length; n < nl; n += 1) {
    name = names[n];
    value = obj[name];
    if (n > 0) {
    row += ","
    }
    row += toCsvValue(value, '"');
    }
    output += row;
    }

    return output;
    }
  3. @JeffJacobson JeffJacobson revised this gist Dec 28, 2012. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions tocsv.js
    Original file line number Diff line number Diff line change
    @@ -24,12 +24,11 @@ function toCsvValue(theValue, sDelimiter) {
    /**
    * Converts an array of objects (with identical schemas) into a CSV table.
    * @param {Array} objArray An array of objects. Each object in the array must have the same property list.
    * @return {String} The CSV table.
    * @param {string} sDelimiter The string delimiter. Defaults to a double quote (") if omitted.
    * @param {string} cDelimiter The column delimiter. Defaults to a comma (,) if omitted.
    * @return {string} The CSV equivalent of objArray.
    */
    function toCsv(objArray, outputFile, sDelimiter, cDelimiter) {
    function toCsv(objArray, sDelimiter, cDelimiter) {
    var i, l, names = [], name, value, obj, row, output = "", n, nl;

    // Initialize default parameters.
  4. @JeffJacobson JeffJacobson revised this gist Dec 27, 2012. 1 changed file with 61 additions and 61 deletions.
    122 changes: 61 additions & 61 deletions tocsv.js
    Original file line number Diff line number Diff line change
    @@ -1,73 +1,73 @@
    /**
    * Converts a value to a string appropriate for entry into a CSV table. E.g., a string value will be surrounded by quotes.
    * @param {string|number|object} theValue
    * @param {string} sDelimiter The string delimiter. Defaults to a double quote (") if omitted.
    */
    function toCsvValue(theValue, sDelimiter) {
    var t = typeof (theValue), output;
    /**
    * Converts a value to a string appropriate for entry into a CSV table. E.g., a string value will be surrounded by quotes.
    * @param {string|number|object} theValue
    * @param {string} sDelimiter The string delimiter. Defaults to a double quote (") if omitted.
    */
    function toCsvValue(theValue, sDelimiter) {
    var t = typeof (theValue), output;

    if (typeof (sDelimiter) === "undefined" || sDelimiter === null) {
    sDelimiter = '"';
    }

    if (t === "undefined" || t === null) {
    output = "";
    } else if (t === "string") {
    output = sDelimiter + theValue + sDelimiter;
    } else {
    output = String(theValue);
    }
    if (typeof (sDelimiter) === "undefined" || sDelimiter === null) {
    sDelimiter = '"';
    }

    return output;
    if (t === "undefined" || t === null) {
    output = "";
    } else if (t === "string") {
    output = sDelimiter + theValue + sDelimiter;
    } else {
    output = String(theValue);
    }

    /**
    * Converts an array of objects (with identical schemas) into a CSV table.
    * @param {Array} objArray An array of objects. Each object in the array must have the same property list.
    * @return {String} The CSV table.
    * @param {string} sDelimiter The string delimiter. Defaults to a double quote (") if omitted.
    * @param {string} cDelimiter The column delimiter. Defaults to a comma (,) if omitted.
    * @return {string} The CSV equivalent of objArray.
    */
    function toCsv(objArray, outputFile, sDelimiter, cDelimiter) {
    var i, l, names = [], name, value, obj, row, output = "", n, nl;
    return output;
    }

    // Initialize default parameters.
    if (typeof (sDelimiter) === "undefined" || sDelimiter === null) {
    sDelimiter = '"';
    }
    if (typeof (cDelimiter) === "undefined" || cDelimiter === null) {
    cDelimiter = ",";
    }
    /**
    * Converts an array of objects (with identical schemas) into a CSV table.
    * @param {Array} objArray An array of objects. Each object in the array must have the same property list.
    * @return {String} The CSV table.
    * @param {string} sDelimiter The string delimiter. Defaults to a double quote (") if omitted.
    * @param {string} cDelimiter The column delimiter. Defaults to a comma (,) if omitted.
    * @return {string} The CSV equivalent of objArray.
    */
    function toCsv(objArray, outputFile, sDelimiter, cDelimiter) {
    var i, l, names = [], name, value, obj, row, output = "", n, nl;

    for (i = 0, l = objArray.length; i < l; i += 1) {
    // Get the names of the properties.
    obj = objArray[i];
    row = "";
    if (i === 0) {
    // Loop through the names
    for (name in obj) {
    if (obj.hasOwnProperty(name)) {
    names.push(name);
    row += [sDelimiter, name, sDelimiter, cDelimiter].join("");
    }
    }
    row = row.substring(0, row.length - 1);
    output += row;
    }
    // Initialize default parameters.
    if (typeof (sDelimiter) === "undefined" || sDelimiter === null) {
    sDelimiter = '"';
    }
    if (typeof (cDelimiter) === "undefined" || cDelimiter === null) {
    cDelimiter = ",";
    }

    output += "\n";
    row = "";
    for (n = 0, nl = names.length; n < nl; n += 1) {
    name = names[n];
    value = obj[name];
    if (n > 0) {
    row += ","
    for (i = 0, l = objArray.length; i < l; i += 1) {
    // Get the names of the properties.
    obj = objArray[i];
    row = "";
    if (i === 0) {
    // Loop through the names
    for (name in obj) {
    if (obj.hasOwnProperty(name)) {
    names.push(name);
    row += [sDelimiter, name, sDelimiter, cDelimiter].join("");
    }
    row += toCsvValue(value, '"');
    }
    row = row.substring(0, row.length - 1);
    output += row;
    }

    return output;
    }
    output += "\n";
    row = "";
    for (n = 0, nl = names.length; n < nl; n += 1) {
    name = names[n];
    value = obj[name];
    if (n > 0) {
    row += ","
    }
    row += toCsvValue(value, '"');
    }
    output += row;
    }

    return output;
    }
  5. @JeffJacobson JeffJacobson created this gist May 22, 2012.
    73 changes: 73 additions & 0 deletions tocsv.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    /**
    * Converts a value to a string appropriate for entry into a CSV table. E.g., a string value will be surrounded by quotes.
    * @param {string|number|object} theValue
    * @param {string} sDelimiter The string delimiter. Defaults to a double quote (") if omitted.
    */
    function toCsvValue(theValue, sDelimiter) {
    var t = typeof (theValue), output;

    if (typeof (sDelimiter) === "undefined" || sDelimiter === null) {
    sDelimiter = '"';
    }

    if (t === "undefined" || t === null) {
    output = "";
    } else if (t === "string") {
    output = sDelimiter + theValue + sDelimiter;
    } else {
    output = String(theValue);
    }

    return output;
    }

    /**
    * Converts an array of objects (with identical schemas) into a CSV table.
    * @param {Array} objArray An array of objects. Each object in the array must have the same property list.
    * @return {String} The CSV table.
    * @param {string} sDelimiter The string delimiter. Defaults to a double quote (") if omitted.
    * @param {string} cDelimiter The column delimiter. Defaults to a comma (,) if omitted.
    * @return {string} The CSV equivalent of objArray.
    */
    function toCsv(objArray, outputFile, sDelimiter, cDelimiter) {
    var i, l, names = [], name, value, obj, row, output = "", n, nl;

    // Initialize default parameters.
    if (typeof (sDelimiter) === "undefined" || sDelimiter === null) {
    sDelimiter = '"';
    }
    if (typeof (cDelimiter) === "undefined" || cDelimiter === null) {
    cDelimiter = ",";
    }

    for (i = 0, l = objArray.length; i < l; i += 1) {
    // Get the names of the properties.
    obj = objArray[i];
    row = "";
    if (i === 0) {
    // Loop through the names
    for (name in obj) {
    if (obj.hasOwnProperty(name)) {
    names.push(name);
    row += [sDelimiter, name, sDelimiter, cDelimiter].join("");
    }
    }
    row = row.substring(0, row.length - 1);
    output += row;
    }

    output += "\n";
    row = "";
    for (n = 0, nl = names.length; n < nl; n += 1) {
    name = names[n];
    value = obj[name];
    if (n > 0) {
    row += ","
    }
    row += toCsvValue(value, '"');
    }
    output += row;
    }

    return output;
    }