everyutil
Version:
A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.
23 lines (22 loc) • 784 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractKeywords = void 0;
/**
* Extracts keywords from a string, removing stopwords and punctuation.
*
* Example: extractKeywords("The cat sat on the mat.", ["the", "on"]) → ['cat', 'sat', 'mat']
*
* @author @dailker
* @param {string} str - The input string.
* @param {string[]} [stopwords=[]] - Words to exclude from the result.
* @returns {string[]} Array of keywords.
*/
function extractKeywords(str, stopwords = []) {
const stops = new Set(stopwords.map(w => w.toLowerCase()));
return str
.toLowerCase()
.replace(/[^\w\s]/g, '')
.split(/\s+/)
.filter(w => w && !stops.has(w));
}
exports.extractKeywords = extractKeywords;