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

stackpress / lib / 21408468031

27 Jan 2026 06:03PM UTC coverage: 79.43% (+0.1%) from 79.327%
21408468031

push

github

cblanquera
version bump

500 of 688 branches covered (72.67%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

3 existing lines in 2 files now uncovered.

948 of 1135 relevant lines covered (83.52%)

26.7 hits per line

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

93.33
/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
    const entry = this.find(callback);
2✔
40
    return Array.isArray(entry) ? entry[0] : undefined;
2✔
41
  }
42

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

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

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

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

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