Skip to content

Instantly share code, notes, and snippets.

@shinnn
Created March 17, 2014 20:21
Show Gist options
  • Save shinnn/9607524 to your computer and use it in GitHub Desktop.
Save shinnn/9607524 to your computer and use it in GitHub Desktop.
(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;
}
}());
function doubleQuote(str) {
return '"' + str + '"';
}
arrayWriteOut(['Apple', 'Orange', 'Blueberry'], doubleQuote);
//=> "Apple", "Orange" and "Blueberry"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment