Skip to content

Commit

Permalink
fix(utils): fixes collect implementation and types
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Mar 5, 2020
1 parent 36e406b commit 7c7fd44
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
9 changes: 4 additions & 5 deletions src/collection/PureCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import {
Coerce,
TakeStrategy,
Take,
take
take,
CollectCollector,
Collect
} from '../utils';
import { PureCollectionInitial } from './types';
import { CollectCollector, Collect } from '~/utils/collect/types';

const internal = Symbol('PureCollection');

Expand Down Expand Up @@ -152,9 +153,7 @@ export class PureCollection<T extends Record<string, any>> {
public select(property: keyof T, a: any, b?: any, c?: any): any {
return select(this.get(property) as any, a, b, c);
}
public collect<O extends Record<string, any>>(
collector: CollectCollector<FilterRecordByType<T, Type>, O>
): Collect<O> {
public collect<O>(collector: CollectCollector<O>): Collect<O> {
return collect(
Object.entries(this.all()).reduce((acc, [key, value]) => {
return isType(value) ? Object.assign(acc, { [key]: value }) : acc;
Expand Down
15 changes: 8 additions & 7 deletions src/utils/collect/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ const functions: CollectorFunctions = {
}
};

export function collect<
I extends Record<string, Type>,
O extends Record<string, any>
>(data: I, collector: CollectCollector<I, O>): Collect<O> {
const callbacks = collector(functions);
export function collect<I extends Record<string, Type>, O>(
data: I,
collector: CollectCollector<O>
): Collect<O> {
const response = collector(functions);
const results: Partial<Collect<O>> = {};
for (const key of Object.keys(callbacks) as Array<keyof O & keyof I>) {
results[key] = callbacks[key](data[key]);
for (const key of Object.keys(response) as Array<keyof O & keyof I>) {
const value = response[key];
results[key] = typeof value === 'function' ? value(data[key]) : value;
}
return results as Collect<O>;
}
4 changes: 1 addition & 3 deletions src/utils/collect/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ export type Collect<O extends Record<string, any>> = {
};

/* Input */
export type CollectCollector<I extends Record<string, Type>, O = any> = (
fn: CollectorFunctions
) => O & { [P in keyof I]?: Type | CollectCollectorCallback<Type, any> };
export type CollectCollector<O> = (fn: CollectorFunctions) => O;

export type CollectCollectorCallback<T extends Type, U> = (data: T) => U;

Expand Down
2 changes: 2 additions & 0 deletions test/utils/collect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test(`calls utils and returns values`, () => {
f: 'barbaz'
},
({ get, assert, take, ensure, coerce, select }) => ({
item: 'value',
a: get(),
b: assert(),
c: take('c0arg' as any, 'c1arg' as any),
Expand All @@ -35,6 +36,7 @@ test(`calls utils and returns values`, () => {
);

expect(result).toEqual({
item: 'value',
a: 'foo',
b: { assert: 'bar' },
c: { take: 'baz' },
Expand Down

0 comments on commit 7c7fd44

Please sign in to comment.