UNPKG

kwrd

Version:

+/- Keyword matching tool for sneaker bots

56 lines (55 loc) 1.37 kB
// src/index.ts var Keyword = class { constructor(value) { this.value = value; } static toKeywordStr(word) { word = word.toLowerCase().trim(); if (Keyword.isValid(word)) { return word; } return `+${word}`; } static from(word) { return new Keyword(Keyword.toKeywordStr(word)); } static isValid(word) { return ["+", "-"].includes(word.charAt(0)); } isPositive() { return this.value.startsWith("+"); } isNegative() { return !this.isPositive(); } getValue() { return this.value.substr(1); } }; function match(rawKeywords, rawName, strict = false) { const keywords = rawKeywords.split(",").map(Keyword.from); const name = rawName.trim().toLowerCase(); if (strict) { return keywords.every((word) => { const isPos = word.isPositive(); const didMatch = name.includes(word.getValue()); return isPos ? didMatch : !didMatch; }); } const mapped = keywords.map((word) => { const isPos = word.isPositive(); const didMatch = name.includes(word.getValue()); return [isPos, didMatch]; }); const matchedNegative = mapped.some(([isPos, didMatch]) => !isPos && didMatch); if (matchedNegative) { return false; } return mapped.some(([isPos, didMatch]) => isPos && didMatch); } var src_default = match; export { Keyword, src_default as default, match };