• 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

96.77
/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] ];
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 typeof entry !== 'undefined' ? entry[0] as number : -1;
2✔
42
  }
43

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

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

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

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

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

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