@@ -161,33 +161,49 @@ function formatN(d, precision) {
161161 * @return {String } return converted values
162162 *
163163 */
164- function formatC ( d , precisionOrFormat ) {
165- if ( d !== null ) console . log ( "formatC" , d , precisionOrFormat ) ;
164+ function formatC ( d , precisionOrFormat , currencyCode ) {
165+ if ( d !== null ) console . log ( "formatC d:" , d ) ;
166+ if ( precisionOrFormat !== null )
167+ console . log ( "formatC precisionOrFormat:" , precisionOrFormat ) ;
168+ if ( currencyCode !== null ) console . log ( "formatC currencyCode:" , currencyCode ) ;
169+
170+ // Default currency to USD if not provided
171+ var _currency = currencyCode || "USD" ;
172+
166173 if ( d !== null && typeof d !== "undefined" ) {
167- var _locale = locale [ this . lang ] || locale . en ; // TODO optimize, this test should be done before
168- var _currency = this . modifiedCurrencyTarget || this . currency . target ;
174+ var _locale = locale [ this . lang ] || locale . en ;
175+
176+ // Access currency information
169177 var _currencyInfo = currency [ _currency ] ;
178+ if ( ! _currencyInfo ) {
179+ console . error ( "Invalid or unsupported currency code:" , _currency ) ;
180+ return d ; // Optionally handle this case more gracefully
181+ }
182+
170183 var _precision = _currencyInfo . precision ;
171184 var _customPrec = parseInt ( precisionOrFormat , 10 ) ;
172- var _formatFn = _locale . currency . L ;
185+
186+ // Determine precision or formatting function
173187 if ( ! isNaN ( _customPrec ) ) {
174188 _precision = _customPrec ;
175- } else if ( _locale . currency [ precisionOrFormat ] instanceof Function ) {
189+ } else if (
190+ typeof precisionOrFormat === "string" &&
191+ _locale . currency [ precisionOrFormat ] instanceof Function
192+ ) {
176193 _formatFn = _locale . currency [ precisionOrFormat ] ;
194+ } else {
195+ _formatFn = _locale . currency . L ; // Default formatting function
177196 }
178- var _valueRaw = _format ( convCurr . call ( this , d ) , _locale . number , _precision ) ;
179- // reset modifiedCurrencyTarget for next use
180- this . modifiedCurrencyTarget = null ;
181- return _formatFn (
182- _valueRaw ,
183- _currencyInfo . symbol ,
184- _currencyInfo . minSymbol ,
185- // eslint-disable-next-line eqeqeq
186- d != 1 ? _currencyInfo . major + "s" : _currencyInfo . major ,
187- // eslint-disable-next-line eqeqeq
188- d != 1 ? _currencyInfo . minor + "s" : _currencyInfo . minor ,
189- _currencyInfo . name
197+
198+ // Convert and format the value
199+ var _valueRaw = _format (
200+ convCurr . call ( this , d , _currency ) ,
201+ _locale . number ,
202+ _precision
190203 ) ;
204+
205+ // Prepend the currency symbol and return the result
206+ return _currencyInfo . symbol + _valueRaw ;
191207 }
192208 return d ;
193209}
0 commit comments