Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 18 | import { createRequire } from 'node:module'; |
| 19 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 20 | // TODO: Move these functions to jami-swig.ts |
| 21 | |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 22 | export function* itRange(lo: number, hi: number) { |
| 23 | for (let i = lo; i < hi; ++i) { |
| 24 | yield i; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | export 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 | |
| 35 | export 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 | |
| 44 | export const itToArr = <T>(it: Iterable<T>) => Array.from(it); |
| 45 | |
| 46 | export const itToMap = <T, U>(it: Iterable<[T, U]>) => { |
| 47 | const m = new Map<T, U>(); |
| 48 | for (const [k, v] of it) { |
| 49 | m.set(k, v); |
| 50 | } |
| 51 | return m; |
| 52 | }; |
| 53 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 54 | export const itToRecord = <T>(it: Iterable<[string, T]>) => { |
| 55 | const r: Record<string, T> = {}; |
| 56 | for (const [k, v] of it) { |
| 57 | r[k] = v; |
| 58 | } |
| 59 | return r; |
| 60 | }; |
| 61 | |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 62 | export const require = createRequire(import.meta.url); |