From e056cdc02c75040d815cf858a33542ec7d42abb4 Mon Sep 17 00:00:00 2001 From: Scott Picquerey Date: Wed, 6 Apr 2022 15:26:42 +0200 Subject: [PATCH] Update example for nested pipes --- README.md | 65 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 6afa661..e37895b 100644 --- a/README.md +++ b/README.md @@ -78,37 +78,64 @@ const triggerEmailCampaign = ({ ```typescript // Bad -const refreshUserToken = (user: User) => +const convertDollarAmountInCountryCurrency = ({ + countryName, + amountInDollar, +}: { + countryName: CountryName; + amountInDollar: number; +}) => pipe( - user.token, - option.match( - () => AuthClient.createToken(user), - (token) => + getCountryCode(countryName), + either.map( + countryCode => pipe( - AuthClient.refreshToken({ user, token }), - rte.chainW(({ newToken }) => - pipe(newToken.hash, user.updateToken, rte.chainEitherKW(storeUser)), + getCountryCurrency(countryCode), + option.map(currency => + pipe( + amountInDollar, + converFromDollar(currency), + convertedAmount => + console.log( + `converted amount for country ${countryCode} is ${convertedAmount}`, + ), + ), + ), ), ), ), ); // Good -const updateUserToken = (user: User) => - flow(user.updateToken, rte.chainEitherKW(storeUser)); - -const refreshAndUpdateUserToken = (user: User) => (token: Token) => +const convertDollarAmountInCountryCodeCurrency = ({ + amountInDollar, + countryCode, +}: { + amountInDollar: number; + countryCode: CountryCode; +}) => pipe( - AuthClient.refreshToken({ user, token }), - rte.chainW(({ newToken }) => updateUserToken(user)(newToken.hash)), + getCurrencyFromCountryCode(countryCode), + option.map(currency => { + const convertedDollarAmount = convertFromDollar(currency)(currency); + console.log( + `converted amount for country ${countryCode} is ${convertedAmount}`, + ); + }), ); -const refreshUserToken = (user: User) => +const convertDollarAmountInCountryCurrency = ({ + amountInDollar, + countryName, +}: { + amountInDollar: number; + countryName: CountryName; +}) => pipe( - user.token, - option.match( - () => AuthClient.createToken(user), - renewAndUpdateUserToken(user), + getCountryCode(countryName), + either.map(countryCode => + convertDollarAmountToCountryCodeCurrency({ amountInDollar, countryCode }), ), ); + ```