Skip to content

Commit

Permalink
🤖 Merge PR #57393 [ramda] optimize compose, pipe, chain and othter fu…
Browse files Browse the repository at this point in the history
…nctions. by @adispring

* [ramda] optimize compose, pipe, chain and othter functions.

* feat: optimize always and cond

* feat: optimise composeWith, support multiple parameters

* feat: optimize composeWith and pipeWith
  • Loading branch information
adispring committed Nov 28, 2021
1 parent 7993394 commit 286eff4
Show file tree
Hide file tree
Showing 16 changed files with 247 additions and 233 deletions.
151 changes: 47 additions & 104 deletions types/ramda/index.d.ts

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions types/ramda/test/always-tests.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import * as R from 'ramda';

() => {
// $ExpectType () => string
const t = R.always('Tee');
// $ExpectType (...args: unknown[]) => string
const alwaysTea = R.always('Tea');
// $ExpectType string
const x: string = t(); // => 'Tee'
alwaysTea(); // => 'Tea'

// $ExpectType string
alwaysTea('Coffee'); // => 'Tea'

// $ExpectType string
alwaysTea(1, 2, 3); // => 'Tea'
};
11 changes: 11 additions & 0 deletions types/ramda/test/call-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as R from 'ramda';

() => {
const add = (a: number, b: number): number => a + b;

// $ExpectType number
const callAdd = R.call(add, 1, 2);

// $ExpectError
R.call(add, 'a', 2);
};
43 changes: 38 additions & 5 deletions types/ramda/test/chain-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,49 @@ import * as R from 'ramda';
return [n, n];
}

R.chain(duplicate, [1, 2, 3]); // => [1, 1, 2, 2, 3, 3]
// $ExpectType (list: readonly number[]) => number[]
const chainDuplicate = R.chain(duplicate);

// $ExpectType number[]
chainDuplicate([1, 2, 3]); // => [1, 1, 2, 2, 3, 3]
// $ExpectType number[]
R.chain(duplicate)([1, 2, 3]); // => [1, 1, 2, 2, 3, 3]
const result1: number[] = R.chain<number, number[], number[]>(
R.append,
R.head,
)([1, 2, 3]); // => [1, 2, 3, 1]

// $ExpectType number[]
R.chain(duplicateConst, [1, 2, 3] as const); // => [1, 1, 2, 2, 3, 3]
// $ExpectType number[]
R.chain(duplicateConst)([1, 2, 3] as const); // => [1, 1, 2, 2, 3, 3]

// $ExpectType number[]
R.chain(duplicateReadonly, [1, 2, 3]); // => [1, 1, 2, 2, 3, 3]
// $ExpectType number[]
R.chain(duplicateReadonly)([1, 2, 3]); // => [1, 1, 2, 2, 3, 3]

interface Score {
maths: number;
physics: number;
chemistry: number;
total?: number;
}

const score = {
maths: 90,
physics: 80,
chemistry: 70,
};

const calculateTotal = (score: Score): number => {
const { maths, physics, chemistry } = score;
return maths + physics + chemistry;
};

const assocTotalToScore = (total: number, score: Score): Score => ({ ...score, total });

// $ExpectType (r: Score) => Score
const calculateAndAssocTotalToScore = R.chain<number, Score, Score>(
assocTotalToScore,
calculateTotal
);
// $ExpectType Score
const scoreWithTotal = calculateAndAssocTotalToScore(score); // => { maths: 90, physics: 80, chemistry: 70, total: 240 }
};
30 changes: 28 additions & 2 deletions types/ramda/test/compose-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,16 @@ import * as R from 'ramda';
};

() => {
const fullName = R.compose(
interface Person {
last: string;
age: number;
first: string;
}

// $ExpectType (args_0: Person) => string
const fullName = R.compose<[Person], string[], string>(
R.join(' '),
R.props(['first', 'last']),
({ first, last }) => [first, last]
);
fullName({ last: 'Bullet-Tooth', age: 33, first: 'Tony' }); // => 'Tony Bullet-Tooth'
};
Expand Down Expand Up @@ -106,3 +113,22 @@ import * as R from 'ramda';
);
const x: number = gn('Hello', 4, 'world');
};

() => {
// Expected at least 1 arguments, but got 0
// $ExpectError
R.compose();

// $ExpectType (x: number, y: number) => number
const f9 = R.compose(
R.inc,
R.inc,
R.inc,
R.inc,
R.inc,
R.inc,
R.inc,
R.negate,
Math.pow,
);
};
19 changes: 15 additions & 4 deletions types/ramda/test/composeWith-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import * as R from 'ramda';
}
};

const getStateCode: (input: any) => any[] = R.composeWith(R.chain, [
// $ExpectType (obj: any) => string[]
const getStateCode = R.composeWith(R.chain, [
R.compose(
val => [val],
R.toUpper,
Expand All @@ -27,13 +28,16 @@ import * as R from 'ramda';
const toString = (input: any): string[] => [`${input}`];
const split = (input: string): string[] => input.split('');

const composed: (num: number) => string[] = R.composeWith(R.chain, [
// $ExpectType (num: number) => string[]
R.composeWith(R.chain, [
split,
toString,
onlyOverNine,
nextThree,
]);
const composed2: (num: number) => string[] = R.composeWith(R.chain)([

// $ExpectType (num: number) => string[]
R.composeWith(R.chain)([
split,
toString,
onlyOverNine,
Expand All @@ -44,5 +48,12 @@ import * as R from 'ramda';
() => {
const composeWhileNotNil = R.composeWith((f, res) => R.isNil(res) ? res : f(res));

composeWhileNotNil([R.inc, R.prop('age')])({age: 1}); // => 2
// $ExpectType (args_0: { age: number; }) => number
const incAgeIfNotNil = composeWhileNotNil([({ age }: { age: number }) => age]);

incAgeIfNotNil({age: 1}); // => 2

// Should pipe at least on function.
// $ExpectError
composeWhileNotNil([]);
};
21 changes: 16 additions & 5 deletions types/ramda/test/cond-tests.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import * as R from 'ramda';

() => {
const f = R.cond<number, string>([
const f = R.cond<[number], string>([
[x => x === 0, () => 'a'],
[() => true, () => 'b'],
]);
f(0); // $ExpectType string
f(''); // $ExpectError
f(1, 2); // $ExpectType string

const g = R.cond([[(a, b) => a === b, () => 'a'], [() => true, () => 'b']]);
// $ExpectType string
f(0);

// $ExpectError
f('');

// $ExpectError
f(1, 2);

const g = R.cond([[(a: any, b: any): boolean => a === b, () => 'a'], [() => true, () => 'b']]);
// $ExpectError
g(0);

// $ExpectError
g('');

// $ExpectType string
g(1, '');
};
19 changes: 19 additions & 0 deletions types/ramda/test/pipe-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,22 @@ function shout(x: number): string {
R.andThen(loadAlternative),
);
};

() => {
// Expected at least 1 arguments, but got 0
// $ExpectError
R.pipe();

// $ExpectType (x: number, y: number) => number
const f9 = R.pipe(
Math.pow,
R.negate,
R.inc,
R.inc,
R.inc,
R.inc,
R.inc,
R.inc,
R.inc,
);
};
17 changes: 14 additions & 3 deletions types/ramda/test/pipeWith-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as R from 'ramda';
}

const db = {
getUserById(userName: string): Promise<User> {
getUserById(userId: string): Promise<User> {
return Promise.resolve({
name: 'Jon',
followers: ['Samwell', 'Edd', 'Grenn'],
Expand All @@ -18,8 +18,19 @@ import * as R from 'ramda';
},
};

const followersForUser: (userName: string) => Promise<string[]> = R.pipeWith(
(f: (value: any) => any, res: any) => res.then(f),
// $ExpectType (userId: string) => Promise<string[]>
const followersForUser = R.pipeWith(
(f: (value: any) => any, res: Promise<unknown>) => res.then(f),
[db.getUserById, db.getFollowers],
);

// $ExpectType (userId: string) => Promise<User>
const getUser = R.pipeWith(
(f: (value: any) => any, res: Promise<unknown>) => res.then(f),
[db.getUserById],
);

// Should pipe at least on function.
// $ExpectError
R.pipeWith((f: (value: any) => any, res: Promise<unknown>) => res.then(f), []);
};
6 changes: 3 additions & 3 deletions types/ramda/test/slice-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ import * as R from 'ramda';
const str = 'Hello World';
const arr = ['one', 'two', 'three', 'four', 'five'];

// $ExpectType (list: string) => string
const pipeSlice = R.pipe(R.slice(2, 5));
// $ExpectType string
R.pipe(
R.slice(2, 5)
)(str); // => 'llo'
pipeSlice(str); // => 'llo'

// $ExpectType string[]
R.pipe(
Expand Down
22 changes: 10 additions & 12 deletions types/ramda/test/sortBy-tests.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as R from 'ramda';

interface Person {
name: string;
age: number;
}

() => {
const sortByAgeDescending = R.sortBy(
R.compose<{}, number, number>(
R.negate,
R.prop('age'),
),
);
// $ExpectType (list: readonly Person[]) => Person[]
const sortByAgeDescending = R.sortBy<Person>(({ age }) => -age);

const alice = {
name: 'ALICE',
age: 101,
Expand All @@ -24,12 +26,8 @@ import * as R from 'ramda';
};

() => {
const sortByNameCaseInsensitive = R.sortBy(
R.compose<Record<'name', string>, string, string>(
R.toLower,
R.prop('name'),
),
);
// $ExpectType (list: readonly Person[]) => Person[]
const sortByNameCaseInsensitive = R.sortBy<Person>(({ name }) => name.toLowerCase());
const alice = {
name: 'ALICE',
age: 101,
Expand Down
3 changes: 2 additions & 1 deletion types/ramda/test/transduce-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import * as R from 'ramda';

() => {
const numbers = [1, 2, 3, 4];
const transducer = R.compose(
// $ExpectType (args_0: number[]) => number[]
const transducer = R.compose<[number[]], number[], number[]>(
R.map(R.add(1)),
R.take(2),
);
Expand Down
7 changes: 5 additions & 2 deletions types/ramda/test/unless-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import * as R from 'ramda';
const a: number[] = coerceArray([1, 2, 3]); // => [1, 2, 3]
const b: number[] = coerceArray(1); // => [1]

// bodyTemperature :: (number) -> number | string
// $ExpectType (a: number) => string | number
const bodyTemperature = R.unless<number, string>(
R.chain(R.equals, R.clamp(36.5, 37.5)),
temperatureC => R.clamp(36.5, 37.5, temperatureC) === temperatureC,
t => `abnormal: ${t}`
);

// $ExpectType string | number
const normal = bodyTemperature(37); // => 37

// $ExpectType string | number
const abnormal = bodyTemperature(38); // => 'abnormal: 38'
};
27 changes: 15 additions & 12 deletions types/ramda/test/when-tests.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import * as R from 'ramda';

() => {
// $ExpectType (a: string) => string
const truncate = R.when(
R.propSatisfies(R.flip(R.gt)(10), 'length'),
R.pipe<string, string, string[], string>(
R.take(10),
R.append('…') as (wrong: any) => string[],
R.join(''),
),
(str: string) => str.length > 10,
(str: string) => str.slice(0, 10) + '…',
);
const a: string = truncate('12345'); // => '12345'
const b: string = truncate('0123456789ABC'); // => '0123456789…'

const addOneIfNotNil = R.when(
R.complement(R.isNil),
// $ExpectType string
truncate('12345'); // => '12345'
// $ExpectType string
truncate('0123456789ABC'); // => '0123456789…'

// $ExpectType (a: number | undefined) => number | undefined
const addOneIfNotNil = R.when<undefined | number, number>(
x => x != null,
R.add(1)
);

const nil: undefined = addOneIfNotNil(undefined);
const two: number = addOneIfNotNil(1);
// $ExpectType number | undefined
const nil = addOneIfNotNil(undefined);
// $ExpectType number | undefined
const two = addOneIfNotNil(1);
};
Loading

0 comments on commit 286eff4

Please sign in to comment.