Skip to content

Commit 1594cf6

Browse files
committed
Support toReplString, allowing overriding of stringify default behaviour
1 parent 7291652 commit 1594cf6

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/__tests__/__snapshots__/stringify.ts.snap

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,51 @@ forceIt(lastStatementResult);
866866
}
867867
`;
868868

869+
exports[`String representation of objects with toReplString member calls toReplString: expectResult 1`] = `
870+
Object {
871+
"alertResult": Array [],
872+
"code": "const o = { toReplString: () => '<RUNE>' };
873+
stringify(o);",
874+
"displayResult": Array [],
875+
"errors": Array [],
876+
"parsedErrors": "",
877+
"result": "<RUNE>",
878+
"resultStatus": "finished",
879+
"transpiled": "const native = nativeStorage;
880+
const forceIt = native.operators.get(\\"forceIt\\");
881+
const callIfFuncAndRightArgs = native.operators.get(\\"callIfFuncAndRightArgs\\");
882+
const boolOrErr = native.operators.get(\\"boolOrErr\\");
883+
const wrap = native.operators.get(\\"wrap\\");
884+
const unaryOp = native.operators.get(\\"unaryOp\\");
885+
const binaryOp = native.operators.get(\\"binaryOp\\");
886+
const throwIfTimeout = native.operators.get(\\"throwIfTimeout\\");
887+
const setProp = native.operators.get(\\"setProp\\");
888+
const getProp = native.operators.get(\\"getProp\\");
889+
let lastStatementResult = undefined;
890+
const globals = native.globals;
891+
{
892+
{
893+
const o = {
894+
toReplString: wrap(() => ({
895+
isTail: false,
896+
value: '<RUNE>'
897+
}), \\"() => '<RUNE>'\\", native)
898+
};
899+
lastStatementResult = eval(\\"callIfFuncAndRightArgs(stringify, 2, 0, o);\\");
900+
globals.variables.set(\\"o\\", {
901+
kind: \\"const\\",
902+
getValue: () => {
903+
return o;
904+
}
905+
});
906+
}
907+
}
908+
forceIt(lastStatementResult);
909+
",
910+
"visualiseListResult": Array [],
911+
}
912+
`;
913+
869914
exports[`String representation of strings are nice: expectResult 1`] = `
870915
Object {
871916
"alertResult": Array [],

src/__tests__/stringify.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,16 @@ test('String representation of objects are nice', () => {
319319
).toMatchInlineSnapshot(`"{\\"a\\": 1, \\"b\\": true, \\"c\\": () => 1}"`)
320320
})
321321

322+
test('String representation of objects with toReplString member calls toReplString', () => {
323+
return expectResult(
324+
stripIndent`
325+
const o = { toReplString: () => '<RUNE>' };
326+
stringify(o);
327+
`,
328+
{ chapter: 100, native: true }
329+
).toMatchInlineSnapshot(`"<RUNE>"`)
330+
})
331+
322332
test('String representation of nested objects are nice', () => {
323333
return expectResult(
324334
stripIndent`

src/utils/stringify.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ ${indentify(indentString.repeat(indentLevel), valueStrs[1])}${arrSuffix}`
117117
return v.toString()
118118
} else if (ancestors.size > MAX_LIST_DISPLAY_LENGTH) {
119119
return '...<truncated>'
120+
} else if (typeof v.toReplString === 'function') {
121+
return v.toReplString()
120122
} else if (Array.isArray(v)) {
121123
return stringifyArray(v, indentLevel)
122124
} else {

0 commit comments

Comments
 (0)