@groww-tech/ella
Version:
Ella is a utility-belt library for JavaScript that provides general purpose methods used in day to day programming.
157 lines (155 loc) • 6.92 kB
TypeScript
/**
* This function is used as a Wrapper around number (Handle every case currently used in Groww Web Project).
* @param {number | string} x - Number that you want to format with NumberFormatter
* @param {
* addCommas?: boolean;
* millionCommas?: boolean;
* fallback?: any;
* toFixedValue?: number;
* formatToLakhCrore?: boolean;
* formatToBillionTrillion?: boolean;
* decimals?: number ;
* isCurrency?:boolean;
* currency?: string;
* absoluteValue?: boolean;
* roundValue?:boolean;
* withSign?: boolean;
* spaceBetweenSignValue?:boolean;
* formatPaisaToRupee?: boolean;
* formatRupeeToPaisa?:boolean;
* plainNumber?:boolean;
* } numberConfig
*
* @remarks
* <br/>
* <h4>numberConfig properties</h4>
* <ul>
* <li> <span style="font-weight: bold;">addCommas</span> => Whether you need commas in your number </li>
* <li> <span style="font-weight: bold;">millionCommas</span> => Used for US-stocks commas </li>
* <li> <span style="font-weight: bold;">fallback</span> => Fallback if the backend fails and we are getting null or undefined or isNaN true or Infinite number (Default is 'NA') </li>
* <li> <span style="font-weight: bold;">toFixedValue</span> => Very well explanatory , eg. 10 = 10.00 </li>
* <li> <span style="font-weight: bold;">formatToLakhCrore</span> => It will convert to lakhs and crore accordingly with no currency sign </li>
* <li> <span style="font-weight: bold;">formatToBillionTrillion</span> => It will convert to million,billion trillion , specific to US-stocks </li>
* <li> <span style="font-weight: bold;">decimals</span> => Used in lakhs and crore, can be used further for other categories also </li>
* <li> <span style="font-weight: bold;">isCurrency</span> => Specify 'true' for currency format, only use for currency formatting, no other use </li>
* <li> <span style="font-weight: bold;">currency</span> => Default is 'INR', specify USD for US-stocks </li>
* <li> <span style="font-weight: bold;">absoluteValue</span> => Absolute value of the number eg. -10 = 10 </li>
* <li> <span style="font-weight: bold;">roundValue</span> => Rounding off of a number eg. 83.56 = 84 </li>
* <li> <span style="font-weight: bold;">withSign</span> => This will return string with sign of the number, eg. '+21.34' </li>
* <li> <span style="font-weight: bold;">spaceBetweenSignValue</span> => if you need +3.54 then it should be false, if you need '+ 3.54' , make it true, applicable only when withSign is true </li>
* <li> <span style="font-weight: bold;">formatPaisaToRupee</span> => Converting number from paisa to rupee </li>
* <li> <span style="font-weight: bold;">formatRupeeToPaisa</span> => Converting number from rupee to paisa </li>
* <li> <span style="font-weight: bold;">plainNumber</span> => Converting only to number </li>
* </ul>
* <br/>
* <div>
* <div style="font-weight:bold">defaultNumberConfig = { <br/></div>
* <div style="display:flex;">
* <div style="font-weight:bold">addCommas: </div>
* <div style="margin-left:4px">true</div>
* </div>
* <div style="display:flex;">
* <div style="font-weight:bold">millionCommas: </div>
* <div style="margin-left:4px">false</div>
* </div>
* <div style="display:flex;">
* <div style="font-weight:bold">fallback: </div>
* <div style="margin-left:4px">'NA'</div>
* </div>
* <div style="display:flex;">
* <div style="font-weight:bold">toFixedValue: </div>
* <div style="margin-left:4px">2</div>
* </div>
* <div style="display:flex;">
* <div style="font-weight:bold">formatToLakhCrore: </div>
* <div style="margin-left:4px">false</div>
* </div>
* <div style="display:flex;">
* <div style="font-weight:bold">formatToBillionTrillion: </div>
* <div style="margin-left:4px">false</div>
* </div>
* <div style="display:flex;">
* <div style="font-weight:bold">decimals: </div>
* <div style="margin-left:4px">2</div>
* </div>
* <div style="display:flex;">
* <div style="font-weight:bold">isCurrency: </div>
* <div style="margin-left:4px">false</div>
* </div>
* <div style="display:flex;">
* <div style="font-weight:bold">currency: </div>
* <div style="margin-left:4px">'INR'</div>
* </div>
* <div style="display:flex;">
* <div style="font-weight:bold">absoluteValue: </div>
* <div style="margin-left:4px">false</div>
* </div>
* <div style="display:flex;">
* <div style="font-weight:bold">roundValue: </div>
* <div style="margin-left:4px">false</div>
* </div>
* <div style="display:flex;">
* <div style="font-weight:bold">withSign: </div>
* <div style="margin-left:4px">false</div>
* </div>
* <div style="display:flex;">
* <div style="font-weight:bold">spaceBetweenSignValue: </div>
* <div style="margin-left:4px">false</div>
* </div>
* <div style="display:flex;">
* <div style="font-weight:bold">formatPaisaToRupee: </div>
* <div style="margin-left:4px">false</div>
* </div>
* <div style="display:flex;">
* <div style="font-weight:bold">formatRupeeToPaisa: </div>
* <div style="margin-left:4px">false</div>
* </div>
* <div style="display:flex;">
* <div style="font-weight:bold">plainNumber: </div>
* <div style="margin-left:4px">false</div>
* </div>
* };
</div>
*
* @example
* ```
* NumberFormatter(100023); // '1,00,023.00'
* NumberFormatter(100023,PRICE_CURRENCY); // '₹1,00,023.00'
* NumberFormatter(100023,PRICE_CURRENCY_TO_FIXED_ZERO); // '₹1,00,023'
* NumberFormatter(2869610500000,CONVERT_TO_BILLION_TRILLION); // '2.87T'
* NumberFormatter(100023,PRICE_CURRENCY_FALLBACK_ZERO); // '₹1,00,023.00' (If anything fails then fallback used as '₹0')
* NumberFormatter(100023,PRICE_CURRENCY_USD); // '$100,023.00'
* NumberFormatter(100023,CURRENCY_CONVERT_TO_RUPEE); // '₹1,000.23' (First it will convert from paisa to rupee and then to currency)
* NumberFormatter(31490000000,CONVERT_TO_LAKH_CRORE); // '3149.00Cr'
* NumberFormatter(100023, {
* ...PRICE_CURRENCY,
* withSign: true
* }); // '+₹1,00,023.00' (It will give you sign also with the currency formatting)
* NumberFormatter(100023, {
* ...PRICE_CURRENCY,
* withSign: true,
* spaceBetweenSignValue
* }); // '+ ₹1,00,023.00' (It will give you sign also (with space between value and sign) with the currency formatting)
* ```
*
*/
declare function NumberFormatter(num: string | number, numberConfig?: numberConfigType): any;
declare type numberConfigType = {
addCommas?: boolean;
millionCommas?: boolean;
fallback?: any;
toFixedValue?: number;
formatToLakhCrore?: boolean;
formatToBillionTrillion?: boolean;
decimals?: number;
isCurrency?: boolean;
currency?: string;
absoluteValue?: boolean;
roundValue?: boolean;
withSign?: boolean;
spaceBetweenSignValue?: boolean;
formatPaisaToRupee?: boolean;
formatRupeeToPaisa?: boolean;
plainNumber?: boolean;
};
export { NumberFormatter };