blob: f69257867f52e3ff742a038d740b2ba1ccaafda8 [file] [log] [blame]
Issam E. Maghnif796a092022-10-09 20:25:26 +00001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
18import { createRequire } from 'node:module';
19
Misha Krieger-Raynauldb6f1c322022-10-23 20:42:57 -040020// TODO: Move these functions to jami-swig.ts
21
Issam E. Maghnif796a092022-10-09 20:25:26 +000022export function* itRange(lo: number, hi: number) {
23 for (let i = lo; i < hi; ++i) {
24 yield i;
25 }
26}
27
28export function* itMap<T, U>(it: Iterable<T>, cb: (value: T, index: number) => U) {
29 let i = 0;
30 for (const item of it) {
31 yield cb(item, i++);
32 }
33}
34
35export function* itFilter<T>(it: Iterable<T>, cb: (value: T, index: number) => boolean) {
36 let i = 0;
37 for (const item of it) {
38 if (cb(item, i++)) {
39 yield item;
40 }
41 }
42}
43
44export const itToArr = <T>(it: Iterable<T>) => Array.from(it);
45
Misha Krieger-Raynauldb6f1c322022-10-23 20:42:57 -040046export const itToRecord = <T>(it: Iterable<[string, T]>) => {
47 const r: Record<string, T> = {};
48 for (const [k, v] of it) {
49 r[k] = v;
50 }
51 return r;
52};
53
Issam E. Maghnif796a092022-10-09 20:25:26 +000054export const require = createRequire(import.meta.url);