• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

source-academy / js-slang / 5936042370

22 Aug 2023 07:58AM UTC coverage: 83.014% (+0.1%) from 82.893%
5936042370

Pull #1471

github

web-flow
Merge 6d88e2972 into 1d991166b
Pull Request #1471: Use Asynchronous Code for Loading Modules

3618 of 4779 branches covered (75.71%)

Branch coverage included in aggregate %.

269 of 269 new or added lines in 23 files covered. (100.0%)

10838 of 12635 relevant lines covered (85.78%)

106101.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/src/utils/arrayMap.ts
1
/**
2
 * Convenience class for maps that store an array of values
3
 */
4
export default class ArrayMap<K, V> {
1✔
5
  constructor(private readonly map: Map<K, V[]> = new Map()) {}
3✔
6

7
  public get(key: K) {
8
    return this.map.get(key)
4✔
9
  }
10

11
  public add(key: K, item: V) {
12
    if (!this.map.has(key)) {
12✔
13
      this.map.set(key, [])
4✔
14
    }
15
    this.map.get(key)!.push(item)
12✔
16
  }
17

18
  public entries() {
19
    return Array.from(this.map.entries())
1✔
20
  }
21

22
  public keys() {
23
    return new Set(this.map.keys())
×
24
  }
25

26
  /**
27
   * Similar to `mapAsync`, but for an async mapping function that does not return any value
28
   */
29
  public async forEachAsync<F extends (k: K, v: V[]) => Promise<void>>(forEach: F): Promise<void> {
30
    await Promise.all(this.entries().map(([key, value]) => forEach(key, value)))
×
31
  }
32

33
  /**
34
   * Using a mapping function that returns a promise, transform an array map
35
   * to another array map with different keys and values. All calls to the mapping function
36
   * execute asynchronously
37
   */
38
  public async mapAsync<F extends (k: K, v: V[]) => Promise<[any, any[]]>>(mapper: F) {
39
    const pairs = await Promise.all(this.entries().map(([key, value]) => mapper(key, value)))
2✔
40

41
    type U = Awaited<ReturnType<F>>
42
    const tempMap = new Map<U[0], U[1]>(pairs)
1✔
43
    return new ArrayMap<U[0], U[1][number]>(tempMap)
1✔
44
  }
45

46
  public [Symbol.toStringTag]() {
47
    return this.entries().map(([key, value]) => `${key}: ${value}`)
×
48
  }
49
}
50

51
/**
52
 * Create an ArrayMap from an iterable of key value pairs
53
 */
54
export function arrayMapFrom<K, V extends Array<any>>(
55
  pairs: Iterable<[K, V]>
56
): ArrayMap<K, V[number]>
57
export function arrayMapFrom<K, V>(pairs: Iterable<[K, V]>): ArrayMap<K, V>
58
export function arrayMapFrom<K, V>(pairs: Iterable<[K, V | V[]]>) {
1✔
59
  const res = new ArrayMap<K, V>()
2✔
60
  for (const [k, v] of pairs) {
2✔
61
    if (Array.isArray(v)) {
4!
62
      for (const each of v) {
4✔
63
        res.add(k, each)
12✔
64
      }
65
    } else {
66
      res.add(k, v)
×
67
    }
68
  }
69

70
  return res
2✔
71
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc