Something went wrong. Try again.
Convert CSV banking statements to hledger journals
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445// Which rule names a few typed letters are for (D12, §6.7 of DESIGN_M1.md).//// A name matches when it holds the letters in order, ignoring case, so "amzn"// finds "amazon-marketplace". Each letter is taken at its first place after// the one before. That is not always the place that would rank best, but it is// one a reader can follow.
const WORD = /[\p{L}\p{N}]/u;
// Where each letter of `query` falls in `name`, or null when they are not all// there. Both are lower case.function positions(query, name) { const found = []; let from = 0; for (const letter of query) { const at = name.indexOf(letter, from); if (at === -1) return null; found.push(at); from = at + letter.length; } return found;}
// How well the letters at `found` fit `name`: in how many runs of adjacent// letters, the fewer the better, and how many of them begin a word.function fit(found, name) { const runs = found.filter((at, index) => index === 0 || at !== found[index - 1] + 1).length; const starts = found.filter((at) => at === 0 || !WORD.test(name[at - 1])).length; return { runs, starts };}
// The indices of the names `query` matches, best first. Sort is stable, so// names that fit as well keep the order they were given in.export function ranked(query, names) { const lower = query.toLowerCase(); const matches = []; names.forEach((name, index) => { const lowerName = name.toLowerCase(); const found = positions(lower, lowerName); if (found !== null) matches.push({ index, ...fit(found, lowerName) }); }); matches.sort((a, b) => a.runs - b.runs || b.starts - a.starts); return matches.map((match) => match.index);}