Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix types #25

Merged
merged 6 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/array.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Flatten array, one level deep.
*
* @param arr
*
* @return
*/
export function flatten<T extends any[]>(arr: T[]): T;
6 changes: 4 additions & 2 deletions lib/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
/**
* Flatten array, one level deep.
*
* @param {Array<?>} arr
* @template T
*
* @return {Array<?>}
* @param {T[][]} arr
*
* @return {T[]}
*/
export function flatten(arr) {
return Array.prototype.concat.apply([], arr);
Expand Down
174 changes: 174 additions & 0 deletions lib/collection.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@

export type Matcher<T> =
((e: T) => boolean) |
((e: T, idx: number) => boolean) |
((e: T, key: string) => boolean) |
any;

export type Extractor<T, U=T[keyof T]> = ((e: T) => U) | string | number;

export type ArrayCollection<T> = Array<T>;
export type StringKeyValueCollection<T> = { [key: string]: T };
export type NumberKeyValueCollection<T> = { [key: number]: T };
export type KeyValueCollection<T> = StringKeyValueCollection<T> | NumberKeyValueCollection<T>;
export type Collection<T> = KeyValueCollection<T> | ArrayCollection<T>;

/**
* Find element in collection.
*
* @param collection
* @param matcher
*
* @return
*/
export function find<T>(collection: Collection<T>, matcher: Matcher<T>): T | undefined;

/**
* Find element index in collection.
*
* @param collection
* @param matcher
*
* @return
*/
export function findIndex<T>(collection: Collection<T>, matcher: Matcher<T>): number | undefined;

/**
* Find element in collection.
*
* @param collection
* @param matcher
*
* @return result
*/
export function filter<T>(collection: Collection<T>, matcher: Matcher<T>): T[];

/**
* Iterate over collection; returning something
* (non-undefined) will stop iteration.
*
* @param collection
* @param iterator
*
* @return return result that stopped the iteration
*/
export function forEach<T>(collection: Collection<T>, iterator: (item: T, convertKey: any /* TODO */) => boolean | void): T;

/**
* Return collection without element.
*
* @param arr
* @param matcher
*
* @return
*/
export function without<T>(arr: T[], matcher: Matcher<T>): T[];

/**
* Reduce collection, returning a single result.
*
* @param collection
* @param iterator
* @param result
*
* @return result returned from last iterator
*/
export function reduce<T, V>(collection: Collection<T>, iterator: (result: V, entry: T, index: any) => V, result: V): V;

/**
* Return true if every element in the collection
* matches the criteria.
*
* @param collection
* @param matcher
*
* @return
*/
export function every<T>(collection: Collection<T>, matcher: Matcher<T>): boolean;

/**
* Return true if some elements in the collection
* match the criteria.
*
* @param collection
* @param matcher
*
* @return
*/
export function some<T>(collection: Collection<T>, matcher: Matcher<T>): boolean;

/**
* Transform a collection into another collection
* by piping each member through the given fn.
*
* @param collection
* @param fn
*
* @return transformed collection
*/
export function map<T, U>(collection: Collection<T>, fn: (value: T, key: number) => U): U[];

/**
* Get the collections keys.
*
* @param collection
*
* @return
*/
export function keys<T>(collection: Collection<T>): T extends Array<any> ? number[] : (keyof T)[];

/**
* Shorthand for `keys(o).length`.
*
* @param collection
*
* @return
*/
export function size<T>(collection: Collection<T>): number;

/**
* Get the values in the collection.
*
* @param collection
*
* @return
*/
export function values<T>(collection: Collection<T>): T[];

/**
* Group collection members by attribute.
*
* @param collection
* @param extractor
*
* @return map with { attrValue => [ a, b, c ] }
*/
export function groupBy<T>(collection: Collection<T>, extractor: Extractor<T>, grouped?: any): { [attrValue: string]: any[] };

export function uniqueBy<T>(extractor: Extractor<T>, ...collections: Collection<T>[]): T[];
export function unionBy<T>(extractor: Extractor<T>, ...collections: Collection<T>[]): T[];

/**
* Sort collection by criteria.
*
* @param collection
* @param extractor
*
* @return
*/
export function sortBy<T>(collection: Collection<T>, extractor: Extractor<T, number | string>): T[];

/**
* Create an object pattern matcher.
*
* @example
*
* const matcher = matchPattern({ id: 1 });
*
* let element = find(elements, matcher);
*
* @param pattern
*
* @return matcherFn
*/
export function matchPattern<T>(pattern: T): (e: any) => boolean;
Loading