Skip to content

Commit

Permalink
fix(pluck): pluck now can return object
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo108 committed Oct 13, 2023
1 parent 1a36036 commit 96b5420
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
31 changes: 22 additions & 9 deletions test/pluck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { __, pluck } from '../es';

type Obj = { name: string; age: number };

const record = {
a: {name: 'foo', age: 13},
b: {name: 'bar', age: 31, desc: 'barrr'}
};
const incorrectRecord = {
a: {name: 'foo', age: 13},
b: {name: 'bar', age: 31, desc: 'barrr'},
c: {}
};

// pluck(key, list)
expectType<string[]>(pluck('name', [] as Obj[]));
expectType<number[]>(pluck('age', [] as Obj[]));
Expand All @@ -20,16 +30,19 @@ expectType<number[]>(pluck(__, [] as Obj[])('age'));
expectError(pluck(__, [] as Obj[])('nope'));

// pluck(key, record)
expectType<string[]>(pluck('name', {} as Record<string, Obj>));
expectType<number[]>(pluck('age', {} as Record<string, Obj>));
expectError(pluck('nope', {} as Record<string, Obj>));
expectType<{ a: string, b: string }>(pluck('name', record));
expectType<{ a: number, b: number }>(pluck('age', record));
expectError(pluck('nope', record));
expectError(pluck('age', incorrectRecord));

// pluck(key)(record)
expectType<string[]>(pluck('name')({} as Record<string, Obj>));
expectType<number[]>(pluck('age')({} as Record<string, Obj>));
expectError(pluck('nope')({} as Record<string, Obj>));
expectType<{ a: string, b: string }>(pluck('name')(record));
expectType<{ a: number, b: number }>(pluck('age')(record));
expectError(pluck('nope')(record));
expectError(pluck('age')(incorrectRecord));

// pluck(__, record)(prop)
expectType<string[]>(pluck(__, {} as Record<string, Obj>)('name'));
expectType<number[]>(pluck(__, {} as Record<string, Obj>)('age'));
expectError(pluck(__, {} as Record<string, Obj>)('nope'));
expectType<{ a: string, b: string }>(pluck(__, record)('name'));
expectType<{ a: number, b: number }>(pluck(__, record)('age'));
expectError(pluck(__, record)('nope'));
expectError(pluck(__, incorrectRecord)('age'));
8 changes: 4 additions & 4 deletions types/pluck.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Placeholder } from 'ramda';

export function pluck<K extends PropertyKey>(prop: K extends Placeholder ? never : K): {
<U extends readonly unknown[] | Record<K, any>>(obj: Record<PropertyKey, U>): U extends readonly (infer T)[] ? T[] : U extends Record<K, infer T> ? T[] : never;
<U extends Record<K, any>, IK extends string>(obj: Record<IK, U>): { [KK in keyof typeof obj]: U[K] };
<U extends readonly unknown[] | Record<K, any>>(list: U[]): U extends readonly (infer T)[] ? T[] : U extends Record<K, infer T> ? T[] : never;
};
export function pluck<U>(__: Placeholder, obj: Record<PropertyKey, U>): <K extends keyof U>(prop: K) => Array<U[K]>;
export function pluck<U>(__: Placeholder, list: readonly U[]): <K extends keyof U>(prop: K) => Array<U[K]>;
export function pluck<K extends keyof U, U>(prop: K, obj: Record<PropertyKey, U>): Array<U[K]>;
export function pluck<K extends keyof U, U extends Record<any, any>, IK extends keyof any>(prop: K, record: Record<IK, U>): { [KK in keyof typeof record]: U[K] };
export function pluck<K extends keyof U, U>(prop: K, list: readonly U[]): Array<U[K]>;
export function pluck<U extends Record<any, any>, IK extends keyof any>(__: Placeholder, record: Record<IK, U>): <K extends keyof U>(prop: K) => { [KK in keyof typeof record]: U[K] };
export function pluck<U>(__: Placeholder, list: readonly U[]): <K extends keyof U>(prop: K) => Array<U[K]>;

0 comments on commit 96b5420

Please sign in to comment.