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

stackpress / lib / 21355728094

26 Jan 2026 11:16AM UTC coverage: 79.327% (-0.2%) from 79.564%
21355728094

push

github

cblanquera
version bump

494 of 682 branches covered (72.43%)

Branch coverage included in aggregate %.

945 of 1132 relevant lines covered (83.48%)

26.76 hits per line

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

91.67
/src/data/Map.ts
1
import type { 
2
  CallableMap, 
3
  DataMapFilter,
4
  DataMapIterator
5
} from '../types.js';
6

7
/**
8
 * Adding extra utility methods to the native Map class
9
 * (that should have been there in the first place...)
10
 */
11
export default class DataMap<K = any, V = any> extends Map<K, V> {
12
  /**
13
   * Filters the data map (returns a new DataMap instance)
14
   */
15
  public filter(callback: DataMapFilter<K, V, this>) {
16
    const entries = Array.from(this.entries()).filter(
2✔
17
      entry => callback(entry[1], entry[0], this)
4✔
18
    );
19
    const constructor = this.constructor as new(map: [K, V][]) => this;
2✔
20
    return new constructor(entries);
2✔
21
  }
22

23
  /**
24
   * Finds the first entry that matches the callback
25
   */
26
  public find(callback: DataMapFilter<K, V, this>) {
27
    for (const entry of this.entries()) {
4✔
28
      if (callback(entry[1], entry[0], this)) {
7✔
29
        return entry;
2✔
30
      }
31
    }
32
    return undefined;
2✔
33
  }
34

35
  /**
36
   * Finds the first key that matches the callback
37
   */
38
  public findKey(callback: DataMapFilter<K, V, this>) {
39
    return this.find(callback)?.[0];
2✔
40
  }
41

42
  /**
43
   * Finds the first value that matches the callback
44
   */
45
  public findValue(callback: DataMapFilter<K, V, this>) {
46
    return this.find(callback)?.[1];
2✔
47
  }
48

49
  /**
50
   * Maps the data map values to a new data map
51
   */
52
  public map<T>(callback: DataMapIterator<K, V, this, T>) {
53
    const constructor = this.constructor as new() => DataMap<K, T>;
1✔
54
    const map = new constructor();
1✔
55
    for (const entry of this.entries()) {
1✔
56
      map.set(entry[0], callback(entry[1], entry[0], this));
2✔
57
    }
58
    return map;
1✔
59
  }
60

61
  /**
62
   * Returns the data map as a plain object
63
   */
64
  public toObject() {
65
    return Object.fromEntries(Object.entries(this));
×
66
  }
67

68
  /**
69
   * Returns the data map as a JSON string
70
   */
71
  public toString(
72
    replacer?: (key: string, value: any) => any, 
73
    space?: string | number
74
  ) {
75
    return JSON.stringify(this.toObject(), replacer, space);
×
76
  }
77
};
78

79
export function map<K = any, V = any> (data?: [K, V][]): CallableMap<K, V> {
80
  const store = new DataMap<K, V>(data);
50✔
81
  const callable = Object.assign(
50✔
82
    (name: K) => store.get(name),
4✔
83
    {
84
      clear: store.clear.bind(store),
85
      delete: store.delete.bind(store),
86
      entries: store.entries.bind(store),
87
      filter: store.filter.bind(store),
88
      find: store.find.bind(store),
89
      findKey: store.findKey.bind(store),
90
      findValue: store.findValue.bind(store),
91
      forEach: store.forEach.bind(store),
92
      get: store.get.bind(store),
93
      has: store.has.bind(store),
94
      keys: store.keys.bind(store),
95
      map: store.map.bind(store),
96
      set: store.set.bind(store),
97
      toObject: store.toObject.bind(store),
98
      toString: store.toString.bind(store),
99
      values: store.values.bind(store)
100
    } as DataMap<K, V>
101
  );
102
  //magic size property
103
  Object.defineProperty(callable, 'size', { get: () => store.size });
50✔
104
  return callable;
50✔
105
};
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

© 2026 Coveralls, Inc