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.
Go to file
Scott Picquerey cb043aec01 Update prettier rules 2022-04-06 15:26:46 +02:00
.github Add dependabot configuration 2022-03-17 16:41:32 +01:00
.vscode 🎉 Initial commit 2020-07-03 18:01:45 +02:00
src Add exercice on sequence 2022-03-07 15:00:59 +01:00
.eslintrc.js 🎉 Initial commit 2020-07-03 18:01:45 +02:00
.gitignore 🎉 Initial commit 2020-07-03 18:01:45 +02:00
.nvmrc ⬆️ Upgrade all dependencies 2022-01-11 14:47:57 +01:00
.prettierrc Update prettier rules 2022-04-06 15:26:46 +02:00
README.md Update example for nested pipes 2022-04-06 15:26:42 +02:00
jest.config.js Stop failing test because of typescript errors 2022-03-04 11:40:00 +01:00
package.json Bump @types/node from 17.0.21 to 17.0.22 2022-03-22 05:29:10 +00:00
tsconfig.json 🎉 Initial commit 2020-07-03 18:01:45 +02:00
yarn.lock Bump @types/node from 17.0.21 to 17.0.22 2022-03-22 05:29:10 +00:00

README.md

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

$ yarn test

You can also run them in watch mode:

$ yarn test:watch

Finally, if you wish to only run the tests for a given exercice exoN, you can run the following:

$ yarn test[:watch] exoN

The exercices are organized into exoN folders and most of what is required to complete each is detailed in the comments.

code style guide

For readability purpose, we replace ReaderTaskEither by rte

  • Use flow instead of pipe when possible

    Why? Using flow reduces the amount of variables to declare in a method, hence the visibility and readability of the code

// Bad
const formatUserPhoneNumber = (user: User) =>
  pipe(user, User.phoneNumber, User.formatPhoneNumber);

// Good
const formatUserPhoneNumber = flow(User.phoneNumber, User.formatPhoneNumber);
  • Avoid using boolean method match when unecessary

    Why? boolean.match can lower the global understanding of a method and enforce nested pipes. Using classic if/else is often the best option

// Bad
const triggerEmailCampaign = ({
  user,
  ...emailSettings
}: {
  user: User} & EmailSettings) =>
  pipe(
    user.nationality === 'FR',
    boolean.match(
      () => triggerGlobalEmailCampain({ to: user.email, emailSettings }),
      () => triggerFrenchEmailCampaign({ to: user.email, emailSettings }),
    ),
  );

// Good
const triggerEmailCampaign = ({
  user,
  ...emailSettings
}: { user: User } & EmailSettings) => {
  if (user.nationality === 'FR') {
    return triggerFrenchEmailCampaign({ to: user.email, emailSettings });
  }
  return triggerGlobalEmailCampain({ to: user.email, emailSettings });
  • 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

// Bad
const convertDollarAmountInCountryCurrency = ({
  countryName,
  amountInDollar,
}: {
  countryName: CountryName;
  amountInDollar: number;
}) =>
  pipe(
    getCountryCode(countryName),
    either.map(
      countryCode =>
        pipe(
          getCountryCurrency(countryCode),
          option.map(currency =>
            pipe(
              amountInDollar,
              converFromDollar(currency),
              convertedAmount =>
                console.log(
                  `converted amount for country ${countryCode} is ${convertedAmount}`,
                ),
              ),
            ),
          ),
        ),
    ),
  );

// Good
const convertDollarAmountInCountryCodeCurrency = ({
  amountInDollar,
  countryCode,
}: {
  amountInDollar: number;
  countryCode: CountryCode;
}) =>
  pipe(
    getCurrencyFromCountryCode(countryCode),
    option.map(currency => {
      const convertedDollarAmount = convertFromDollar(currency)(currency);
      console.log(
        `converted amount for country ${countryCode} is ${convertedAmount}`,
      );
    }),
  );

const convertDollarAmountInCountryCurrency = ({
  amountInDollar,
  countryName,
}: {
  amountInDollar: number;
  countryName: CountryName;
}) =>
  pipe(
    getCountryCode(countryName),
    either.map(countryCode =>
      convertDollarAmountToCountryCodeCurrency({ amountInDollar, countryCode }),
    ),
  );