Skip to content

Commit

Permalink
feat: extend collection methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Aybrea committed Nov 3, 2024
1 parent 543d6be commit d4b5e45
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
isWeakMap,
} from './general'

type CollectionByKey<Member> = Record<number | string, Member>
type AnyLiteral = Record<string, any>

export function mergeWith<TObject extends Record<string, any>, TSource extends Record<string, any>>(
object: TObject,
source: TSource,
Expand Down Expand Up @@ -167,3 +170,76 @@ export function cloneDeepWith<T>(value: T, fn: (value: any) => any): T {

return baseCloneDeep(value, cache)
}

export function buildCollection<T extends AnyLiteral, K extends keyof T | number | string, R = T>(
collection: T[],
keyOrCallback: keyof T | ((member: T) => [K, R]),
): Record<K, R> {
return collection.reduce(
(byKey: Record<K, R>, member: T) => {

Check warning on line 179 in src/collection.ts

View check run for this annotation

Codecov / codecov/patch

src/collection.ts#L177-L179

Added lines #L177 - L179 were not covered by tests
if (typeof keyOrCallback === 'function') {
const [key, value] = keyOrCallback(member)
byKey[key] = value
} else {
byKey[member[keyOrCallback] as K] = member as unknown as R

Check warning on line 184 in src/collection.ts

View check run for this annotation

Codecov / codecov/patch

src/collection.ts#L181-L184

Added lines #L181 - L184 were not covered by tests
}

return byKey

Check warning on line 187 in src/collection.ts

View check run for this annotation

Codecov / codecov/patch

src/collection.ts#L187

Added line #L187 was not covered by tests
},
{} as Record<K, R>,
)
}

export function mapValues<R, M>(
byKey: CollectionByKey<M>,
callback: (member: M, key: string, index: number, originalByKey: CollectionByKey<M>) => R,
): CollectionByKey<R> {
return Object.keys(byKey).reduce((newByKey: CollectionByKey<R>, key, index) => {
newByKey[key] = callback(byKey[key], key, index, byKey)
return newByKey

Check warning on line 199 in src/collection.ts

View check run for this annotation

Codecov / codecov/patch

src/collection.ts#L196-L199

Added lines #L196 - L199 were not covered by tests
}, {})
}

export function pick<T, K extends keyof T>(object: T, keys: K[]) {
return keys.reduce(
(result, key) => {
result[key] = object[key]
return result

Check warning on line 207 in src/collection.ts

View check run for this annotation

Codecov / codecov/patch

src/collection.ts#L203-L207

Added lines #L203 - L207 were not covered by tests
},
{} as Pick<T, K>,
)
}

export function pickTruthy<T, K extends keyof T>(object: T, keys: K[]) {
return keys.reduce(
(result, key) => {

Check warning on line 215 in src/collection.ts

View check run for this annotation

Codecov / codecov/patch

src/collection.ts#L213-L215

Added lines #L213 - L215 were not covered by tests
if (object[key]) {
result[key] = object[key]

Check warning on line 217 in src/collection.ts

View check run for this annotation

Codecov / codecov/patch

src/collection.ts#L217

Added line #L217 was not covered by tests
}

return result

Check warning on line 220 in src/collection.ts

View check run for this annotation

Codecov / codecov/patch

src/collection.ts#L220

Added line #L220 was not covered by tests
},
{} as Pick<T, K>,
)
}

export function omit<T extends object, K extends keyof T>(object: T, keys: K[]): Omit<T, K> {
const stringKeys = new Set(keys.map(String))
const savedKeys = Object.keys(object).filter((key) => !stringKeys.has(key)) as Array<Exclude<keyof T, K>>

Check warning on line 228 in src/collection.ts

View check run for this annotation

Codecov / codecov/patch

src/collection.ts#L226-L228

Added lines #L226 - L228 were not covered by tests

return pick(object, savedKeys)

Check warning on line 230 in src/collection.ts

View check run for this annotation

Codecov / codecov/patch

src/collection.ts#L230

Added line #L230 was not covered by tests
}

export function omitUndefined<T extends object>(object: T): T {
return Object.keys(object).reduce((result, stringKey) => {
const key = stringKey as keyof T

Check warning on line 235 in src/collection.ts

View check run for this annotation

Codecov / codecov/patch

src/collection.ts#L233-L235

Added lines #L233 - L235 were not covered by tests
if (object[key] !== undefined) {
result[key as keyof T] = object[key]

Check warning on line 237 in src/collection.ts

View check run for this annotation

Codecov / codecov/patch

src/collection.ts#L237

Added line #L237 was not covered by tests
}
return result

Check warning on line 239 in src/collection.ts

View check run for this annotation

Codecov / codecov/patch

src/collection.ts#L239

Added line #L239 was not covered by tests
}, {} as T)
}

export function isLiteralObject(value: any): value is AnyLiteral {

Check warning on line 243 in src/collection.ts

View check run for this annotation

Codecov / codecov/patch

src/collection.ts#L243

Added line #L243 was not covered by tests
return isObject(value) && !Array.isArray(value)
}

0 comments on commit d4b5e45

Please sign in to comment.