forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterop.js
39 lines (36 loc) · 1.01 KB
/
interop.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
// Node vs browser behavior
var interop = {};
if (typeof module === 'undefined') {
var exports = interop,
GLOBAL = window;
}
function resolve_js(str) {
if (str.match(/\./)) {
var re = /^(.*)\.[^\.]*$/,
match = re.exec(str);
return [eval(match[1]), eval(str)];
} else {
return [GLOBAL, eval(str)];
}
}
function js_to_mal(obj) {
if (obj === null || obj === undefined) {
return null;
}
var cache = [];
var str = JSON.stringify(obj, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;
}
// Store value in our collection
cache.push(value);
}
return value;
});
cache = null; // Enable garbage collection
return JSON.parse(str);
}
exports.resolve_js = interop.resolve_js = resolve_js;
exports.js_to_mal = interop.js_to_mal = js_to_mal;