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

feat(partition): add support for type guards #464

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions snapshots/partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ declare const a_1_b_2_c_3: {
b: 2;
c: 3;
};
declare const number_type_guard: (x: unknown) => x is number;
declare const unknown_array: unknown[];

// @dts-jest:pass:snap -> [number[], number[]]
R.partition(number_to_boolean)(number_array);
Expand All @@ -17,3 +19,8 @@ R.partition(number_to_boolean, number_array);
R.partition(number_to_boolean)(a_1_b_2_c_3);
// @dts-jest:pass:snap -> [Partial<{ a: 1; b: 2; c: 3; }>, Partial<{ a: 1; b: 2; c: 3; }>]
R.partition(number_to_boolean, a_1_b_2_c_3);

// @dts-jest:pass:snap -> [number[], unknown[]]
R.partition(number_type_guard)(unknown_array);
// @dts-jest:pass:snap -> [number[], unknown[]]
R.partition(number_type_guard, unknown_array);
2 changes: 2 additions & 0 deletions snapshots/ramda-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2173,6 +2173,8 @@ import * as R from '../ramda/dist/index';
R.partition((x: number) => x > 2, [1, 2, 3, 4]);
// @dts-jest:pass:snap -> [number[], number[]]
R.partition((x: number) => x > 2)([1, 2, 3, 4]);
// @dts-jest:pass:snap -> [number[], unknown[]]
R.partition((x: unknown): x is number => typeof x === 'number')([1, 'a', 2, 'b', 3, 4, 'c']);
// @dts-jest:pass:snap -> [Partial<{ a: string; b: string; foo: string; }>, Partial<{ a: string; b: string; foo: string; }>]
R.partition(R.contains('s'), { a: 'sss', b: 'ttt', foo: 'bars' }); //=> [ { a: 'sss', foo: 'bars' }, { b: 'ttt' } ]
})();
Expand Down
4 changes: 4 additions & 0 deletions templates/partition.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Dictionary, List, Predicate } from './$types';

export function $list<T, P extends T>(
fn: (a: T) => a is P,
list: List<T>
): [P[], T[]];
export function $list<T>(fn: Predicate<T>, list: List<T>): [T[], T[]];
export function $object<T, U extends Dictionary<T>>(
fn: Predicate<T>,
Expand Down
3 changes: 3 additions & 0 deletions templates/partition.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ that has a filter method such as `Array`.

R.partition(R.contains('s'), { a: 'sss', b: 'ttt', foo: 'bars' });
// => [ { a: 'sss', foo: 'bars' }, { b: 'ttt' } ]

R.partition((value: unknown): value is number => typeof value === 'number', ['a', 'b', 1, 2, 'c']);
// => [ [1, 2], ['a', 'b', 'c'] ]
4 changes: 4 additions & 0 deletions tests/__snapshots__/partition.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ exports[`R.partition(number_to_boolean)(number_array) 1`] = `"[number[], number[
exports[`R.partition(number_to_boolean, a_1_b_2_c_3) 1`] = `"[Partial<{ a: 1; b: 2; c: 3; }>, Partial<{ a: 1; b: 2; c: 3; }>]"`;

exports[`R.partition(number_to_boolean, number_array) 1`] = `"[number[], number[]]"`;

exports[`R.partition(number_type_guard)(unknown_array) 1`] = `"[number[], unknown[]]"`;

exports[`R.partition(number_type_guard, unknown_array) 1`] = `"[number[], unknown[]]"`;
2 changes: 2 additions & 0 deletions tests/__snapshots__/ramda-tests.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,8 @@ exports[`partition R.partition((x: number) => x > 2)([1, 2, 3, 4]) 1`] = `"[numb

exports[`partition R.partition((x: number) => x > 2, [1, 2, 3, 4]) 1`] = `"[number[], number[]]"`;

exports[`partition R.partition((x: unknown): x is number => typeof x === 'number')([1, 'a', 2, 'b', 3, 4, 'c']) 1`] = `"[number[], unknown[]]"`;

exports[`partition R.partition(R.contains('s'))(['sss', 'ttt', 'foo', 'bars']) 1`] = `"[R.List<string>[], R.List<string>[]]"`;

exports[`partition R.partition(R.contains('s'), ['sss', 'ttt', 'foo', 'bars']) 1`] = `"[R.List<string>[], R.List<string>[]]"`;
Expand Down
7 changes: 7 additions & 0 deletions tests/partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ declare const a_1_b_2_c_3: {
b: 2;
c: 3;
};
declare const number_type_guard: (x: unknown) => x is number;
declare const unknown_array: unknown[];

// @dts-jest:pass:snap
R.partition(number_to_boolean)(number_array);
Expand All @@ -17,3 +19,8 @@ R.partition(number_to_boolean, number_array);
R.partition(number_to_boolean)(a_1_b_2_c_3);
// @dts-jest:pass:snap
R.partition(number_to_boolean, a_1_b_2_c_3);

// @dts-jest:pass:snap
R.partition(number_type_guard)(unknown_array);
// @dts-jest:pass:snap
R.partition(number_type_guard, unknown_array);
2 changes: 2 additions & 0 deletions tests/ramda-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2174,6 +2174,8 @@ import * as R from '../ramda/dist/index';
// @dts-jest:pass:snap
R.partition((x: number) => x > 2)([1, 2, 3, 4]);
// @dts-jest:pass:snap
R.partition((x: unknown): x is number => typeof x === 'number')([1, 'a', 2, 'b', 3, 4, 'c']);
// @dts-jest:pass:snap
R.partition(R.contains('s'), { a: 'sss', b: 'ttt', foo: 'bars' }); //=> [ { a: 'sss', foo: 'bars' }, { b: 'ttt' } ]
})();

Expand Down