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

WolferyScripting / resclient-ts / #17

29 Jul 2025 02:12PM UTC coverage: 55.405% (+4.4%) from 51.039%
#17

push

DonovanDMC
Add tests for CacheItem
woo 100 tests

216 of 267 branches covered (80.9%)

Branch coverage included in aggregate %.

1383 of 2619 relevant lines covered (52.81%)

12.45 hits per line

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

64.08
/lib/models/ResCollection.ts
1
import type ResClient from "./ResClient.js";
1✔
2
import type CacheItem from "./CacheItem.js";
1✔
3
import { copy } from "../includes/utils/obj.js";
1✔
4
import Properties from "../util/Properties.js";
1✔
5
import { type AnyObject, type AnyFunction } from "../util/types.js";
1✔
6

1✔
7
export default class ResCollection<V = unknown, C extends ResClient = ResClient> {
1✔
8
    private _idCallback?: (item: V) => string;
12✔
9
    private _list: Array<V> = [];
12✔
10
    private _map!: Record<string, V> | null;
12✔
11
    protected api!: C;
12✔
12
    rid!: string;
12✔
13
    constructor(api: C | null, rid: string, options?: { idCallback?(item: V): string; }) {
12✔
14
        options = copy(options ?? {}, {
12✔
15
            idCallback: { type: "?function" }
12✔
16
        });
12✔
17
        Properties.of(this)
12✔
18
            .writableBulk(["_idCallback", options?.idCallback?.bind(this)], "_list", ["_map", options.idCallback ? {} : null])
12✔
19
            .readOnly("api", api)
12✔
20
            .define("rid", false, true, true, rid);
12✔
21
    }
12✔
22

12✔
23
    private _hasID(): void {
12✔
24
        if (!this._idCallback) {
14!
25
            throw new Error("No id callback defined");
×
26
        }
×
27
    }
14✔
28

12✔
29
    /** Add a direct dependency to this collection's CacheItem, preventing it from being unsubscribed. */
12✔
30
    get cacheItem(): CacheItem<ResCollection> {
12✔
31
        return this.getClient().cache[this.rid] as CacheItem<ResCollection>;
×
32
    }
×
33

12✔
34
    /** If this collection is empty. */
12✔
35
    get empty(): boolean {
12✔
36
        return this.length === 0;
×
37
    }
×
38

12✔
39
    get length(): number {
12✔
40
        return this._list.length;
×
41
    }
×
42

12✔
43
    get list(): Array<V> {
12✔
44
        return this._list;
×
45
    }
×
46

12✔
47
    [Symbol.iterator](): Iterator<V, undefined> {
12✔
48
        return this._list[Symbol.iterator]();
×
49
    }
×
50

12✔
51
    add(item: V, index: number): void {
12✔
52
        this._list.splice(index, 0, item);
3✔
53

3✔
54
        if (this._idCallback) {
3✔
55
            const id = String(this._idCallback(item));
2✔
56
            if (["", "undefined", "null"].includes(id) || id.replaceAll(/\W/g, "") === "") {
2!
57
                console.debug(item);
×
58
                throw new Error("No id for item");
×
59
            }
×
60
            if (this._map![id]) {
2✔
61
                throw new Error(`Duplicate id - ${id}`);
1✔
62
            }
1✔
63
            this._map![id] = item;
1✔
64
        }
1✔
65
    }
3✔
66

12✔
67
    at(index: number): V | undefined {
12✔
68
        return this._list[index];
14✔
69
    }
14✔
70

12✔
71
    auth<T = unknown>(method: string, params: unknown): Promise<T> {
12✔
72
        return this.api.authenticate<T>(this.rid, method, params);
×
73
    }
×
74

12✔
75
    call<T = unknown>(method: string, params: unknown): Promise<T> {
12✔
76
        return this.api.call<T>(this.rid, method, params);
×
77
    }
×
78

12✔
79
    /** See: {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every | Array#every } */
12✔
80
    every<T extends V, ThisArg = ResCollection<V>>(predicate: (value: V, index: number, array: Array<V>) => value is T, thisArg?: ThisArg): this is Array<T>;
12✔
81
    every<ThisArg = ResCollection<V>>(predicate: (value: V, index: number, array: Array<V>) => unknown, thisArg?: ThisArg): boolean;
12✔
82
    every(predicate: (value: V, index: number, array: Array<V>) => unknown, thisArg?: unknown): boolean {
12✔
83
        return this.toArray().every(predicate, thisArg);
×
84

×
85
    }
×
86

12✔
87
    /** See: {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter | Array#filter } */
12✔
88
    filter<S extends V, ThisArg = ResCollection<V>>(predicate: (this: ThisArg, value: V, index: number, array: Array<V>) => value is S, thisArg?: ThisArg): Array<S>;
12✔
89
    filter<ThisArg = ResCollection<V>>(predicate: (this: ThisArg, value: V, index: number, array: Array<V>) => unknown, thisArg?: ThisArg): Array<V>;
12✔
90
    filter(predicate: (value: V, index: number, array: Array<V>) => unknown, thisArg?: unknown): Array<V> {
12✔
91
        return this.toArray().filter(predicate, thisArg) ;
×
92
    }
×
93

12✔
94
    /** See: {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find | Array#find } */
12✔
95
    find<S extends V, ThisArg = ResCollection<V>>(predicate: (this: ThisArg, value: V, index: number, obj: Array<V>) => value is S, thisArg?: ThisArg): S | undefined;
12✔
96
    find<ThisArg = ResCollection<V>>(predicate: (this: ThisArg, value: V, index: number, obj: Array<V>) => unknown, thisArg?: ThisArg): V | undefined;
12✔
97
    find(predicate: (value: V, index: number, obj: Array<V>) => unknown, thisArg?: unknown): V | undefined {
12✔
98
        return this.toArray().find(predicate, thisArg);
×
99
    }
×
100

12✔
101
    /** See: {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex | Array#findIndex } */
12✔
102
    findIndex(predicate: (value: V, index: number, obj: Array<V>) => unknown, thisArg?: unknown): number {
12✔
103
        return this.toArray().findIndex(predicate, thisArg);
×
104
    }
×
105

12✔
106
    /**
12✔
107
     * Get the first element, or first X elements if a number is provided.
12✔
108
     * @param amount The amount of elements to get.
12✔
109
     */
12✔
110
    first(): V | undefined;
12✔
111
    first(amount: number): Array<V>;
12✔
112
    first(amount?: number): V | Array<V> | undefined {
12✔
113
        if (amount === undefined) {
×
114
            const iterable = this[Symbol.iterator]();
×
115
            return iterable.next().value;
×
116
        }
×
117

×
118
        if (amount < 0) {
×
119
            return this.last(amount * -1);
×
120
        }
×
121
        amount = Math.min(amount, this.length);
×
122

×
123
        const iterable = this[Symbol.iterator]();
×
124
        return Array.from({ length: amount }, () => iterable.next().value!);
×
125
    }
×
126

12✔
127
    get(id: string | number): V | undefined {
12✔
128
        this._hasID();
14✔
129
        return this._map![id];
14✔
130
    }
14✔
131

12✔
132
    getClient(): C {
12✔
133
        return this.api;
×
134
    }
×
135

12✔
136
    getOrThrow(id: string | number): V {
12✔
137
        const item = this.get(id);
×
138
        if (item === undefined) {
×
139
            throw new TypeError(`${id} not found in ${this.rid}`);
×
140
        }
×
141

×
142
        return item;
×
143
    }
×
144

12✔
145
    indexOf(item: V): number {
12✔
146
        return this._list.indexOf(item);
×
147
    }
×
148

12✔
149
    init(data: Array<V> = []): this {
12✔
150
        this._list = data;
12✔
151

12✔
152
        if (this._idCallback) {
12✔
153
            this._map = {};
6✔
154
            for (const v of this._list) {
6✔
155
                const id = String(this._idCallback(v));
18✔
156
                if (["", "undefined", "null"].includes(id) || id.replaceAll(/\W/g, "") === "") {
18!
157
                    throw new Error("No id for item");
×
158
                }
×
159
                if (this._map[id]) {
18!
160
                    throw new Error(`Duplicate id - ${id}`);
×
161
                }
×
162
                this._map[id] = v;
18✔
163
            }
18✔
164
        }
6✔
165

12✔
166
        return this;
12✔
167
    }
12✔
168

12✔
169
    /**
12✔
170
     * Get the last element, or last X elements if a number is provided.
12✔
171
     * @param amount The amount of elements to get.
12✔
172
     */
12✔
173
    last(): V | undefined;
12✔
174
    last(amount: number): Array<V>;
12✔
175
    last(amount?: number): V | Array<V> | undefined {
12✔
176
        const iterator = Array.from(this._list);
×
177
        if (amount === undefined) {
×
178
            return iterator.at(-1);
×
179
        }
×
180
        if (amount < 0) {
×
181
            return this.first(amount * -1);
×
182
        }
×
183
        if (!amount) {
×
184
            return [];
×
185
        }
×
186

×
187
        return iterator.slice(-amount);
×
188
    }
×
189

12✔
190
    /** See: {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | Array#map } */
12✔
191
    map<T>(predicate: (value: V, index: number, obj: Array<V>) => T, thisArg?: unknown): Array<T> {
12✔
192
        return this.toArray().map(predicate, thisArg);
×
193
    }
×
194

12✔
195
    off(events: string | Array<string> | null, handler: AnyFunction): this {
12✔
196
        this.api.resourceOff(this.rid, events, handler);
×
197
        return this;
×
198
    }
×
199

12✔
200
    on(events: string | Array<string> | null, handler: AnyFunction): this {
12✔
201
        this.api.resourceOn(this.rid, events, handler);
×
202
        return this;
×
203
    }
×
204

12✔
205
    /**
12✔
206
     * Pick a random element from the collection, or undefined if the collection is empty.
12✔
207
     */
12✔
208
    random(): V | undefined {
12✔
209
        if (this.empty) {
×
210
            return undefined;
×
211
        }
×
212
        const iterable = Array.from(this._list);
×
213

×
214
        return iterable[Math.floor(Math.random() * iterable.length)];
×
215
    }
×
216

12✔
217
    /** See: {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | Array#reduce } */
12✔
218
    reduce(predicate: (previousValue: V, currentValue: V, currentIndex: number, array: Array<V>) => V): V;
12✔
219
    reduce(predicate: (previousValue: V, currentValue: V, currentIndex: number, array: Array<V>) => V, initialValue: V): V;
12✔
220
    reduce<T>(predicate: (previousValue: T, currentValue: V, currentIndex: number, array: Array<V>) => T, initialValue: T): T;
12✔
221
    reduce<T>(predicate: (previousValue: T, currentValue: V, currentIndex: number, array: Array<V>) => T, initialValue?: T): T {
12✔
222
        return this.toArray().reduce(predicate, initialValue!);
×
223
    }
×
224

12✔
225
    /** See: {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight | Array#reduceRight } */
12✔
226
    reduceRight(predicate: (previousValue: V, currentValue: V, currentIndex: number, array: Array<V>) => V): V;
12✔
227
    reduceRight(predicate: (previousValue: V, currentValue: V, currentIndex: number, array: Array<V>) => V, initialValue: V): V;
12✔
228
    reduceRight<T>(predicate: (previousValue: T, currentValue: V, currentIndex: number, array: Array<V>) => T, initialValue: T): T;
12✔
229
    reduceRight<T>(predicate: (previousValue: T, currentValue: V, currentIndex: number, array: Array<V>) => T, initialValue?: T): T {
12✔
230
        return this.toArray().reduceRight(predicate, initialValue!);
×
231
    }
×
232

12✔
233
    remove(index: number): V | undefined {
12✔
234
        const item = this._list[index];
2✔
235
        if (item !== undefined) {
2✔
236
            this._list.splice(index, 1);
2✔
237

2✔
238
            if (this._idCallback) {
2✔
239
                delete this._map![this._idCallback(item)];
1✔
240
            }
1✔
241
        }
2✔
242

2✔
243
        return item;
2✔
244
    }
2✔
245

12✔
246
    /** See: {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some | Array#some } */
12✔
247
    some<ThisArg = ResCollection<V>>(predicate: (value: V, index: number, array: Array<V>) => unknown, thisArg?: ThisArg): boolean {
12✔
248
        return this.toArray().some(predicate, thisArg);
×
249
    }
×
250

12✔
251
    /** Get the values of this collection as an array. */
12✔
252
    toArray(): Array<V> {
12✔
253
        return Array.from(this._list);
×
254
    }
×
255

12✔
256
    toJSON(): Array<unknown> {
12✔
257
        return this._list.map(v => (
×
258
            v !== null && typeof v === "object" && "toJSON" in v
×
259
                ? (v as { toJSON(): AnyObject; }).toJSON()
×
260
                : v
×
261
        ));
×
262
    }
×
263
}
12✔
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