11import { format as isoformat } from "isoformat" ;
2+ import { html } from "htl" ;
23
34// Note: use formatAuto (or any other localized format) to present values to the
45// user; stringify is only intended for machine values.
@@ -8,16 +9,32 @@ export function stringify(x) {
89
910export const formatLocaleAuto = localize ( locale => {
1011 const formatNumber = formatLocaleNumber ( locale ) ;
11- return value => value == null ? ""
12+ return value => value === null ? gray ( "null" )
13+ : value === undefined ? gray ( "undefined" )
14+ : Number . isNaN ( value ) ? gray ( NaN )
1215 : typeof value === "number" ? formatNumber ( value )
1316 : value instanceof Date ? formatDate ( value )
17+ : Array . isArray ( value ) ? formatArray ( value )
18+ : typeof value === "object" ? formatObject ( value )
1419 : `${ value } ` ;
1520} ) ;
1621
1722export const formatLocaleNumber = localize ( locale => {
18- return value => value === 0 ? "0" : value . toLocaleString ( locale ) ; // handle negative zero
23+ return value => value === 0 ? "0" // handle negative zero
24+ : value === null ? gray ( "null" )
25+ : value === undefined ? gray ( "undefined" )
26+ : Number . isNaN ( value ) ? gray ( NaN )
27+ : value . toLocaleString ( locale ) ;
1928} ) ;
2029
30+ function formatObject ( o ) {
31+ return `{${ Object . entries ( o ) . map ( ( [ key , value ] ) => `${ key } : ${ value } ` ) . join ( ", " ) } }` ;
32+ }
33+
34+ function formatArray ( value ) {
35+ return `[${ value . slice ( 0 , 100 ) . map ( formatAuto ) . join ( ", " ) } ]` ;
36+ }
37+
2138export const formatAuto = formatLocaleAuto ( ) ;
2239
2340export const formatNumber = formatLocaleNumber ( ) ;
@@ -45,3 +62,7 @@ export function localize(f) {
4562 let key = localize , value ;
4663 return ( locale = "en" ) => locale === key ? value : ( value = f ( key = locale ) ) ;
4764}
65+
66+ function gray ( label ) {
67+ return html `< span class =gray > ${ label } ` ;
68+ }
0 commit comments