Skip to content

Commit 47b040a

Browse files
committed
chore: wip
1 parent 05262e8 commit 47b040a

File tree

1 file changed

+20
-21
lines changed
  • storage/framework/core/collections/src

1 file changed

+20
-21
lines changed

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class Collection<T extends object> {
3535
}
3636

3737
return (
38-
new (this.constructor as new (items: typeof this.items) => typeof this)(this.items).pluck(key).sum() /
38+
new (this.constructor as new (items: typeof this.items) => typeof this)(this.items).pluck(key).sum(...[]) /
3939
this.items.length
4040
)
4141
}
@@ -49,7 +49,7 @@ export class Collection<T extends object> {
4949
if (Array.isArray(this.items)) {
5050
do {
5151
const items = this.items.slice(index, index + size)
52-
const collection = new this.constructor(items)
52+
const collection = new (this.constructor as new (items: typeof this.items) => typeof this)(items)
5353

5454
chunks.push(collection)
5555
index += size
@@ -59,36 +59,36 @@ export class Collection<T extends object> {
5959

6060
do {
6161
const keysOfChunk = keys.slice(index, index + size)
62-
const collection = new this.constructor({})
62+
const collection = new (this.constructor as new (items: typeof this.items) => typeof this)({} as T[])
6363

64-
keysOfChunk.forEach((key) => collection.put(key, this.items[key]))
64+
keysOfChunk.forEach((key: string) => collection.put(key, (this.items as Record<string, any>)[key]))
6565

6666
chunks.push(collection)
6767
index += size
6868
} while (index < keys.length)
6969
} else {
70-
chunks.push(new this.constructor([this.items]))
70+
chunks.push(new (this.constructor as new (items: typeof this.items) => typeof this)([this.items]))
7171
}
7272

73-
return new this.constructor(chunks)
73+
return new (this.constructor as new (items: T[][]) => Collection<T[]>)(chunks as unknown as T[][])
7474
}
7575

76-
collapse(): Collection<T> {
77-
return new this.constructor([].concat(...this.items))
76+
collapse(): Collection<T extends any[] ? T[number] : T> {
77+
return new (this.constructor as any)(([] as T[]).concat(...(this.items as any[])))
7878
}
7979

8080
combine(array: Collection<T> | T[]): Collection<T> {
8181
let values = array
8282

8383
if (values instanceof this.constructor) {
84-
values = array.all()
84+
values = (values as Collection<T>).all()
8585
}
8686

8787
const collection = {}
8888

8989
if (Array.isArray(this.items) && Array.isArray(values)) {
9090
this.items.forEach((key, iterator) => {
91-
collection[key] = values[iterator]
91+
;(collection as Record<string, unknown>)[String(key)] = values[iterator]
9292
})
9393
} else if (typeof this.items === 'object' && typeof values === 'object') {
9494
Object.keys(this.items).forEach((key, index) => {
@@ -334,21 +334,21 @@ export class Collection<T extends object> {
334334
return this
335335
}
336336

337-
eachSpread(fn) {
338-
this.each((values, key) => {
337+
eachSpread(fn: (...args: any[]) => void): Collection<T> {
338+
this.each((values: any, key: any) => {
339339
fn(...values, key)
340340
})
341341

342342
return this
343343
}
344344

345-
every(fn) {
345+
every(fn: (value: T, key: string | number, collection: T[]) => boolean): boolean {
346346
const items = values(this.items)
347347

348348
return items.every(fn)
349349
}
350350

351-
except(...args) {
351+
except(...args: any[]): Collection<T> {
352352
const properties = variadic(args)
353353

354354
if (Array.isArray(this.items)) {
@@ -368,7 +368,7 @@ export class Collection<T extends object> {
368368
return new this.constructor(collection)
369369
}
370370

371-
filter(fn) {
371+
filter(fn: (value: T, key: string | number, collection: T[]) => boolean): Collection<T> {
372372
const func = fn || false
373373
let filteredItems = null
374374
if (Array.isArray(this.items)) {
@@ -380,7 +380,7 @@ export class Collection<T extends object> {
380380
return new this.constructor(filteredItems)
381381
}
382382

383-
first(fn, defaultValue) {
383+
first(fn: (value: T, key: string | number, collection: T[]) => boolean, defaultValue: T): T {
384384
if (isFunction(fn)) {
385385
const keys = Object.keys(this.items)
386386

@@ -417,7 +417,7 @@ export class Collection<T extends object> {
417417
return defaultValue
418418
}
419419

420-
firstOrFail(key, operator, value) {
420+
firstOrFail(key: string, operator: string, value: any): T {
421421
if (isFunction(key)) {
422422
return this.first(key, () => {
423423
throw new Error('Item not found.')
@@ -433,7 +433,7 @@ export class Collection<T extends object> {
433433
return collection.first()
434434
}
435435

436-
firstWhere(key, operator, value) {
436+
firstWhere(key: string, operator: string, value: any): T {
437437
return this.where(key, operator, value).first() || null
438438
}
439439

@@ -1011,10 +1011,9 @@ export class Collection<T extends object> {
10111011
return fn(this)
10121012
}
10131013

1014-
pluck(value, key) {
1014+
pluck(value: string, key?: string) {
10151015
if (value.indexOf('*') !== -1) {
10161016
const keyPathMap = buildKeyPathMap(this.items)
1017-
10181017
const keyMatches = []
10191018

10201019
if (key !== undefined) {
@@ -1594,7 +1593,7 @@ export class Collection<T extends object> {
15941593
return new this.constructor(collection)
15951594
}
15961595

1597-
sum(key) {
1596+
sum(key?: string | ((item: T) => number)): number {
15981597
const items = values(this.items)
15991598

16001599
let total = 0

0 commit comments

Comments
 (0)