Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/common/safe-stringify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const circularReplacer = () => {
const seen = new WeakSet();
return (_, value) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) {
return Array.isArray(value) ? '[...]' : '{...}';
}
seen.add(value);
}
return value;
};
};

const sanitize = (input) => {
if (typeof input === "object" && input !== null) {
return JSON.stringify(input, circularReplacer());
} else {
return input;
}
};

const sanitizeVariableType = (input, type) => {
if (type === "list") {
return input.map(item => sanitize(item));
} else {
return sanitize(input);
}
};

export default sanitizeVariableType;
6 changes: 4 additions & 2 deletions src/scaffolding/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import styles from './style.css';
import {readAsText} from '../common/readers';
import downloadBlob from './download';

import sanitizeVariableType from '../common/safe-stringify';

class Monitor {
constructor (parent, monitor) {
this.parent = parent;
Expand Down Expand Up @@ -194,7 +196,7 @@ class VariableMonitor extends Monitor {
return;
}

let value = monitor.get('value');
let value = sanitizeVariableType(monitor.get('value'), '');
if (typeof value === 'number') {
value = Number(value.toFixed(6));
}
Expand Down Expand Up @@ -547,7 +549,7 @@ class ListMonitor extends Monitor {
this.root.style.width = `${this.width}px`;
this.root.style.height = `${this.height}px`;

this.updateValue(monitor.get('value'));
this.updateValue(sanitizeVariableType(monitor.get('value'), 'list'));
}

createRow (index) {
Expand Down