Skip to content

Commit 03c92fe

Browse files
committed
chore: wip
1 parent 6287089 commit 03c92fe

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

storage/framework/core/collections/src/index.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { clone, deleteKeys, isArray, isFunction, isObject, nestedValue, values, variadic } from './helpers/'
1+
import { clone, deleteKeys, isArray, isFunction, isObject, nestedValue, values } from './helpers/'
22

33
export class Collection<T> {
44
protected items: T[] | Record<string, T>
@@ -13,8 +13,8 @@ export class Collection<T> {
1313
return this.items
1414
}
1515

16-
all(): T[] | Record<string, T> {
17-
return this.items
16+
all(): CollectionAll<T> {
17+
return this.items as CollectionAll<T>
1818
}
1919

2020
average(key?: string | ((item: T) => number)): number {
@@ -48,9 +48,11 @@ export class Collection<T> {
4848
return new (this.constructor as any)(([] as T[]).concat(...(this.items as any[])))
4949
}
5050

51-
combine(values: T[]): Collection<T> {
51+
combine<U extends unknown[]>(values: U): Collection<{ [K in T[number]]: U[number] }> {
5252
if (Array.isArray(this.items) && Array.isArray(values)) {
53-
return new Collection(Object.fromEntries(this.items.map((key, index) => [key, values[index]])))
53+
return new Collection(Object.fromEntries(this.items.map((key, index) => [key, values[index]]))) as Collection<{
54+
[K in T[number]]: U[number]
55+
}>
5456
}
5557

5658
throw new Error('Cannot combine non-array collections')
@@ -1949,7 +1951,11 @@ function SymbolIterator() {
19491951
}
19501952
}
19511953

1952-
export const collect = <T extends object | number>(collection?: T | T[] | Collection<T>): Collection<T> =>
1953-
new Collection<T>(collection)
1954+
export function collect<T extends object | any[]>(items: T): Collection<T> {
1955+
return new Collection(items)
1956+
}
1957+
1958+
type IsArray<T> = T extends any[] ? true : false
1959+
type CollectionAll<T> = IsArray<T> extends true ? T : { [K in keyof T]: T[K] }
19541960

19551961
export default collect

storage/framework/core/collections/tests/collections.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('@stacksjs/collections', () => {
99

1010
describe('all() method', () => {
1111
it('should return all items, simple array', () => {
12-
expect(collect<number>([1, 2, 3, 4, 5]).all()).toEqual([1, 2, 3, 4, 5])
12+
expect(collect([1, 2, 3, 4, 5]).all()).toEqual([1, 2, 3, 4, 5])
1313
})
1414

1515
it('should recursively return all items', () => {
@@ -76,7 +76,7 @@ describe('@stacksjs/collections', () => {
7676
})
7777

7878
it('collapse()', () => {
79-
const collection = collect<number | number[]>([[1], [2, 3], [4, 5]])
79+
const collection = collect([[1], [2, 3], [4, 5]])
8080
expect(collection.collapse().all()).toEqual([1, 2, 3, 4, 5])
8181
})
8282

0 commit comments

Comments
 (0)