forked from jamovi/jamovi-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sourcify.js
53 lines (47 loc) · 1.25 KB
/
sourcify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
const _ = require('underscore');
const jsesc = require('jsesc');
const sourcify = function(object, indent) {
if (typeof indent === 'undefined')
indent = ' ';
let str = '';
if (object === undefined) {
str = 'NULL';
}
else if (object === null) {
str = 'NULL';
}
else if (object === true) {
str = 'TRUE'
}
else if (object === false) {
str = 'FALSE'
}
else if (typeof(object) === 'number') {
str = '' + object;
}
else if (typeof(object) === 'string') {
str = jsesc(object, { json: true, wrap: true });
}
else if (_.isArray(object)) {
str = 'list('
let sep = '\n' + indent + ' ';
for (let value of object) {
str += sep + sourcify(value, indent + ' ');
sep = ',\n' + indent + ' ';
}
str += ')';
}
else if (_.isObject(object)) {
str = 'list(';
let sep = '';
for (let prop in object) {
let value = object[prop];
str += sep + '\n' + indent + ' ' + '`' + prop + '`' + '=' + sourcify(value, indent + ' ');
sep = ', '
}
str += ')';
}
return str;
}
module.exports = sourcify;