-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmptyCollectionHolder.ts
564 lines (387 loc) · 24 KB
/
EmptyCollectionHolder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*******************************************************************************
Copyright (c) 2023-2024. Jonathan Bédard ~ JóôòKiwi
This project is free to use.
All the right is reserved to the author of this project.
******************************************************************************/
import type {Nullable, NullableString, NumericOrObject, TemplateOrNumber} from "@joookiwi/type"
import type {CollectionHolder} from "./CollectionHolder"
import type {CollectionHolderName, IndexWithReturnCallback, PossibleIterableArraySetOrCollectionHolder} from "./CollectionHolder.types"
import type {CollectionIterator} from "./iterator/CollectionIterator"
import type {EmptyCollectionIterator} from "./iterator/EmptyCollectionIterator"
import type {MinimalistCollectionHolder} from "./MinimalistCollectionHolder"
import {EmptyCollectionHolderException} from "./exception/EmptyCollectionHolderException"
import {CollectionConstants} from "./CollectionConstants"
import {isArray} from "./method/isArray"
import {isArrayByStructure} from "./method/isArrayByStructure"
import {isCollectionIterator} from "./method/isCollectionIterator"
import {isCollectionIteratorByStructure} from "./method/isCollectionIteratorByStructure"
import {isCollectionHolder} from "./method/isCollectionHolder"
import {isCollectionHolderByStructure} from "./method/isCollectionHolderByStructure"
import {isMinimalistCollectionHolder} from "./method/isMinimalistCollectionHolder"
import {isMinimalistCollectionHolderByStructure} from "./method/isMinimalistCollectionHolderByStructure"
import {isSet} from "./method/isSet"
import {isSetByStructure} from "./method/isSetByStructure"
import {prefixAndPostfixOnly} from "./method/joinToString"
/**
* A {@link CollectionHolder} with no values (as a singleton instance)
*
* @see GenericMinimalistCollectionHolder
* @see GenericCollectionHolder
* @see LazyGenericCollectionHolder
* @singleton
*/
export class EmptyCollectionHolder
implements CollectionHolder<never> {
//#region -------------------- Singleton usage --------------------
static #instance?: EmptyCollectionHolder
protected constructor() {}
public static get get(): EmptyCollectionHolder {
return EmptyCollectionHolder.#instance ??= new EmptyCollectionHolder()
}
//#endregion -------------------- Singleton usage --------------------
//#region -------------------- Fields --------------------
[index: TemplateOrNumber]: undefined
//#endregion -------------------- Fields --------------------
//#region -------------------- Methods --------------------
//#region -------------------- Size methods --------------------
public get size(): 0 { return 0 }
public get length(): 0 { return this.size }
public get count(): 0 { return this.size }
public get isEmpty(): true { return true }
public get isNotEmpty(): false { return false }
//#endregion -------------------- Size methods --------------------
//#region -------------------- Research methods --------------------
//#region -------------------- Get --------------------
public get(index?: Nullable<NumericOrObject>, ..._: readonly unknown[]): never
public get(index?: Nullable<NumericOrObject>,) {
throw new EmptyCollectionHolderException(null, index,)
}
public at(index?: Nullable<NumericOrObject>, ..._: readonly unknown[]): never
public at(index?: Nullable<NumericOrObject>,) { return this.get(index,) }
public elementAt(index?: Nullable<NumericOrObject>, ..._: readonly unknown[]): never
public elementAt(index?: Nullable<NumericOrObject>,) { return this.get(index,) }
public getOrElse<const U, >(index: number, defaultValue: IndexWithReturnCallback<U>, ..._: readonly unknown[]): U
public getOrElse(index: number, defaultValue: IndexWithReturnCallback<never>, ..._: readonly unknown[]): never
public getOrElse(index: number, defaultValue: IndexWithReturnCallback<unknown>,) {
return defaultValue(index,)
}
public atOrElse<const U, >(index: number, defaultValue: IndexWithReturnCallback<U>, ..._: readonly unknown[]): U
public atOrElse(index: number, defaultValue: IndexWithReturnCallback<never>, ..._: readonly unknown[]): never
public atOrElse(index: number, defaultValue: IndexWithReturnCallback<unknown>,) { return this.getOrElse(index, defaultValue,) }
public elementAtOrElse<const U, >(index: number, defaultValue: IndexWithReturnCallback<U>, ..._: readonly unknown[]): U
public elementAtOrElse(index: number, defaultValue: IndexWithReturnCallback<never>, ..._: readonly unknown[]): never
public elementAtOrElse(index: number, defaultValue: IndexWithReturnCallback<unknown>,) { return this.getOrElse(index, defaultValue,) }
public getOrNull(..._: readonly unknown[]): null
public getOrNull() { return null }
public atOrNull(..._: readonly unknown[]): null
public atOrNull() { return this.getOrNull() }
public elementAtOrNull(..._: readonly unknown[]): null
public elementAtOrNull() { return this.getOrNull() }
//#endregion -------------------- Get --------------------
//#region -------------------- First --------------------
public first<const S, >(..._: readonly unknown[]): never
public first() { throw new EmptyCollectionHolderException() }
public firstOrNull<const S, >(..._: readonly unknown[]): null
public firstOrNull() { return null }
//#endregion -------------------- First --------------------
//#region -------------------- Last --------------------
public last<const S, >(..._: readonly unknown[]): never
public last() { throw new EmptyCollectionHolderException() }
public lastOrNull<const S, >(..._: readonly unknown[]): null
public lastOrNull() { return null }
//#endregion -------------------- Last --------------------
//#region -------------------- Find --------------------
public find<const S, >(..._: readonly unknown[]): null
public find() { return null }
public findIndexed<const S, >(..._: readonly unknown[]): null
public findIndexed() { return null }
public findLast<const S, >(..._: readonly unknown[]): null
public findLast() { return null }
public findLastIndexed<const S, >(..._: readonly unknown[]): null
public findLastIndexed() { return null }
//#endregion -------------------- Find --------------------
//#endregion -------------------- Research methods --------------------
//#region -------------------- Index methods --------------------
public indexOf(..._: readonly unknown[]): null
public indexOf() { return null }
public lastIndexOf(..._: readonly unknown[]): null
public lastIndexOf() { return null }
public indexOfFirst(..._: readonly unknown[]): null
public indexOfFirst() { return null }
public indexOfFirstIndexed(..._: readonly unknown[]): null
public indexOfFirstIndexed() { return null }
public indexOfLast(..._: readonly unknown[]): null
public indexOfLast() { return null }
public indexOfLastIndexed(..._: readonly unknown[]): null
public indexOfLastIndexed() { return null }
//#endregion -------------------- Index methods --------------------
//#region -------------------- Validation methods --------------------
//#region -------------------- All --------------------
/** @return {true} */
public all<const S extends never, >(..._: readonly unknown[]): this is CollectionHolder<S>
public all() {
return true
}
/** @return {true} */
public every<const S extends never, >(..._: readonly unknown[]): this is CollectionHolder<S>
public every() {
return true
}
//#endregion -------------------- All --------------------
//#region -------------------- Any --------------------
public any(..._: readonly unknown[]): false
public any() { return false }
public some(..._: readonly unknown[]): false
public some() { return false }
//#endregion -------------------- Any --------------------
//#region -------------------- None --------------------
public none(..._: readonly unknown[]): true
public none() { return true }
//#endregion -------------------- None --------------------
//#region -------------------- Has null --------------------
public get hasNull(): false { return false }
public get includesNull(): false { return this.hasNull }
public get containsNull(): false { return this.hasNull }
//#endregion -------------------- Has null --------------------
//#region -------------------- Has duplicate --------------------
public get hasDuplicate(): false { return false }
public get includesDuplicate(): false { return this.hasDuplicate }
public get containsDuplicate(): false { return this.hasDuplicate }
//#endregion -------------------- Has duplicate --------------------
//#region -------------------- Has --------------------
public has(..._: readonly unknown[]): false
public has() { return false }
public includes(..._: readonly unknown[]): false
public includes() { return this.has() }
public contains(..._: readonly unknown[]): false
public contains() { return this.has() }
//#endregion -------------------- Has --------------------
//#region -------------------- Has one --------------------
public hasOne(..._: readonly unknown[]): false
public hasOne() { return false }
public includesOne(..._: readonly unknown[]): false
public includesOne() { return this.hasOne() }
public containsOne(..._: readonly unknown[]): false
public containsOne() { return this.hasOne() }
//#endregion -------------------- Has one --------------------
//#region -------------------- Has all --------------------
public hasAll(values: readonly never[], ..._: readonly unknown[]): boolean
public hasAll(values: ReadonlySet<never>, ..._: readonly unknown[]): boolean
public hasAll(values: CollectionHolder<never>, ..._: readonly unknown[]): boolean
public hasAll(values: MinimalistCollectionHolder<never>, ..._: readonly unknown[]): boolean
public hasAll(values: CollectionIterator<never>, ..._: readonly unknown[]): boolean
public hasAll(values: Iterable<never>, ..._: readonly unknown[]): boolean
public hasAll(values: PossibleIterableArraySetOrCollectionHolder<never>, ..._: readonly unknown[]): boolean
public hasAll(values: readonly unknown[], ..._: readonly unknown[]): boolean
public hasAll(values: ReadonlySet<unknown>, ..._: readonly unknown[]): boolean
public hasAll(values: CollectionHolder, ..._: readonly unknown[]): boolean
public hasAll(values: MinimalistCollectionHolder, ..._: readonly unknown[]): boolean
public hasAll(values: CollectionIterator, ..._: readonly unknown[]): boolean
public hasAll(values: Iterable<unknown>, ..._: readonly unknown[]): boolean
public hasAll(values: PossibleIterableArraySetOrCollectionHolder<unknown>, ..._: readonly unknown[]): boolean
public hasAll(values: PossibleIterableArraySetOrCollectionHolder<unknown>,) {
if (isArray(values,))
return values.length == 0
if (isSet(values,))
return values.size == 0
if (isCollectionHolder(values,))
return values.isEmpty
if (isMinimalistCollectionHolder(values,))
return values.size == 0
if (isCollectionIterator(values,))
return values.size == 0
if (isArrayByStructure(values,))
return values.length == 0
if (isSetByStructure(values,))
return values.size == 0
if (isCollectionHolderByStructure<never>(values,))
return values.isEmpty
if (isMinimalistCollectionHolderByStructure<never>(values,))
return values.size == 0
if (isCollectionIteratorByStructure<never>(values,))
return values.size == 0
return values[Symbol.iterator]().next().done
}
public includesAll(values: readonly never[], ..._: readonly unknown[]): boolean
public includesAll(values: ReadonlySet<never>, ..._: readonly unknown[]): boolean
public includesAll(values: CollectionHolder<never>, ..._: readonly unknown[]): boolean
public includesAll(values: MinimalistCollectionHolder<never>, ..._: readonly unknown[]): boolean
public includesAll(values: CollectionIterator<never>, ..._: readonly unknown[]): boolean
public includesAll(values: Iterable<never>, ..._: readonly unknown[]): boolean
public includesAll(values: PossibleIterableArraySetOrCollectionHolder<never>, ..._: readonly unknown[]): boolean
public includesAll(values: readonly unknown[], ..._: readonly unknown[]): boolean
public includesAll(values: ReadonlySet<unknown>, ..._: readonly unknown[]): boolean
public includesAll(values: CollectionHolder, ..._: readonly unknown[]): boolean
public includesAll(values: MinimalistCollectionHolder, ..._: readonly unknown[]): boolean
public includesAll(values: CollectionIterator, ..._: readonly unknown[]): boolean
public includesAll(values: Iterable<unknown>, ..._: readonly unknown[]): boolean
public includesAll(values: PossibleIterableArraySetOrCollectionHolder<unknown>, ..._: readonly unknown[]): boolean
public includesAll(values: PossibleIterableArraySetOrCollectionHolder<unknown>,) { return this.hasAll(values,) }
public containsAll(values: readonly never[], ..._: readonly unknown[]): boolean
public containsAll(values: ReadonlySet<never>, ..._: readonly unknown[]): boolean
public containsAll(values: CollectionHolder<never>, ..._: readonly unknown[]): boolean
public containsAll(values: MinimalistCollectionHolder<never>, ..._: readonly unknown[]): boolean
public containsAll(values: CollectionIterator<never>, ..._: readonly unknown[]): boolean
public containsAll(values: Iterable<never>, ..._: readonly unknown[]): boolean
public containsAll(values: PossibleIterableArraySetOrCollectionHolder<never>, ..._: readonly unknown[]): boolean
public containsAll(values: readonly unknown[], ..._: readonly unknown[]): boolean
public containsAll(values: ReadonlySet<unknown>, ..._: readonly unknown[]): boolean
public containsAll(values: CollectionHolder, ..._: readonly unknown[]): boolean
public containsAll(values: MinimalistCollectionHolder, ..._: readonly unknown[]): boolean
public containsAll(values: CollectionIterator, ..._: readonly unknown[]): boolean
public containsAll(values: Iterable<unknown>, ..._: readonly unknown[]): boolean
public containsAll(values: PossibleIterableArraySetOrCollectionHolder<unknown>, ..._: readonly unknown[]): boolean
public containsAll(values: PossibleIterableArraySetOrCollectionHolder<unknown>,) { return this.hasAll(values,) }
//#endregion -------------------- Has all --------------------
//#region -------------------- Require no nulls --------------------
public requireNoNulls(..._: readonly unknown[]): this
public requireNoNulls() { return this }
//#endregion -------------------- Require no nulls --------------------
//#endregion -------------------- Validation methods --------------------
//#region -------------------- Transformation methods --------------------
//#region -------------------- Filter --------------------
public filter<const S, >(..._: readonly unknown[]): this
public filter() { return this }
public filterIndexed<const S, >(..._: readonly unknown[]): this
public filterIndexed() { return this }
public filterNot<const S, >(..._: readonly unknown[]): this
public filterNot() { return this }
public filterNotIndexed<const S, >(..._: readonly unknown[]): this
public filterNotIndexed() { return this }
public filterNotNull(..._: readonly unknown[]): this
public filterNotNull() { return this }
//#endregion -------------------- Filter --------------------
//#region -------------------- Slice --------------------
public slice(..._: readonly unknown[]): this
public slice() { return this }
//#endregion -------------------- Slice --------------------
//#region -------------------- Take --------------------
public take(..._: readonly unknown[]): this
public take() { return this }
public takeWhile(..._: readonly unknown[]): this
public takeWhile() { return this }
public takeWhileIndexed(..._: readonly unknown[]): this
public takeWhileIndexed() { return this }
public takeLast(..._: readonly unknown[]): this
public takeLast() { return this }
public takeLastWhile(..._: readonly unknown[]): this
public takeLastWhile() { return this }
public takeLastWhileIndexed(..._: readonly unknown[]): this
public takeLastWhileIndexed() { return this }
//#endregion -------------------- Take --------------------
//#region -------------------- Drop --------------------
public drop(..._: readonly unknown[]): this
public drop() { return this }
public dropWhile(..._: readonly unknown[]): this
public dropWhile() { return this }
public dropWhileIndexed(..._: readonly unknown[]): this
public dropWhileIndexed() { return this }
public dropLast(..._: readonly unknown[]): this
public dropLast() { return this }
public dropLastWhile(..._: readonly unknown[]): this
public dropLastWhile() { return this }
public dropLastWhileIndexed(..._: readonly unknown[]): this
public dropLastWhileIndexed() { return this }
//#endregion -------------------- Drop --------------------
//#region -------------------- Map --------------------
public map<const U, >(..._: readonly unknown[]): CollectionHolder<U>
public map() { return this }
public mapIndexed<const U, >(..._: readonly unknown[]): CollectionHolder<U>
public mapIndexed() { return this }
public mapNotNull<const U extends NonNullable<unknown>, >(..._: readonly unknown[]): CollectionHolder<U>
public mapNotNull() { return this }
public mapNotNullIndexed<const U extends NonNullable<unknown>, >(..._: readonly unknown[]): CollectionHolder<U>
public mapNotNullIndexed() { return this }
//#endregion -------------------- Map --------------------
//#endregion -------------------- Transformation methods --------------------
//#region -------------------- Loop methods --------------------
//#region -------------------- For each --------------------
public forEach(..._: readonly unknown[]): void
public forEach() {}
public forEachIndexed(..._: readonly unknown[]): void
public forEachIndexed() {}
//#endregion -------------------- For each --------------------
//#region -------------------- On each --------------------
public onEach(..._: readonly unknown[]): this
public onEach() { return this }
public onEachIndexed(..._: readonly unknown[]): this
public onEachIndexed() { return this }
//#endregion -------------------- On each --------------------
//#endregion -------------------- Loop methods --------------------
//#region -------------------- Reordering methods --------------------
//#region -------------------- to reverse --------------------
public toReverse(..._: readonly unknown[]): this
public toReverse() { return this }
public toReversed(..._: readonly unknown[]): this
public toReversed() { return this }
public reversed(..._: readonly unknown[]): this
public reversed() { return this }
//#endregion -------------------- to reverse --------------------
//#endregion -------------------- Reordering methods --------------------
//#region -------------------- Javascript methods --------------------
public [Symbol.iterator](..._: readonly unknown[]): EmptyCollectionIterator
public [Symbol.iterator]() { return CollectionConstants.EMPTY_COLLECTION_ITERATOR }
public get [Symbol.toStringTag](): CollectionHolderName { return "CollectionHolder" }
//#endregion -------------------- Javascript methods --------------------
//#region -------------------- Conversion methods --------------------
//#region -------------------- To iterator --------------------
public toIterator(..._: readonly unknown[]): EmptyCollectionIterator
public toIterator() { return this[Symbol.iterator]() }
//#endregion -------------------- To iterator --------------------
//#region -------------------- To array --------------------
public toArray(..._: readonly unknown[]): readonly never[]
public toArray() { return CollectionConstants.EMPTY_ARRAY }
public toMutableArray(..._: readonly unknown[]): never[]
public toMutableArray() { return [] }
//#endregion -------------------- To array --------------------
//#region -------------------- To set --------------------
public toSet(..._: readonly unknown[]): ReadonlySet<never>
public toSet() { return CollectionConstants.EMPTY_SET }
public toMutableSet(..._: readonly unknown[]): Set<never>
public toMutableSet() { return new Set() }
//#endregion -------------------- To set --------------------
//#region -------------------- To weak set --------------------
public toWeakSet(..._: readonly unknown[]): Readonly<WeakSet<never>>
public toWeakSet() { return CollectionConstants.EMPTY_WEAK_SET }
public toMutableWeakSet(..._: readonly unknown[]): WeakSet<never>
public toMutableWeakSet() { return new WeakSet() }
//#endregion -------------------- To weak set --------------------
//#region -------------------- To map --------------------
public toMap(..._: readonly unknown[]): ReadonlyMap<never, never>
public toMap() { return CollectionConstants.EMPTY_MAP }
public toMutableMap(..._: readonly unknown[]): Map<never, never>
public toMutableMap() { return new Map() }
//#endregion -------------------- To map --------------------
//#region -------------------- To weak map --------------------
public toWeakMap(..._: readonly unknown[]): Readonly<WeakMap<never, never>>
public toWeakMap() { return CollectionConstants.EMPTY_WEAK_MAP }
public toMutableWeakMap(..._: readonly unknown[]): WeakMap<never, never>
public toMutableWeakMap() { return new WeakMap() }
//#endregion -------------------- To weak map --------------------
//#region -------------------- To string --------------------
public toString(..._: readonly unknown[]): "[]"
public toString() { return "[]" }
public toLocaleString(..._: readonly unknown[]): "[]"
public toLocaleString() { return "[]" }
public toLowerCaseString(..._: readonly unknown[]): "[]"
public toLowerCaseString() { return "[]" }
public toLocaleLowerCaseString(..._: readonly unknown[]): "[]"
public toLocaleLowerCaseString() { return "[]" }
public toUpperCaseString(..._: readonly unknown[]): "[]"
public toUpperCaseString() { return "[]" }
public toLocaleUpperCaseString(..._: readonly unknown[]): "[]"
public toLocaleUpperCaseString() { return "[]" }
//#endregion -------------------- To string --------------------
//#region -------------------- Join to string --------------------
public join(separator?: unknown, prefix?: NullableString, postfix?: NullableString, ..._: readonly unknown[]): string
public join(separator?: unknown, prefix?: NullableString, postfix?: NullableString,) {
return this.joinToString(separator, prefix, postfix,)
}
public joinToString(separator?: unknown, prefix?: NullableString, postfix?: NullableString, ..._: readonly unknown[]): string
public joinToString(_separator?: unknown, prefix?: NullableString, postfix?: NullableString,) {
return prefixAndPostfixOnly(prefix, postfix,)
}
//#endregion -------------------- Join to string --------------------
//#endregion -------------------- Conversion methods --------------------
//#endregion -------------------- Methods --------------------
}