CB-9835 Downgrade properties-parser to prevent failures in Node < 4.x

This commit is contained in:
Vladimir Kotikov
2015-10-21 13:21:10 +03:00
parent 12c282ce5c
commit 1151856d38
4 changed files with 23 additions and 104 deletions
+10 -76
View File
@@ -219,42 +219,8 @@ function isNewLineRange(range) {
return false;
}
function escapeMaker(escapes) {
return function escapeKey(key) {
var zeros = [ "", "0", "00", "000" ];
var buf = [];
for(var i = 0; i < key.length; i++) {
var chr = key.charAt(i);
if(escapes[chr]) { buf.push(escapes[chr]); continue; }
var code = chr.codePointAt(0);
if(code <= 0x7F) { buf.push(chr); continue; }
var hex = code.toString(16);
buf.push("\\u");
buf.push(zeros[4 - hex.length]);
buf.push(hex);
}
return buf.join("");
};
}
var escapeKey = escapeMaker({ " ": "\\ ", "\n": "\\n", ":": "\\:", "=": "\\=" });
var escapeVal = escapeMaker({ "\n": "\\n" });
function Editor(text, options) {
if (typeof text === 'object') {
options = text;
text = null;
}
function Editor(text, path) {
text = text || "";
var path = options.path;
var separator = options.separator || '=';
var ranges = stringToRanges(text);
var obj = rangesToObject(ranges, text);
@@ -280,15 +246,10 @@ function Editor(text, options) {
if(val == null) { this.unset(key); return; }
obj[key] = val;
var escapedKey = escapeKey(key);
var escapedVal = escapeVal(val);
var range = keyRange[key];
if(!range) {
keyRange[key] = range = {
type: "literal",
text: escapedKey + separator + escapedVal
};
keyRange[key] = range = { type: "literal", text: key + "=" + val };
var prevRange = ranges[ranges.length - 1];
if(prevRange != null && !isNewLineRange(prevRange)) {
@@ -303,10 +264,10 @@ function Editor(text, options) {
}
if(range.type === "literal") {
range.text = escapedKey + separator + escapedVal;
range.text = key + "=" + val;
if(range.comment != null) { range.text = range.comment + range.text; }
} else if(range.type === "key-value") {
range.children[2] = { type: "literal", text: escapedVal };
range.children[2] = { type: "literal", text: val };
} else {
throw "Unknown node type: " + range.type;
}
@@ -356,48 +317,21 @@ function Editor(text, options) {
}
newPath = newPath || path;
if(!newPath) {
if (callback) {
return callback("Unknown path");
}
throw new Error("Unknown path");
}
if (callback) {
fs.writeFile(newPath, this.toString(), callback);
} else {
fs.writeFileSync(newPath, this.toString());
}
if(!newPath) { callback("Unknown path"); }
fs.writeFile(newPath, this.toString(), callback || function() {});
};
}
function createEditor(/*path, options, callback*/) {
var path, options, callback;
var args = Array.prototype.slice.call(arguments);
for (var i = 0; i < args.length; i ++) {
var arg = args[i];
if (!path && typeof arg === 'string') {
path = arg;
} else if (!options && typeof arg === 'object') {
options = arg;
} else if (!callback && typeof arg === 'function') {
callback = arg;
}
}
options = options || {};
path = path || options.path;
callback = callback || options.callback;
options.path = path;
function createEditor(path, callback) {
if(!path) { return new Editor(); }
if(!path) { return new Editor(options); }
if(!callback) { return new Editor(fs.readFileSync(path).toString(), options); }
if(!callback) { return new Editor(fs.readFileSync(path).toString(), path); }
return fs.readFile(path, function(err, text) {
if(err) { return callback(err, null); }
text = text.toString();
return callback(null, new Editor(text, options));
return callback(null, new Editor(text, path));
});
}