diff options
author | Alon Zakai <alonzakai@gmail.com> | 2011-11-28 20:34:32 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2011-11-28 20:34:32 -0800 |
commit | 7b6798ecf48622d5c9fe9b20a5950bd5c3f13bb7 (patch) | |
tree | 7baa94e3bc1acb13a08900a29915e5a55cf8bc08 | |
parent | 2b46a65a8d91e0721a7cdd7f91e4899e07831eaa (diff) |
support for nested objects in library objects, including functions
-rw-r--r-- | src/jsifier.js | 14 | ||||
-rw-r--r-- | src/utility.js | 10 |
2 files changed, 11 insertions, 13 deletions
diff --git a/src/jsifier.js b/src/jsifier.js index 89beaa32..48fb7968 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -359,19 +359,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { snippet = '_' + snippet; } } else if (typeof snippet === 'object') { - if (snippet === null) { - snippet = 'null'; - } else { - var members = []; - for (var property in snippet) { - if (typeof snippet[property] === 'function') { - members.push(property + ': ' + snippet[property].toString()); - } else { - members.push(property + ': ' + JSON.stringify(snippet[property])); - } - } - snippet = '{' + members.join(', ') + ' }'; - } + snippet = stringifyWithFunctions(snippet); } else if (typeof snippet === 'function') { isFunction = true; snippet = snippet.toString(); diff --git a/src/utility.js b/src/utility.js index 737d515a..e49776d8 100644 --- a/src/utility.js +++ b/src/utility.js @@ -284,3 +284,13 @@ function jsonCompare(x, y) { return JSON.stringify(x) == JSON.stringify(y); } +function stringifyWithFunctions(obj) { + if (typeof obj === 'function') return obj.toString(); + if (obj === null || typeof obj !== 'object') return JSON.stringify(obj); + if (isArray(obj)) { + return '[' + obj.map(stringifyWithFunctions).join(',') + ']'; + } else { + return '{' + keys(obj).map(function(key) { return key + ':' + stringifyWithFunctions(obj[key]) }).join(',') + '}'; + } +} + |