Skip to content

Instantly share code, notes, and snippets.

@shinnn
Created March 17, 2014 20:21

Revisions

  1. shinnn created this gist Mar 17, 2014.
    30 changes: 30 additions & 0 deletions array-write-out.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    (function() {
    'use strict';

    function arrayWriteOut(arr, process) {
    if (!process) {
    process = function(str) {
    return str;
    };
    }

    var result = '';
    for (var i=0; i < arr.length; i++) {
    if (i > 0) {
    if (i < arr.length - 1) {
    result += ', ';
    } else {
    result += ' and ';
    }
    }
    result += process.call(null, arr[i], i);
    }
    return result;
    }

    if (typeof module !== 'undefined' && module.exports) {
    module.exports = arrayWriteOut;
    } else {
    window.arrayWriteOut = arrayWriteOut;
    }
    }());
    6 changes: 6 additions & 0 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    function doubleQuote(str) {
    return '"' + str + '"';
    }

    arrayWriteOut(['Apple', 'Orange', 'Blueberry'], doubleQuote);
    //=> "Apple", "Orange" and "Blueberry"