You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Recently I've started using dinero.js@alpha for currency-related operations, and I've noticed one important thing between dinero.js@v1 and @alpha, in alpha we can't like... query the currency so actual serialization of currency would only require stored code instad complete JSON with details of currency - in real-world scenarios currencies defined in ISO standard are unlikely to be changed in near time and I think library should expose utilities to search currency.
I'm aware it's a alpha version which should probably not be used by me, however I think this feature is missing because on back-end we do not always have access to imports and some multi-currency systems handles more currencies than one have tested or thought about
Sadly, I'm not much able to contribute in near time, however I'm able to provide the "wrapper" that I've build for my usecase that helped me a lot with alpha version and maybe some of this will be useful for earlier mentioned utilities. (ignore type aliases, I have weird need to represent things in natural langauge)
importDinerofrom"dinero.js";import*asDineroCurrenciesfrom"@dinero.js/currencies";import{Tagged}from"type-fest";typeCurrencyDictionary={[keyinkeyoftypeofDineroCurrencies&string]: Currency;};exportconstcurrencies: CurrencyDictionary=DineroCurrencies/** * This is a type alias for Dinero. */exportvarMoney=Dinero;/** * This is a type alias for Dinero.Currency<number>. */exporttypeCurrency=Dinero.Currency<number>;exporttypeCurrencyCode=Tagged<keyoftypeofDineroCurrencies,"currency_code">;exportfunctionisCurrencyCode(value: unknown): value is CurrencyCode{if(typeofvalue!=="string")returnfalse;returnvalueincurrencies;}exportfunctionassertCurrencyCode(value: unknown): asserts value is CurrencyCode{if(!isCurrencyCode(value)){thrownewTypeError(`Expected ISO-4217 Currency Code, but received ${typeofvalue}${value} instead.`);}}/** * Returns a Dinero.Currency object for the given currency code. * * @param currency The ISO-4217 Currency Code. */exportfunctiongetCurrencyByCode(currency: CurrencyCode): Currency{returncurrencies[currencyaskeyoftypeofcurrencies];}/** * Function will validate if provided value is a valid Dinero.Currency object. * This is validation which allows definition of custom currency objects once * they are compatible with Dinero.Currency<number> type. * @param value */exportfunctionisCurrency(value: unknown): value is Currency{if(!(valueinstanceofObject))returnfalse;if(!("code"invalue))returnfalse;if(!("base"invalue))returnfalse;if(!("exponent"invalue))returnfalse;if(typeofvalue.code!=="string")returnfalse;if(typeofvalue.base!=="number")returnfalse;if(typeofvalue.exponent!=="number")returnfalse;if(!isCurrencyCode(value.code))returnfalse;returntrue;}/** * Function will validate if provided value is a valid Dinero.Currency object. * @param value */exportfunctionassertCurrency(value: unknown): asserts value is Currency{if(!isCurrency(value)){thrownewTypeError(`Expected Dinero.Currency<number> object, but received ${typeofvalue} (${JSON.stringify(value)}) instead.`);}}/** * Function will define currency object, this is useful for defining custom currency objects * @param currency */exportfunctiondefineCurrency(currency: Currency): Currency{assertCurrency(currency);returncurrency;}
The text was updated successfully, but these errors were encountered:
Recently I've started using
dinero.js@alpha
for currency-related operations, and I've noticed one important thing betweendinero.js@v1
and@alpha
, inalpha
we can't like... query the currency so actual serialization of currency would only require stored code instad complete JSON with details of currency - in real-world scenarios currencies defined in ISO standard are unlikely to be changed in near time and I think library should expose utilities to search currency.I'm aware it's a alpha version which should probably not be used by me, however I think this feature is missing because on back-end we do not always have access to imports and some multi-currency systems handles more currencies than one have tested or thought about
Sadly, I'm not much able to contribute in near time, however I'm able to provide the "wrapper" that I've build for my usecase that helped me a lot with alpha version and maybe some of this will be useful for earlier mentioned utilities. (ignore type aliases, I have weird need to represent things in natural langauge)
The text was updated successfully, but these errors were encountered: