Commit | Line | Data |
---|---|---|
3fb3743f JM |
1 | // Node vs browser behavior |
2 | var interop = {}; | |
3 | if (typeof module === 'undefined') { | |
4 | var exports = interop, | |
5 | GLOBAL = window; | |
6 | } | |
7 | ||
8 | function resolve_js(str) { | |
9 | if (str.match(/\./)) { | |
10 | var re = /^(.*)\.\([^\.]*)$/, | |
11 | match = re.exec(str); | |
12 | return [eval(match[0]), eval(str)]; | |
13 | } else { | |
14 | return [GLOBAL, eval(str)]; | |
15 | } | |
16 | } | |
17 | ||
18 | function js_to_mal(obj) { | |
19 | var cache = []; | |
20 | var str = JSON.stringify(obj, function(key, value) { | |
21 | if (typeof value === 'object' && value !== null) { | |
22 | if (cache.indexOf(value) !== -1) { | |
23 | // Circular reference found, discard key | |
24 | return; | |
25 | } | |
26 | // Store value in our collection | |
27 | cache.push(value); | |
28 | } | |
29 | return value; | |
30 | }); | |
31 | cache = null; // Enable garbage collection | |
32 | return JSON.parse(str); | |
33 | } | |
34 | ||
35 | exports.resolve_js = interop.resolve_js = resolve_js; | |
36 | exports.js_to_mal = interop.js_to_mal = js_to_mal; |