From 608ae350c369bd620bf2fcae87c0f3e8ee48cc52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Antunes=20Silva?= Date: Mon, 7 Dec 2020 13:45:00 -0300 Subject: [PATCH] feat(types): add type `Obj` --- src/Collection.ts | 31 ++++++++++++++----------------- src/types/index.ts | 3 ++- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/Collection.ts b/src/Collection.ts index e0b7d30..f4eed98 100644 --- a/src/Collection.ts +++ b/src/Collection.ts @@ -21,6 +21,7 @@ import type { ItemOrDefault, Key, KeyVariadic, + Obj, Operator, ValueOf } from './types' @@ -198,12 +199,10 @@ export default class Collection< * @param {Function} callback * @return {Object} */ - public countBy( - callback: (item: Item, index: number) => Key - ): Record { + public countBy(callback: (item: Item, index: number) => Key): Obj { const group = this.groupBy(callback) - return Object.keys(group).reduce((result: Record, key) => { + return Object.keys(group).reduce((result: Obj, key) => { result[key] = (group[key] as unknown[]).length return result }, {}) @@ -483,9 +482,9 @@ export default class Collection< * @param {Collection} [collection] * @return {Object} */ - public getDictionary(collection?: Collection): Record { + public getDictionary(collection?: Collection): Obj { const items = collection || this.items - const dictionary: Record = {} + const dictionary: Obj = {} for (const item of items) { dictionary[this.getPrimaryKey(item)] = item @@ -502,8 +501,8 @@ export default class Collection< */ public groupBy( key: keyof Item | K | ((item: Item, index: number) => Key) - ): Record { - const collection: Record = {} + ): Obj { + const collection: Obj = {} this.items.forEach((item, index) => { const resolvedKey: Key = (resolveValue(item, key, index) as Key) ?? '' @@ -642,8 +641,8 @@ export default class Collection< */ public mapToGroups( callback: (item: Item, index: number) => [Key, unknown] - ): Record { - const collection: Record = {} + ): Obj { + const collection: Obj = {} this.items.forEach((item, index) => { const [key, value] = callback(item, index) @@ -666,10 +665,8 @@ export default class Collection< * @param {Function} callback * @return {Object} */ - public mapWithKeys( - callback: (item: Item) => [Key, unknown] - ): Record { - const collection: Record = {} + public mapWithKeys(callback: (item: Item) => [Key, unknown]): Obj { + const collection: Obj = {} this.items.forEach((item) => { const [key, value] = callback(item) @@ -842,7 +839,7 @@ export default class Collection< public pluck( value: keyof Item | V, key: keyof Item | K - ): Record> + ): Obj> /** * The pluck method retrieves all of the values for a given key. @@ -854,7 +851,7 @@ export default class Collection< public pluck( value: keyof Item | V, key?: keyof Item | K - ): ValueOf[] | Record> { + ): ValueOf[] | Obj> { if ((value as string).indexOf('*') !== -1) { const [keyMatches, valueMatches]: [K[], V[]] = getMatches( this.items, @@ -867,7 +864,7 @@ export default class Collection< this.items, keyMatches, valueMatches - ) as Record>) + ) as Obj>) : ([valueMatches] as ValueOf[]) } diff --git a/src/types/index.ts b/src/types/index.ts index 59272c6..96a697c 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -19,7 +19,8 @@ export type Operator = export type Key = string export type KeyVariadic = Key | Key[] // eslint-disable-next-line @typescript-eslint/no-explicit-any -export type ItemData = Record +export type Obj = Record +export type ItemData = Obj // eslint-disable-next-line @typescript-eslint/ban-types export type ExtractFunction = Extract export type ItemOrDefault =