11import { toString as dateToString } from "./Date.js" ;
22import { compare as numericCompare , isNumeric , multiply , Numeric , toExponential , toFixed , toHex , toPrecision } from "./Numeric.js" ;
33import { escape } from "./RegExp.js" ;
4- import { toString } from "./Types.js" ;
4+ import { FSharpRef , toString } from "./Types.js" ;
55
6- const fsFormatRegExp = / ( ^ | [ ^ % ] ) % ( [ 0 + \- ] * ) ( \d + ) ? (?: \. ( \d + ) ) ? ( \w ) / ;
6+ const fsFormatRegExp = / ( ^ | [ ^ % ] ) % ( [ 0 + \- ] * ) ( \* | \ d+ ) ? (?: \. ( \d + ) ) ? ( \w ) / ;
77const interpolateRegExp = / (?: ( ^ | [ ^ % ] ) % ( [ 0 + \- ] * ) ( \d + ) ? (?: \. ( \d + ) ) ? ( \w ) ) ? % P \( \) / g;
88const formatRegExp = / \{ ( \d + ) ( , - ? \d + ) ? (?: \: ( [ a - z A - Z ] ) ( \d { 0 , 2 } ) | \: ( .+ ?) ) ? \} / g;
99
@@ -184,7 +184,7 @@ function formatReplacement(rep: any, prefix: any, flags: any, padLength: any, pr
184184 } else {
185185 rep = toString ( rep ) ;
186186 }
187- padLength = parseInt ( padLength , 10 ) ;
187+ padLength = typeof padLength === "number" ? padLength : parseInt ( padLength , 10 ) ;
188188 if ( ! isNaN ( padLength ) ) {
189189 const zeroFlag = flags . indexOf ( "0" ) >= 0 ; // Use '0' for left padding
190190 const minusFlag = flags . indexOf ( "-" ) >= 0 ; // Right padding
@@ -201,22 +201,33 @@ function formatReplacement(rep: any, prefix: any, flags: any, padLength: any, pr
201201 return prefix ? prefix + rep : rep ;
202202}
203203
204- function formatOnce ( str2 : string , rep : any ) {
205- return str2 . replace ( fsFormatRegExp , ( _ , prefix , flags , padLength , precision , format ) => {
204+ function formatOnce ( str2 : string , rep : any , padRef : FSharpRef < number | null > ) {
205+ return str2 . replace ( fsFormatRegExp , ( match , prefix , flags , padLength , precision , format ) => {
206+ if ( padRef . contents != null ) {
207+ padLength = padRef . contents ;
208+ padRef . contents = null ;
209+ }
210+ else if ( padLength === "*" ) {
211+ if ( rep < 0 ) {
212+ throw new Error ( "Non-negative number required" ) ;
213+ }
214+ padRef . contents = rep ;
215+ return match ;
216+ }
206217 const once = formatReplacement ( rep , prefix , flags , padLength , precision , format ) ;
207218 return once . replace ( / % / g, "%%" ) ;
208219 } ) ;
209220}
210221
211- function createPrinter ( str : string , cont : ( ...args : any [ ] ) => any ) {
222+ function createPrinter ( str : string , cont : ( ...args : any [ ] ) => any , padRef = new FSharpRef < number | null > ( null ) ) {
212223 return ( ...args : any [ ] ) => {
213224 // Make a copy as the function may be used several times
214225 let strCopy = str ;
215226 for ( const arg of args ) {
216- strCopy = formatOnce ( strCopy , arg ) ;
227+ strCopy = formatOnce ( strCopy , arg , padRef ) ;
217228 }
218229 return fsFormatRegExp . test ( strCopy )
219- ? createPrinter ( strCopy , cont )
230+ ? createPrinter ( strCopy , cont , padRef )
220231 : cont ( strCopy . replace ( / % % / g, "%" ) ) ;
221232 } ;
222233}
0 commit comments