kwrd
Version:
+/- Keyword matching tool for sneaker bots
69 lines (67 loc) • 1.8 kB
JavaScript
var __defProp = Object.defineProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
__markAsModule(target);
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
// src/index.ts
__export(exports, {
Keyword: () => Keyword,
default: () => src_default,
match: () => match
});
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;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Keyword,
match
});