toSentence.js 411 B

123456789101112
  1. var rtrim = require('./rtrim');
  2. module.exports = function toSentence(array, separator, lastSeparator, serial) {
  3. separator = separator || ', ';
  4. lastSeparator = lastSeparator || ' and ';
  5. var a = array.slice(),
  6. lastMember = a.pop();
  7. if (array.length > 2 && serial) lastSeparator = rtrim(separator) + lastSeparator;
  8. return a.length ? a.join(separator) + lastSeparator + lastMember : lastMember;
  9. };