Added Item.toString() and moved save logic there.

This commit is contained in:
Mike Holly 2011-12-22 16:17:04 -08:00
parent 79b8009b2a
commit f507223705
2 changed files with 46 additions and 39 deletions

View File

@ -17,42 +17,8 @@ PO.prototype.save = function(filename, callback) {
lines.push(''); lines.push('');
var _processEach = function(keyword, text, i) {
var lines = []
, parts = text.split(/\n/)
, index = typeof i != 'undefined' ? util.format('[%d]', i) : '';
if (parts.length > 1) {
lines.push(util.format('%s%s ""', keyword, index));
parts.forEach(function(part){
lines.push(util.format('"%s"', part))
});
}
else {
lines.push(util.format('%s%s "%s"', keyword, index, text));
}
return lines;
}
this.items.forEach(function(item){ this.items.forEach(function(item){
if (item.references.length > 0) { lines.push(item.toString());
item.references.forEach(function(ref){
lines.push(util.format('#: %s', ref));
});
['msgid', 'msgid_plural', 'msgstr'].forEach(function(keyword){
var text = item[keyword];
if (text != null) {
if (util.isArray(text) && text.length > 1) {
text.forEach(function(t, i){
lines = lines.concat(_processEach(keyword, t, i));
});
}
else {
text = util.isArray(text) ? text.join() : text;
lines = lines.concat(_processEach(keyword, text));
}
}
});
};
lines.push(''); lines.push('');
}); });
@ -82,7 +48,7 @@ PO.load = function(filename, callback) {
parts.forEach(function(part){ parts.forEach(function(part){
if (part.length < 1) return; if (part.length < 1) return;
var item = PO.Item.parse(part); var item = PO.Item.parse(part);
po.items.push(item); po.items.push(item.toString());
}); });
callback && callback(po); callback && callback(po);
@ -96,6 +62,49 @@ PO.Item = function() {
this.msgstr = []; this.msgstr = [];
}; };
PO.Item.prototype.toString = function() {
var lines = []
, that = this;
var _processEach = function(keyword, text, i) {
var lines = []
, parts = text.split(/\n/)
, index = typeof i != 'undefined' ? util.format('[%d]', i) : '';
if (parts.length > 1) {
lines.push(util.format('%s%s ""', keyword, index));
parts.forEach(function(part){
lines.push(util.format('"%s"', part))
});
}
else {
lines.push(util.format('%s%s "%s"', keyword, index, text));
}
return lines;
}
if (this.references.length > 0) {
this.references.forEach(function(ref){
lines.push(util.format('#: %s', ref));
});
['msgid', 'msgid_plural', 'msgstr'].forEach(function(keyword){
var text = that[keyword];
if (text != null) {
if (util.isArray(text) && text.length > 1) {
text.forEach(function(t, i){
lines = lines.concat(_processEach(keyword, t, i));
});
}
else {
text = util.isArray(text) ? text.join() : text;
lines = lines.concat(_processEach(keyword, text));
}
}
});
};
return lines.join("\n");
};
PO.Item.parse = function(chunk) { PO.Item.parse = function(chunk) {
var item = new PO.Item(); var item = new PO.Item();

View File

@ -7,9 +7,7 @@ po.load('text.po', function(_po){
_po.save('copy.po', function(){ _po.save('copy.po', function(){
var orig = fs.readFileSync('text.po'); var orig = fs.readFileSync('text.po');
var data = fs.readFileSync('copy.po'); var data = fs.readFileSync('copy.po');
console.log(data == orig);
assert.equal(orig, data, 'Saved data is identical to original.'); assert.equal(orig, data, 'Saved data is identical to original.');
}); });
}); });