Skip to content

Commit 2431357

Browse files
committed
Remove options parameter for echo
1 parent 97fddf3 commit 2431357

File tree

6 files changed

+64
-40
lines changed

6 files changed

+64
-40
lines changed

runtime/index.js

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {transpileJavaScript} from "@observablehq/notebook-kit";
22
import {Runtime} from "@observablehq/runtime";
3-
import inspector from "object-inspect";
43
import {parse} from "acorn";
54
import {group} from "d3-array";
65
import {dispatch as d3Dispatch} from "d3-dispatch";
76
import * as stdlib from "./stdlib.js";
87
import {OUTPUT_MARK, ERROR_MARK} from "./constant.js";
8+
import {Inspector} from "./inspect.js";
99

1010
const OUTPUT_PREFIX = `//${OUTPUT_MARK}`;
1111

@@ -37,35 +37,15 @@ function debounce(fn, delay = 0) {
3737
};
3838
}
3939

40-
function isMultiline(value) {
41-
const isString = typeof value === "string";
42-
if (!isString) return false;
43-
const lines = value.split("\n");
44-
return lines.length > 1;
40+
function merge(...strings) {
41+
return strings.join("\n");
4542
}
4643

47-
function inspect(value, {limit = 200, quote = "double", indent = null} = {}) {
48-
if (isMultiline(value)) return value;
49-
if (typeof value === "string" && !quote) return value;
50-
const string = inspector(value, {indent, quoteStyle: quote});
51-
if (string.length > limit) return string.slice(0, limit) + "…";
52-
return string;
53-
}
54-
55-
function format(value, options, prefix) {
56-
const string = inspect(value, options);
44+
function addPrefix(string, prefix) {
5745
const lines = string.split("\n");
5846
return lines.map((line) => `${prefix} ${line}`).join("\n");
5947
}
6048

61-
function formatOutput(value, options) {
62-
return format(value, options, OUTPUT_PREFIX);
63-
}
64-
65-
function formatError(value, options) {
66-
return format(value, options, ERROR_PREFIX);
67-
}
68-
6949
export function createRuntime(initialCode) {
7050
let code = initialCode;
7151
let prevCode = null;
@@ -85,9 +65,22 @@ export function createRuntime(initialCode) {
8565
const start = node.start;
8666
const {values} = node.state;
8767
if (values.length) {
88-
const f = (v, o) => (isError(v) ? formatError(v, o) : formatOutput(v, o));
89-
const output = values.map(({value, options}) => f(value, options)).join("\n") + "\n";
90-
changes.push({from: start, insert: output});
68+
let output = "";
69+
let error = false;
70+
for (let i = 0; i < values.length; i++) {
71+
const line = values[i];
72+
const formatted = line.map((v) => {
73+
if (isError(v)) error = true;
74+
const inspector = v instanceof Inspector ? v : new Inspector(v);
75+
return inspector.format();
76+
});
77+
const merged = merge(...formatted);
78+
output += merged;
79+
output += i < values.length - 1 ? "\n" : "";
80+
}
81+
const prefix = error ? ERROR_PREFIX : OUTPUT_PREFIX;
82+
const prefixed = addPrefix(output, prefix);
83+
changes.push({from: start, insert: prefixed + "\n"});
9184
}
9285
}
9386

@@ -147,7 +140,7 @@ export function createRuntime(initialCode) {
147140
const offset = loc.line === 1 ? 0 : 1;
148141
const from = prevLine.join("\n").length + offset;
149142
const changes = removeChanges(code);
150-
const errorMsg = formatError(error) + "\n";
143+
const errorMsg = addPrefix(new Inspector(error).format(), ERROR_PREFIX) + "\n";
151144
changes.push({from, insert: errorMsg});
152145
dispatch(changes);
153146
return null;
@@ -189,9 +182,9 @@ export function createRuntime(initialCode) {
189182
return changes;
190183
}
191184

192-
function echo(state, value, options) {
185+
function echo(state, ...values) {
193186
if (!isRunning) return;
194-
state.values.push({value, options});
187+
state.values.push(values);
195188
rerun(code);
196189
}
197190

@@ -270,13 +263,13 @@ export function createRuntime(initialCode) {
270263
inputs.filter((i) => i !== "echo" && i !== "clear"),
271264
() => {
272265
const version = v._version; // Capture version on input change.
273-
return (value, options) => {
266+
return (value, ...args) => {
274267
if (version < docVersion) throw new Error("stale echo");
275268
else if (state.variables[0] !== v) throw new Error("stale echo");
276269
else if (version > docVersion) clear(state);
277270
docVersion = version;
278-
echo(state, value, options);
279-
return value;
271+
echo(state, value, ...args);
272+
return args.length ? [value, ...args] : value;
280273
};
281274
},
282275
);

runtime/inspect.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import objectInspector from "object-inspect";
2+
3+
function isMultiline(value) {
4+
const isString = typeof value === "string";
5+
if (!isString) return false;
6+
const lines = value.split("\n");
7+
return lines.length > 1;
8+
}
9+
10+
function inspector(value, {limit = 200, quote = "double", indent = null} = {}) {
11+
if (isMultiline(value)) return value;
12+
if (typeof value === "string" && !quote) return value;
13+
const string = objectInspector(value, {indent, quoteStyle: quote});
14+
if (string.length > limit) return string.slice(0, limit) + "…";
15+
return string;
16+
}
17+
18+
export class Inspector {
19+
constructor(value, options = {}) {
20+
this.value = value;
21+
this.options = options;
22+
}
23+
format() {
24+
return inspector(this.value, this.options);
25+
}
26+
}
27+
28+
export function inspect(value, options) {
29+
return new Inspector(value, options);
30+
}

runtime/stdlib.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export {require} from "d3-require";
22
export {now} from "./now.js";
33
export {interval} from "./interval.js";
4+
export {inspect, Inspector} from "./inspect.js";
45
export * from "./controls";

test/js/generator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export const generator = `const now = echo(recho.now());
22
33
const x = echo(Math.abs(~~(Math.sin(now / 1000) * 22)));
44
5-
echo('~'.repeat(x) + '(๑•̀ㅂ•́)و✧', {quote: false});`;
5+
echo(recho.inspect('~'.repeat(x) + '(๑•̀ㅂ•́)و✧', {quote: false}));`;

test/js/inspector.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ echo(new Map([[1, 2], [3, 4]]));
1919
2020
echo(new Date('2025-09-23T12:00:00Z'));
2121
22-
echo(new Map([[1, 2], [3, 4]]), {indent: 2});
22+
echo(recho.inspect(new Map([[1, 2], [3, 4]]), {indent: 2}));
2323
2424
echo(new Array(100).fill(0));
2525
26-
echo(new Array(100).fill(0), {limit: 100});
26+
echo(recho.inspect(new Array(100).fill(0), {limit: 100}));
2727
28-
echo(new Array(100).fill(0), {limit: Infinity});`;
28+
echo(recho.inspect(new Array(100).fill(0), {limit: Infinity}));`;

test/output/inspector.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ echo(new Date('2025-09-23T12:00:00Z'));
3131
//➜ 1 => 2,
3232
//➜ 3 => 4
3333
//➜ }
34-
echo(new Map([[1, 2], [3, 4]]), {indent: 2});
34+
echo(recho.inspect(new Map([[1, 2], [3, 4]]), {indent: 2}));
3535

3636
//➜ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
3737
echo(new Array(100).fill(0));
3838

3939
//➜ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
40-
echo(new Array(100).fill(0), {limit: 100});
40+
echo(recho.inspect(new Array(100).fill(0), {limit: 100}));
4141

4242
//➜ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
43-
echo(new Array(100).fill(0), {limit: Infinity});
43+
echo(recho.inspect(new Array(100).fill(0), {limit: Infinity}));

0 commit comments

Comments
 (0)