UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

30 lines (29 loc) 924 B
function implode(glue, pieces) { // From: http://phpjs.org/functions // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: Waldo Malqui Silva // + improved by: Itsacon (http://www.itsacon.net/) // + bugfixed by: Brett Zamir (http://brett-zamir.me) // * example 1: implode(' ', ['Kevin', 'van', 'Zonneveld']); // * returns 1: 'Kevin van Zonneveld' // * example 2: implode(' ', {first:'Kevin', last: 'van Zonneveld'}); // * returns 2: 'Kevin van Zonneveld' var i = '', retVal = '', tGlue = ''; if (arguments.length === 1) { pieces = glue; glue = ''; } if (typeof pieces === 'object') { if (Object.prototype.toString.call(pieces) === '[object Array]') { return pieces.join(glue); } for (i in pieces) { retVal += tGlue + pieces[i]; tGlue = glue; } return retVal; } return pieces; }