From f507223705f0360f1d31f917d5eebfecc5b3f31d Mon Sep 17 00:00:00 2001 From: Mike Holly Date: Thu, 22 Dec 2011 16:17:04 -0800 Subject: [PATCH] Added Item.toString() and moved save logic there. --- lib/po.js | 81 +++++++++++++++++++++++++++++----------------------- tests/run.js | 4 +-- 2 files changed, 46 insertions(+), 39 deletions(-) diff --git a/lib/po.js b/lib/po.js index 62f5069..e3b6130 100644 --- a/lib/po.js +++ b/lib/po.js @@ -17,42 +17,8 @@ PO.prototype.save = function(filename, callback) { 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){ - if (item.references.length > 0) { - 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(item.toString()); lines.push(''); }); @@ -82,7 +48,7 @@ PO.load = function(filename, callback) { parts.forEach(function(part){ if (part.length < 1) return; var item = PO.Item.parse(part); - po.items.push(item); + po.items.push(item.toString()); }); callback && callback(po); @@ -96,6 +62,49 @@ PO.Item = function() { 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) { var item = new PO.Item(); diff --git a/tests/run.js b/tests/run.js index 0a72808..920c67e 100644 --- a/tests/run.js +++ b/tests/run.js @@ -7,9 +7,7 @@ po.load('text.po', function(_po){ _po.save('copy.po', function(){ var orig = fs.readFileSync('text.po'); var data = fs.readFileSync('copy.po'); - - console.log(data == orig); assert.equal(orig, data, 'Saved data is identical to original.'); }); }); - \ No newline at end of file +