Skip to content

Commit a2d8bb0

Browse files
committed
default format for arrays, objects, null and explicit undefined
closes #166 #164
1 parent 86fa5c0 commit a2d8bb0

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/format.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {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

910
export 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

1722
export 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+
2138
export const formatAuto = formatLocaleAuto();
2239

2340
export 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+
}

src/style.css

+4
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ form.__ns__-table {
242242
padding: 3px 6.5px 3px 0;
243243
}
244244

245+
.__ns__-table td .gray {
246+
color: #888;
247+
}
248+
245249
.__ns__-table tr > :not(:first-of-type) {
246250
padding-left: var(--length2);
247251
}

src/table.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {length} from "./css.js";
44
import {formatDate, formatLocaleAuto, formatLocaleNumber} from "./format.js";
55
import {newId} from "./id.js";
66
import {identity} from "./identity.js";
7-
import {defined, ascending, descending} from "./sort.js";
7+
import {ascending, descending} from "./sort.js";
88

99
const rowHeight = 22;
1010

@@ -130,8 +130,8 @@ function initialize(
130130
input.value = i;
131131
if (d != null) for (let j = 0; j < columns.length; ++j) {
132132
let column = columns[j];
133+
if (!(column in d)) continue;
133134
let value = d[column];
134-
if (!defined(value)) continue;
135135
value = format[column](value, i, data);
136136
if (!(value instanceof Node)) value = document.createTextNode(value);
137137
itr.childNodes[j + 1].appendChild(value);

0 commit comments

Comments
 (0)