summaryrefslogtreecommitdiff
path: root/lib/Dumper.js
diff options
context:
space:
mode:
authorJeremy Faivre <jeremy.faivre@gmail.com>2014-07-22 10:19:12 +0200
committerJeremy Faivre <jeremy.faivre@gmail.com>2014-07-22 10:21:17 +0200
commitc6cbcf0326723c5f0013490d1fea489694790775 (patch)
treefa735c3ee627b64bd546165aed093d1f6cfce055 /lib/Dumper.js
parentc5a6a8601d31508bb3e5880b310b2918834e89c9 (diff)
downloadyamljs-c6cbcf0326723c5f0013490d1fea489694790775.tar.gz
Improve performances (more than 2 times faster), rewrite in coffee-script, fix various bugs.
This is the first pass of rewrite of the YAML library (in progress). The focus was on improving code readability, make it well-documented and fix most of the current issues. It didn't focus on the performances yet, even though they are better than the previous version. The next big step (not included in this first pass in progress) will focus on removing most RegExps because they are time consuming and they make the code working on a "line by line" basis. The code will move to a "character by character" parsing, allowing us to deeply improve performances, and add new features from the 1.2 Yaml spec.
Diffstat (limited to 'lib/Dumper.js')
-rw-r--r--lib/Dumper.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/Dumper.js b/lib/Dumper.js
new file mode 100644
index 0000000..8d32a56
--- /dev/null
+++ b/lib/Dumper.js
@@ -0,0 +1,46 @@
+// Generated by CoffeeScript 1.7.1
+var Dumper, Inline, Utils;
+
+Utils = require('./Utils');
+
+Inline = require('./Inline');
+
+Dumper = (function() {
+ function Dumper() {}
+
+ Dumper.indentation = 4;
+
+ Dumper.prototype.dump = function(input, inline, indent, exceptionOnInvalidType, objectEncoder) {
+ var isAHash, key, output, prefix, value, willBeInlined;
+ if (inline == null) {
+ inline = 0;
+ }
+ if (indent == null) {
+ indent = 0;
+ }
+ if (exceptionOnInvalidType == null) {
+ exceptionOnInvalidType = false;
+ }
+ if (objectEncoder == null) {
+ objectEncoder = null;
+ }
+ output = '';
+ prefix = (indent ? Utils.strRepeat(' ', indent) : '');
+ if (inline <= 0 || typeof input !== 'object' || Utils.isEmpty(input)) {
+ output += prefix + Inline.dump(input, exceptionOnInvalidType, objectEncoder);
+ } else {
+ isAHash = !(input instanceof Array);
+ for (key in input) {
+ value = input[key];
+ willBeInlined = inline - 1 <= 0 || typeof value !== 'object' || Utils.isEmpty(value);
+ output += prefix + (isAHash ? Inline.dump(key, exceptionOnInvalidType, objectEncoder) + ':' : '-') + (willBeInlined ? ' ' : "\n") + this.dump(value, inline - 1, (willBeInlined ? 0 : indent + this.indentation), exceptionOnInvalidType, objectEncoder) + (willBeInlined ? "\n" : '');
+ }
+ }
+ return output;
+ };
+
+ return Dumper;
+
+})();
+
+module.exports = Dumper;