This repository has been archived on 2023-05-29. You can view files and clone it, but cannot push or open issues or pull requests.
fp-ts-training/README.md

115 lines
2.9 KiB
Markdown
Raw Normal View History

2020-07-06 16:49:45 +03:00
# Inato `fp-ts` training
This repo is a work in progress toward having a comprehensive training material
to onboard people on using `fp-ts` efficiently.
The exercices consist of unimplemented functions and their associated failing
tests.
To run the tests, simply run
2022-03-31 17:29:10 +03:00
2020-07-06 16:49:45 +03:00
```sh
$ yarn test
```
You can also run them in watch mode:
2022-03-31 17:29:10 +03:00
```sh
$ yarn test:watch
```
Finally, if you wish to only run the tests for a given exercice `exoN`, you can
run the following:
2022-03-31 17:29:10 +03:00
```sh
$ yarn test[:watch] exoN
```
2020-07-06 16:49:45 +03:00
The exercices are organized into `exoN` folders and most of what is required to
2022-03-31 17:29:10 +03:00
complete each is detailed in the comments.
2022-04-01 11:00:29 +03:00
## code style guide
2022-03-31 17:29:10 +03:00
2022-04-01 12:21:53 +03:00
For readability purpose, we replace `ReaderTaskEither` by `rte`
2022-03-31 17:29:10 +03:00
- Use `flow` instead of `pipe` when possible
2022-04-06 15:20:55 +03:00
> Why? Using flow reduces the amount of variables to declare in a method, hence the visibility and readability of the code
2022-03-31 17:29:10 +03:00
```typescript
// Bad
2022-04-01 12:21:53 +03:00
const formatUserPhoneNumber = (user: User) =>
pipe(user, User.phoneNumber, User.formatPhoneNumber);
2022-03-31 17:29:10 +03:00
// Good
2022-04-01 12:21:53 +03:00
const formatUserPhoneNumber = flow(User.phoneNumber, User.formatPhoneNumber);
2022-03-31 17:29:10 +03:00
```
- Avoid using `boolean` method `match` when unecessary
2022-04-01 11:00:29 +03:00
> Why? boolean.match can lower the global understanding of a method and enforce nested pipes. Using classic `if/else` is often the best option
2022-03-31 17:29:10 +03:00
```typescript
// Bad
2022-04-01 12:21:53 +03:00
const triggerEmailCampaign = ({
user,
...emailSettings
}: {
2022-04-06 15:20:55 +03:00
user: User} & EmailSettings) =>
2022-04-01 12:21:53 +03:00
pipe(
user.nationality === 'FR',
boolean.match(
() => triggerGlobalEmailCampain({ to: user.email, emailSettings }),
() => triggerFrenchEmailCampaign({ to: user.email, emailSettings }),
),
);
2022-03-31 17:29:10 +03:00
// Good
2022-04-01 12:21:53 +03:00
const triggerEmailCampaign = ({
user,
2022-04-06 15:20:55 +03:00
...emailSettings
}: { user: User } & EmailSettings) => {
2022-04-01 12:21:53 +03:00
if (user.nationality === 'FR') {
return triggerFrenchEmailCampaign({ to: user.email, emailSettings });
2022-03-31 17:29:10 +03:00
}
2022-04-01 12:21:53 +03:00
return triggerGlobalEmailCampain({ to: user.email, emailSettings });
2022-03-31 17:29:10 +03:00
```
2022-04-01 12:21:53 +03:00
- Avoid nested pipes
> Why? They lower global understanding of the code. We allow ourselves 2 levels of piping maximum per function and tend to do atomic functions instead
2022-03-31 17:29:10 +03:00
```typescript
// Bad
2022-04-06 15:20:55 +03:00
const refreshUserToken = (user: User) =>
2022-04-01 12:21:53 +03:00
pipe(
user.token,
option.match(
() => AuthClient.createToken(user),
(token) =>
2022-04-06 15:20:55 +03:00
pipe(
AuthClient.refreshToken({ user, token }),
2022-04-01 12:21:53 +03:00
rte.chainW(({ newToken }) =>
pipe(newToken.hash, user.updateToken, rte.chainEitherKW(storeUser)),
),
),
),
);
2022-03-31 17:29:10 +03:00
// Good
2022-04-06 15:20:55 +03:00
const updateUserToken = (user: User) =>
flow(user.updateToken, rte.chainEitherKW(storeUser));
2022-04-01 12:21:53 +03:00
2022-04-06 15:20:55 +03:00
const refreshAndUpdateUserToken = (user: User) => (token: Token) =>
pipe(
AuthClient.refreshToken({ user, token }),
rte.chainW(({ newToken }) => updateUserToken(user)(newToken.hash)),
2022-04-01 12:21:53 +03:00
);
2022-04-06 15:20:55 +03:00
const refreshUserToken = (user: User) =>
2022-04-01 12:21:53 +03:00
pipe(
user.token,
option.match(
() => AuthClient.createToken(user),
renewAndUpdateUserToken(user),
),
);
2022-03-31 17:29:10 +03:00
```