|
| 1 | +import { ITERATE_ENTRIES, type IteratorType } from './Iterator'; |
| 2 | +import { IndexedSeq, KeyedSeq, Seq, SetSeq } from './Seq'; |
| 3 | +import type ValueObject from './ValueObject'; |
| 4 | +import { isAssociative } from './predicates/isAssociative'; |
| 5 | +import { isCollection } from './predicates/isCollection'; |
| 6 | +import { isIndexed } from './predicates/isIndexed'; |
| 7 | +import { isKeyed } from './predicates/isKeyed'; |
| 8 | +import assertNotInfinite from './utils/assertNotInfinite'; |
| 9 | +import deepEqual from './utils/deepEqual'; |
| 10 | +import { hashCollection } from './utils/hashCollection'; |
| 11 | + |
| 12 | +export function Collection<I extends CollectionImpl<unknown, unknown>>( |
| 13 | + collection: I |
| 14 | +): I; |
| 15 | +export function Collection<T>( |
| 16 | + collection: Iterable<T> | ArrayLike<T> |
| 17 | +): IndexedCollectionImpl<T>; |
| 18 | +export function Collection<V>(obj: { |
| 19 | + [key: string]: V; |
| 20 | +}): KeyedCollectionImpl<string, V>; |
| 21 | +export function Collection<K = unknown, V = unknown>( |
| 22 | + value: never |
| 23 | +): CollectionImpl<K, V>; |
| 24 | +export function Collection(value: unknown): CollectionImpl<unknown, unknown> { |
| 25 | + return isCollection(value) ? value : Seq(value); |
| 26 | +} |
| 27 | + |
| 28 | +export class CollectionImpl<K, V> implements ValueObject { |
| 29 | + private __hash: number | undefined; |
| 30 | + |
| 31 | + size: number = 0; |
| 32 | + |
| 33 | + equals(other: unknown): boolean { |
| 34 | + return deepEqual(this, other); |
| 35 | + } |
| 36 | + |
| 37 | + hashCode() { |
| 38 | + return this.__hash || (this.__hash = hashCollection(this)); |
| 39 | + } |
| 40 | + |
| 41 | + every( |
| 42 | + predicate: (value: V, key: K, iter: this) => boolean, |
| 43 | + context?: CollectionImpl<K, V> |
| 44 | + ): boolean { |
| 45 | + assertNotInfinite(this.size); |
| 46 | + let returnValue = true; |
| 47 | + this.__iterate((v, k, c) => { |
| 48 | + if (!predicate.call(context, v, k, c)) { |
| 49 | + returnValue = false; |
| 50 | + return false; |
| 51 | + } |
| 52 | + }); |
| 53 | + return returnValue; |
| 54 | + } |
| 55 | + |
| 56 | + entries() { |
| 57 | + return this.__iterator(ITERATE_ENTRIES); |
| 58 | + } |
| 59 | + |
| 60 | + __iterate( |
| 61 | + fn: (value: V, index: K, iter: this) => boolean, |
| 62 | + reverse?: boolean |
| 63 | + ): number; |
| 64 | + __iterate( |
| 65 | + fn: (value: V, index: K, iter: this) => void, |
| 66 | + reverse?: boolean |
| 67 | + ): void; |
| 68 | + __iterate( |
| 69 | + fn: (value: V, index: K, iter: this) => boolean | void, |
| 70 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 71 | + reverse: boolean = false |
| 72 | + ): number | void { |
| 73 | + throw new Error( |
| 74 | + 'CollectionImpl does not implement __iterate. Use a subclass instead.' |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + __iterator( |
| 79 | + type: IteratorType, |
| 80 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 81 | + reverse: boolean = false |
| 82 | + ): Iterator<K | V | [K, V]> { |
| 83 | + throw new Error( |
| 84 | + 'CollectionImpl does not implement __iterator. Use a subclass instead.' |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Always returns a Seq.Keyed, if input is not keyed, expects an |
| 91 | + * collection of [K, V] tuples. |
| 92 | + * |
| 93 | + * Note: `Seq.Keyed` is a conversion function and not a class, and does not |
| 94 | + * use the `new` keyword during construction. |
| 95 | + */ |
| 96 | +export function KeyedCollection<K, V>( |
| 97 | + collection?: Iterable<[K, V]> |
| 98 | +): KeyedCollectionImpl<K, V>; |
| 99 | +export function KeyedCollection<V>(obj: { |
| 100 | + [key: string]: V; |
| 101 | +}): KeyedCollectionImpl<string, V>; |
| 102 | +export function KeyedCollection( |
| 103 | + value: unknown |
| 104 | +): KeyedCollectionImpl<unknown, unknown> { |
| 105 | + return isKeyed(value) ? value : KeyedSeq(value); |
| 106 | +} |
| 107 | + |
| 108 | +export class KeyedCollectionImpl<K, V> extends CollectionImpl<K, V> {} |
| 109 | + |
| 110 | +export function IndexedCollection<T>( |
| 111 | + value: Iterable<T> | ArrayLike<T> |
| 112 | +): IndexedCollectionImpl<T> { |
| 113 | + return isIndexed(value) ? value : IndexedSeq(value); |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Interface representing all oredered collections. |
| 118 | + * This includes `List`, `Stack`, `Map`, `OrderedMap`, `Set`, and `OrderedSet`. |
| 119 | + * return of `isOrdered()` return true in that case. |
| 120 | + */ |
| 121 | +interface OrderedCollection<T> { |
| 122 | + /** |
| 123 | + * Shallowly converts this collection to an Array. |
| 124 | + */ |
| 125 | + toArray(): Array<T>; |
| 126 | + |
| 127 | + [Symbol.iterator](): IterableIterator<T>; |
| 128 | +} |
| 129 | + |
| 130 | +export class IndexedCollectionImpl<T> |
| 131 | + extends CollectionImpl<number, T> |
| 132 | + implements OrderedCollection<T> |
| 133 | +{ |
| 134 | + declare toArray: () => T[]; |
| 135 | + |
| 136 | + declare [Symbol.iterator]: () => IterableIterator<T>; |
| 137 | +} |
| 138 | + |
| 139 | +export function SetCollection<T>( |
| 140 | + value: Iterable<T> | ArrayLike<T> |
| 141 | +): SetCollectionImpl<T> { |
| 142 | + return isCollection(value) && !isAssociative(value) ? value : SetSeq(value); |
| 143 | +} |
| 144 | + |
| 145 | +export class SetCollectionImpl<T> extends CollectionImpl<T, T> {} |
| 146 | + |
| 147 | +Collection.Keyed = KeyedCollection; |
| 148 | +Collection.Indexed = IndexedCollection; |
| 149 | +Collection.Set = SetCollection; |
0 commit comments