char/avril
(wip) why'd you have to go and make things so complicaaaaated?
git clone https://git.t4t.associates/char/avril
1738b3c
main
1import { 2NumberParseFloat , 3NumberParseInt , 4String , 5TypeError , 6} from "ext:core/primordials" ; 7import { inspect } from "./inspect.js" ; 8 9function format ( args :readonly unknown [], colors = false ) :string { 10if ( args . length === 0 ) return "" ; 11let text = "" ; 12let consumed = 0 ; 13if ( typeof args [ 0 ] === "string" && args . length > 1 ) { 14const template = args [ 0 ]; 15consumed = 1 ; 16for ( let index = 0 ; index < template . length ; index ++ ) { 17const character = template [ index ]; 18const token = template [ index + 1 ]; 19if ( character === "%" && token === "%" ) { 20text += "%" ; 21index ++ ; 22continue ; 23} 24if ( 25character !== "%" || consumed === args . length || 26! ( token === "s" || token === "d" || token === "i" || token === "f" || 27token === "o" || token === "O" || token === "c" ) 28) { 29text += character ; 30continue ; 31} 32const value = args [ consumed ++ ]; 33index ++ ; 34if ( token === "s" ) text += String ( value ); 35else if ( token === "d" || token === "i" ) { 36text += typeof value === "symbol" 37 ?"NaN" 38 :String ( NumberParseInt ( value , 10 )); 39} else if ( token === "f" ) { 40text += typeof value === "symbol" 41 ?"NaN" 42 :String ( NumberParseFloat ( value )); 43} else if ( token !== "c" ) { 44text += inspect ( value , { depth :token === "o" ?3 :2 , colors}); 45} 46} 47} 48for (; consumed < args . length ; consumed ++ ) { 49if ( consumed > 0 ) text += " " ; 50const value = args [ consumed ]; 51text += typeof value === "string" ?value :inspect ( value , { colors}); 52} 53return text ; 54} 55 56function labelString ( value :unknown ) :string { 57if ( typeof value === "symbol" ) { 58throw new TypeError ( "Cannot convert a Symbol value to a string" ); 59} 60return String ( value ); 61} 62 63export { format , labelString };