hunspell-reader
Version:
A library for reading Hunspell Dictionary Files
90 lines • 2.11 kB
JavaScript
export function hrTimeToSeconds([seconds, nanoseconds]) {
return seconds + nanoseconds / 1_000_000_000;
}
export function uniqueFilter(historySize, key = (a) => a) {
const f0 = new Set();
const f1 = new Set();
const found = [f0, f1, f0];
let g = 0;
return (t) => {
const w = key(t);
const p = found[g];
if (p.has(w))
return false;
const s = found[g + 1];
const r = !s.has(w);
p.add(w);
if (p.size >= historySize) {
s.clear();
g = (g + 1) % 2;
}
return r;
};
}
export function* batch(i, size) {
let data = [];
for (const t of i) {
data.push(t);
if (data.length === size) {
yield data;
data = [];
}
}
if (data.length) {
yield data;
}
}
/**
* Generate a filter function that will remove adjacent values that compare to falsy;
* @param compare function to evaluate if two values are considered the same.
*/
export function filterOrderedList(compare) {
let last;
return (t) => {
const r = last === undefined ? last !== t : !!compare(last, t);
last = r ? t : last;
return r;
};
}
export function isDefined(v) {
return v !== undefined;
}
/**
* Remove all `undefined` values from an Object.
* @param obj
* @returns the same object.
*/
export function cleanObject(obj) {
if (typeof obj != 'object')
return obj;
const r = obj;
for (const [k, v] of Object.entries(r)) {
if (v === undefined) {
delete r[k];
}
}
return obj;
}
export function groupByField(i, field) {
const r = new Map();
for (const t of i) {
const k = t[field];
let a = r.get(k);
if (!a) {
a = [];
r.set(k, a);
}
a.push(t);
}
return r;
}
export function insertItemIntoGroupByField(map, field, item) {
const k = item[field];
let a = map.get(k);
if (!a) {
a = [];
map.set(k, a);
}
a.push(item);
}
//# sourceMappingURL=util.js.map