hunspell-reader
Version:
A library for reading Hunspell Dictionary Files
77 lines • 2.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleanObject = exports.isDefined = exports.filterOrderedList = exports.batch = exports.uniqueFilter = exports.hrTimeToSeconds = void 0;
function hrTimeToSeconds([seconds, nanoseconds]) {
return seconds + nanoseconds / 1000000000;
}
exports.hrTimeToSeconds = hrTimeToSeconds;
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;
};
}
exports.uniqueFilter = uniqueFilter;
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;
}
}
exports.batch = batch;
/**
* 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.
*/
function filterOrderedList(compare) {
let last;
return function (t) {
const r = last === undefined ? last !== t : !!compare(last, t);
last = r ? t : last;
return r;
};
}
exports.filterOrderedList = filterOrderedList;
function isDefined(v) {
return v !== undefined;
}
exports.isDefined = isDefined;
/**
* Remove all `undefined` values from an Object.
* @param obj
* @returns the same object.
*/
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;
}
exports.cleanObject = cleanObject;
//# sourceMappingURL=util.js.map