unique-words
Version:
Returns an array of unique words, or the number of occurrences of each word in a string or list.
20 lines (15 loc) • 476 B
JavaScript
/**
* unique-words <https://github.com/jonschlinkert/unique-words>
*
* Copyright (c) 2014-present, Jon Schlinkert.
* Licensed under the MIT license.
*/
;
const split = (...args) => [].concat.apply([], args).join(' ').split(/\W+/);
module.exports = (...args) => [...new Set(split(...args))];
module.exports.counts = (...args) => {
return split(...args).reduce((acc, word) => {
if (word) acc[word] = (acc[word] || 0) + 1;
return acc;
}, {});
};