createCompounder.js 652 B

1234567891011121314151617181920212223242526
  1. var deburr = require('../string/deburr'),
  2. words = require('../string/words');
  3. /**
  4. * Creates a function that produces compound words out of the words in a
  5. * given string.
  6. *
  7. * @private
  8. * @param {Function} callback The function to combine each word.
  9. * @returns {Function} Returns the new compounder function.
  10. */
  11. function createCompounder(callback) {
  12. return function(string) {
  13. var index = -1,
  14. array = words(deburr(string)),
  15. length = array.length,
  16. result = '';
  17. while (++index < length) {
  18. result = callback(result, array[index], index);
  19. }
  20. return result;
  21. };
  22. }
  23. module.exports = createCompounder;