Skip to content
This repository has been archived by the owner on Jul 25, 2018. It is now read-only.

Commit

Permalink
Update R.filter
Browse files Browse the repository at this point in the history
support dictionary
  • Loading branch information
ikatyang committed Jun 22, 2017
1 parent 816d0cb commit 91c817a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ source version: v0.24.1
- [x] equals
- [x] evolve
- [ ] F
- [ ] filter
- [x] filter
- [ ] find
- [ ] findIndex
- [ ] findLast
Expand Down
18 changes: 9 additions & 9 deletions src/filter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ declare const filter: filter_00;
type filter_00 = {
<T>(_fn: PH, list: List<T>): filter_list_01<T>;
<T, U extends Filterable<T>>(_fn: PH, filterable: U): filter_filterable_01<T, U>;
<T, U extends Dictionary<T>>(_fn: PH, object: U): filter_object_01<T, U>;
<T>(_fn: PH, object: Dictionary<T>): filter_dictionary_01<T>;
<T>(fn: Predicate<T>, list: List<T>): filter_list_11<T>;
<T, U extends Filterable<T>>(fn: Predicate<T>, filterable: U): filter_filterable_11<T, U>;
<T, U extends Dictionary<T>>(fn: Predicate<T>, object: U): filter_object_11<T, U>;
<T>(fn: Predicate<T>, object: Dictionary<T>): filter_dictionary_11<T>;
<$SEL extends "11", $KIND extends "list">(): <T>(fn: Predicate<T>, list: List<T>) => filter_list_11<T>;
<$SEL extends "01", $KIND extends "list">(): <T>(_fn: PH, list: List<T>) => filter_list_01<T>;
<$SEL extends "11", $KIND extends "filterable">(): <T, U extends Filterable<T>>(fn: Predicate<T>, filterable: U) => filter_filterable_11<T, U>;
<$SEL extends "01", $KIND extends "filterable">(): <T, U extends Filterable<T>>(_fn: PH, filterable: U) => filter_filterable_01<T, U>;
<$SEL extends "11", $KIND extends "object">(): <T, U extends Dictionary<T>>(fn: Predicate<T>, object: U) => filter_object_11<T, U>;
<$SEL extends "01", $KIND extends "object">(): <T, U extends Dictionary<T>>(_fn: PH, object: U) => filter_object_01<T, U>;
<$SEL extends "11", $KIND extends "dictionary">(): <T>(fn: Predicate<T>, object: Dictionary<T>) => filter_dictionary_11<T>;
<$SEL extends "01", $KIND extends "dictionary">(): <T>(_fn: PH, object: Dictionary<T>) => filter_dictionary_01<T>;
<$SEL extends "1">(): <T>(fn: Predicate<T>) => filter_10<T>;
<T>(fn: Predicate<T>): filter_10<T>;
};
Expand All @@ -49,19 +49,19 @@ type filter_10<T> = {
<U extends Filterable<T>>(filterable: U): filter_filterable_11<T, U>;
<$SEL extends "1", $KIND extends "list">(): (list: List<T>) => filter_list_11<T>;
<$SEL extends "1", $KIND extends "filterable">(): <U extends Filterable<T>>(filterable: U) => filter_filterable_11<T, U>;
<$SEL extends "1", $KIND extends "object">(): <U extends Dictionary<T>>(object: U) => filter_object_11<T, U>;
<U extends Dictionary<T>>(object: U): filter_object_11<T, U>;
<$SEL extends "1", $KIND extends "dictionary">(): (object: Dictionary<T>) => filter_dictionary_11<T>;
(object: Dictionary<T>): filter_dictionary_11<T>;
};
type filter_list_01<T> = {
(fn: Predicate<T>): filter_list_11<T>;
};
type filter_filterable_01<T, U extends Filterable<T>> = {
(fn: Predicate<T>): filter_filterable_11<T, U>;
};
type filter_object_01<T, U extends Dictionary<T>> = {
(fn: Predicate<T>): filter_object_11<T, U>;
type filter_dictionary_01<T> = {
(fn: Predicate<T>): filter_dictionary_11<T>;
};
type filter_list_11<T> = T[];
type filter_filterable_11<T, U extends Filterable<T>> = U;
type filter_object_11<T, U extends Dictionary<T>> = Partial<U>;
type filter_dictionary_11<T> = Dictionary<T>;
export = filter;
2 changes: 1 addition & 1 deletion templates/filter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import {Dictionary, Filterable, List, Predicate} from './$types';

export function $list<T>(fn: Predicate<T>, list: List<T>): T[];
export function $filterable<T, U extends Filterable<T>>(fn: Predicate<T>, filterable: U): U;
export function $object<T, U extends Dictionary<T>>(fn: Predicate<T>, object: U): Partial<U>;
export function $dictionary<T>(fn: Predicate<T>, object: Dictionary<T>): Dictionary<T>;
4 changes: 2 additions & 2 deletions tests/__snapshots__/filter.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

exports[`R_filter(string_predicate)(string_array) 1`] = `"string[]"`;

exports[`R_filter(string_predicate)(string_dictionary) 1`] = `"Partial<Dictionary<string>>"`;
exports[`R_filter(string_predicate)(string_dictionary) 1`] = `"Dictionary<string>"`;
exports[`R_filter(string_predicate)(string_filterable) 1`] = `"Filterable<string>"`;
exports[`R_filter(string_predicate, string_array) 1`] = `"string[]"`;
exports[`R_filter(string_predicate, string_dictionary) 1`] = `"Partial<Dictionary<string>>"`;
exports[`R_filter(string_predicate, string_dictionary) 1`] = `"Dictionary<string>"`;
exports[`R_filter(string_predicate, string_filterable) 1`] = `"Filterable<string>"`;
6 changes: 5 additions & 1 deletion tests/__snapshots__/ramda-tests.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,13 @@ exports[`evolve R.evolve(transformations)(tomato) 1`] = `"{ firstName: string; d
exports[`evolve R.evolve(transformations, tomato) 1`] = `"{ firstName: string; data: { elapsed: number; remaining: number; }; id: number; }"`;
exports[`filter R.filter(isEven)([1, 2, 3, 4]) 1`] = `"number[]"`;
exports[`filter R.filter(isEven)({a: 1, b: 2, c: 3, d: 4}) 1`] = `"Dictionary<number>"`;
exports[`filter R.filter(isEven, [1, 2, 3, 4]) 1`] = `"number[]"`;
exports[`filter onlyNumberList(R.filter(isEven, [1, 2, 3, 4])) 1`] = `"number[]"`;
exports[`filter R.filter(isEven, {a: 1, b: 2, c: 3, d: 4}) 1`] = `"Dictionary<number>"`;
exports[`find f(tasks) 1`] = `"Task"`;
Expand Down
34 changes: 14 additions & 20 deletions tests/ramda-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,20 @@ import * as R from 'ramda';
R.evolve(transformations)(tomato); // => {firstName: 'Tomato', data: {elapsed: 101, remaining: 1399}, id: 123}
}

// @dts-jest:group filter
{
const isEven = (n: number) => n % 2 === 0;

// @dts-jest:pass
R.filter(isEven, [1, 2, 3, 4]); // => [2, 4]
// @dts-jest:pass
R.filter(isEven)([1, 2, 3, 4]);
// @dts-jest:pass
R.filter(isEven, {a: 1, b: 2, c: 3, d: 4}); // => {b: 2, d: 4}
// @dts-jest:pass
R.filter(isEven)({a: 1, b: 2, c: 3, d: 4});
}

// ---------------------------------------------------------------------

const double = (x: number): number => x + x;
Expand Down Expand Up @@ -1217,26 +1231,6 @@ class F {
* List category
********************/

// @dts-jest:group:skip filter
{
const isEven = (n: number) =>
n % 2 === 0;
// filter works with lists...
// @dts-jest:pass
R.filter(isEven, [1, 2, 3, 4]); // => [2, 4]
const isEvenFn = R.filter(isEven);
isEvenFn([1, 2, 3, 4]);
// ... but also objects
// @dts-jest:show Dictionary<number>
R.filter(isEven, {a: 1, b: 2, c: 3, d: 4}); // => {b: 2, d: 4}
// see that we did not break anything
// and we kept type information
// @dts-jest:pass
onlyNumberList(R.filter(isEven, [1, 2, 3, 4]));
// @dts-jest:show Dictionary<number>
onlyNumberObj(R.filter(isEven, {a: 1, b: 2, c: 3, d: 4})); // strictNullChecks: Partial fails, consider Pick
}

// @dts-jest:group:skip find, propEq
{
const xs = [{a: 1}, {a: 2}, {a: 3}];
Expand Down

0 comments on commit 91c817a

Please sign in to comment.