@ixo/supamoto-bot-sdk
Version:
An SDK to easily interact with Supamoto bot db
23 lines • 708 B
JavaScript
import crypto from 'crypto';
function normalize(s) {
return s.toLowerCase().replace(/[^a-z0-9]+/g, ' ').replace(/\s+/g, ' ') // collapse spaces
.trim();
}
export function trigrams(s, size = 3) {
const t = normalize(s);
if (!t) return [];
const grams = [];
for (let i = 0; i <= t.length - size; i++) {
grams.push(t.slice(i, i + size));
}
return Array.from(new Set(grams)); // dedupe
}
export function hmacBytes(msg, key) {
return crypto.createHmac('sha256', key).update(msg).digest(); // 32-byte
}
export function hmacTrigrams(grams, key) {
return grams.map(g => hmacBytes(g, key));
}
export function hmacCountry(country, key) {
return hmacBytes(country.trim().toUpperCase(), key);
}