UNPKG

@ts-for-gir/lib

Version:

Typescript .d.ts generator from GIR for gjs

67 lines 1.54 kB
/** * A simple two-key map. */ export class TwoKeyMap { baseMap = new Map(); forEach(op) { this.baseMap.forEach((map) => { map.forEach((v) => { op(v); }); }); } has(key1, key2) { const obj = this.baseMap.get(key1); if (!obj) { return false; } return obj.has(key2); } getIfOnly(key1) { const obj = this.baseMap.get(key1); if (!obj) { return undefined; } if (obj.size === 1) { const [k2] = obj.keys(); const v = obj.get(k2); if (!v) { return undefined; } return [k2, v]; } return undefined; } get(key1, key2) { const obj = this.baseMap.get(key1); if (!obj) { return undefined; } return obj.get(key2); } set(key1, key2, value) { let obj = this.baseMap.get(key1); if (!obj) { obj = new Map(); this.baseMap.set(key1, obj); } obj.set(key2, value); } } /** * If the predicate returns a value other than undefined, * that value is returned. It combines the .find() and * .map() APIs for convenience. * * @param arr * @param predicate */ export function findMap(arr, predicate) { for (const a of arr) { const val = predicate(a); if (val !== undefined) return val; } return undefined; } //# sourceMappingURL=util.js.map