Skip to content

Commit 4b290b1

Browse files
committed
chore: adjust types to handle empty collections
1 parent b190416 commit 4b290b1

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/collect.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -385,33 +385,34 @@ function createCollectionOperations<T>(collection: Collection<T>): CollectionOpe
385385
})
386386
},
387387

388-
pad(size: number, value: T) {
389-
const result = [...collection.items]
388+
pad<U = T>(size: number, value: U): CollectionOperations<T | U> {
389+
const result: Array<T | U> = collection.items.map(item => item as T | U)
390390
const padSize = Math.abs(size)
391-
const padValue = value
392391

393392
while (result.length < padSize) {
394-
size > 0 ? result.push(padValue) : result.unshift(padValue)
393+
size > 0 ? result.push(value) : result.unshift(value)
395394
}
396395

397-
return collect(result)
396+
return collect<T | U>(result)
398397
},
399398

400399
pop() {
401400
return collection.items.pop()
402401
},
403402

404-
prepend(value: T) {
405-
return collect([value, ...collection.items])
403+
prepend<U = T>(value: U): CollectionOperations<T | U> {
404+
const result: Array<T | U> = [value, ...collection.items.map(item => item as T | U)]
405+
return collect<T | U>(result)
406406
},
407407

408408
pull<K extends keyof T>(key: K) {
409409
const item = collection.items[0]
410410
return item ? item[key] : undefined
411411
},
412412

413-
push(value: T) {
414-
return collect([...collection.items, value])
413+
push<U = T>(value: U): CollectionOperations<T | U> {
414+
const result: Array<T | U> = [...collection.items.map(item => item as T | U), value]
415+
return collect<T | U>(result)
415416
},
416417

417418
put<K extends string, V>(key: K, value: V): CollectionOperations<any> {

src/types.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ export interface CollectionOperations<T> extends Collection<T> {
207207
}
208208
containsOneItem: () => boolean
209209
containsAll: {
210-
(items: Array<T | undefined>): boolean; // Handle direct items check with optional undefined
211-
<K extends keyof T>(key: K, values: Array<T[K] | undefined>): boolean; // Handle key/value check
210+
(items: Array<T | undefined>): boolean // Handle direct items check with optional undefined
211+
<K extends keyof T>(key: K, values: Array<T[K] | undefined>): boolean // Handle key/value check
212212
}
213213
countBy: {
214214
<K extends keyof T>(key: K): Map<T[K], number>
@@ -246,11 +246,14 @@ export interface CollectionOperations<T> extends Collection<T> {
246246
only: <K extends string>(...keys: K[]) => CollectionOperations<{
247247
[P in K & keyof T]?: T[P]
248248
}>
249-
pad: (size: number, value: T) => CollectionOperations<T>
249+
// eslint-disable-next-line ts/method-signature-style
250+
pad<U = T>(size: number, value: U): CollectionOperations<T | U>
250251
pop: () => T | undefined
251-
prepend: (value: T) => CollectionOperations<T>
252+
// eslint-disable-next-line ts/method-signature-style
253+
prepend<U = T>(value: U): CollectionOperations<T | U>
252254
pull: <K extends keyof T>(key: K) => T[K] | undefined
253-
push: (value: T) => CollectionOperations<T>
255+
// eslint-disable-next-line ts/method-signature-style
256+
push<U = T>(value: U): CollectionOperations<T | U>
254257
// First overload handles existing keys
255258
// eslint-disable-next-line ts/method-signature-style
256259
put<K extends keyof T>(key: K, value: T[K]): CollectionOperations<T>

0 commit comments

Comments
 (0)