4 examples of 'node string to json' in JavaScript

Every line of 'node string to json' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
49get json() {
50 var ports = [];
51 this.forEachPort(
52 port => ports.push(Port.fromJSON(port, {
53 graph: this.graph,
54 node: this
55 }))
56 );
57 return {
58 id: this.id,
59 data: this.data,
60 bounds: this.bounds.toArray(),
61 layer: this.layer,
62 scale: this.scale,
63 ports
64 };
65}
288function stringify(node) {
289 const { type } = node;
290 switch (type) {
291 case 'BooleanLiteral':
292 case 'NullLiteral':
293 case 'NumericLiteral':
294 case 'StringLiteral':
295 case 'TemplateLiteral':
296 return JSON.stringify(literalValue(node));
297
298 case 'Identifier':
299 return JSON.stringify(node.name);
300
301 case 'ArrayExpression':
302 return `[${node.elements.map(stringify)}]`;
303
304 case 'ObjectExpression':
305 return `{${node.properties.map(stringify).filter(Boolean)}}`;
306
307 case 'ObjectProperty': {
308 const { key, value } = node;
309 if (t.isIdentifier(value, { name: 'undefined' })) {
310 return '';
311 }
312 return `${stringify(key)}:${stringify(value)}`;
313 }
314 }
315
316 // istanbul ignore next
317 throw new Error(`Can't handle type "${type}"`);
318}
86function nodeStr(n) {
87 if (/[\u0000-\u001F\u0080-\uFFFF]/.test(n.value)) {
88 // use the convoluted algorithm to avoid broken low/high characters
89 var str = "";
90 for (var i = 0; i < n.value.length; i++) {
91 var c = n.value[i];
92 if (c <= "\x1F" || c >= "\x80") {
93 var cc = c.charCodeAt(0).toString(16);
94 while (cc.length < 4) cc = "0" + cc;
95 str += "\\u" + cc;
96 } else {
97 str += nodeStrEscape(c);
98 }
99 }
100 return '"' + str + '"';
101 }
102
103 return '"' + nodeStrEscape(n.value) + '"';
104}
1function nodeToString(node) {
2 var str = '';
3 if (node.nodeType == Node.ELEMENT_NODE) {
4 str += node.nodeName;
5 if (node.id)
6 str += '#' + node.id;
7 else if (node.class)
8 str += '.' + node.class;
9 } else if (node.nodeType == Node.TEXT_NODE) {
10 str += '\'' + node.data + '\'';
11 } else if (node.nodeType == Node.DOCUMENT_NODE) {
12 str += '#document';
13 }
14 return str;
15}

Related snippets