• 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

97.06
/src/data/Set.ts
1
import type { 
2
  CallableSet, 
3
  DataSetFilter, 
4
  DataSetIterator 
5
} from '../types.js';
6

7
/**
8
 * Adding extra utility methods to the native Set class
9
 * (that should have been there in the first place...)
10
 */
11
export default class DataSet<V = any> extends Set<V> {
12
  /**
13
   * Filters the data map (returns a new DataSet instance)
14
   */
15
  public filter(callback: DataSetFilter<V, this>) {
16
    const values = this.toArray().filter(
2✔
17
      (value, index) => callback(value, index, this)
4✔
18
    );
19
    const constructor = this.constructor as new(set: V[]) => this;
2✔
20
    return new constructor(values);
2✔
21
  }
22

23
  /**
24
   * Finds the first entry that matches the callback
25
   */
26
  public find(callback: DataSetFilter<V, this>) {
27
    const values = this.toArray();
4✔
28
    for (let i = 0; i < values.length; i++) {
4✔
29
      if (callback(values[i], i, this)) {
8✔
30
        return [ i, values[i] ] as [number, V];
2✔
31
      }
32
    }
33
    return undefined;
2✔
34
  }
35

36
  /**
37
   * Finds the first index that matches the callback
38
   */
39
  public findIndex(callback: DataSetFilter<V, this>) {
40
    const entry = this.find(callback);
2✔
41
    return Array.isArray(entry) ? entry[0] : -1;
2✔
42
  }
43

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

52
  /**
53
   * Returns the value at the given index
54
   */
55
  public index(index: number) {
56
    const values = this.toArray();
2✔
57
    return values[index];
2✔
58
  }
59

60
  /**
61
   * Maps the data map values to a new data map
62
   */
63
  public map<T>(callback: DataSetIterator<V, this, T>) {
64
    const constructor = this.constructor as new() => DataSet<T>;
1✔
65
    const map = new constructor();
1✔
66
    const values = this.toArray();
1✔
67
    for (let i = 0; i < values.length; i++) {
1✔
68
      map.add(callback(values[i], i, this));
2✔
69
    }
70
    return map;
1✔
71
  }
72

73
  /**
74
   * Returns the data set as a plain array
75
   */
76
  public toArray() {
77
    return Array.from(this);
9✔
78
  }
79

80
  /**
81
   * Returns the data set as a JSON string
82
   */
83
  public toString(
84
    replacer?: (key: string, value: any) => any, 
85
    space?: string | number
86
  ) {
UNCOV
87
    return JSON.stringify(this.toArray(), replacer, space);
×
88
  }
89
};
90

91
export function set<V = any> (data?: V[]): CallableSet<V> {
92
  const store = new DataSet<V>(data);
5✔
93
  const callable = Object.assign(
5✔
94
    (index: number) => Array.from(store.values())[index],
2✔
95
    {
96
      add: store.add.bind(store),
97
      clear: store.clear.bind(store),
98
      delete: store.delete.bind(store),
99
      entries: store.entries.bind(store),
100
      filter: store.filter.bind(store),
101
      find: store.find.bind(store),
102
      findIndex: store.findIndex.bind(store),
103
      findValue: store.findValue.bind(store),
104
      forEach: store.forEach.bind(store),
105
      has: store.has.bind(store),
106
      index: store.index.bind(store),
107
      map: store.map.bind(store),
108
      toArray: store.toArray.bind(store),
109
      toString: store.toString.bind(store),
110
      values: store.values.bind(store)
111
    } as DataSet<V>
112
  );
113
  //magic size property
114
  Object.defineProperty(callable, 'size', { get: () => store.size });
5✔
115
  return callable;
5✔
116
};
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