From 6b2cb2e71e347fbff6159bb1000a68441cf529eb Mon Sep 17 00:00:00 2001 From: Ruben Vermeersch Date: Wed, 18 Dec 2013 16:46:09 +0100 Subject: [PATCH] Stop using util.format. This will massively shrink the browserified footprint. --- lib/po.js | 20 ++++++++++---------- package.json | 3 +++ test/write.js | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/lib/po.js b/lib/po.js index 4a7fe88..2e0c586 100644 --- a/lib/po.js +++ b/lib/po.js @@ -1,5 +1,5 @@ var fs = require('fs'), - util = require('util'); + isArray = require('lodash.isarray'); function trim(string) { return string.replace(/^\s+|\s+$/g, ''); @@ -30,7 +30,7 @@ PO.prototype.toString = function () { var keys = Object.keys(this.headers); keys.forEach(function (key) { - lines.push(util.format('"%s: %s\\n"', key, that.headers[key])); + lines.push('"' + key + ': ' + that.headers[key] + '\\n"'); }); lines.push(''); @@ -174,40 +174,40 @@ PO.Item.prototype.toString = function () { var _process = function (keyword, text, i) { var lines = [], parts = text.split(/\n/), - index = typeof i !== 'undefined' ? util.format('[%d]', i) : ''; + index = typeof i !== 'undefined' ? '[' + i + ']' : ''; if (parts.length > 1) { - lines.push(util.format('%s%s ""', keyword, index)); + lines.push(keyword + index + ' ""'); parts.forEach(function (part) { - lines.push(util.format('"%s"', part)); + lines.push('"' + part + '"'); }); } else { - lines.push(util.format('%s%s "%s"', keyword, index, text)); + lines.push(keyword + index + ' "' + text + '"'); } return lines; }; if (this.references.length > 0) { this.references.forEach(function (ref) { - lines.push(util.format('#: %s', ref)); + lines.push('#: ' + ref); }); } var flags = Object.keys(this.flags); if (flags.length > 0) { - lines.push(util.format('#, %s', flags.join(","))); + lines.push('#, ' + flags.join(",")); } ['msgid', 'msgid_plural', 'msgstr'].forEach(function (keyword) { var text = that[keyword]; if (text != null) { - if (util.isArray(text) && text.length > 1) { + if (isArray(text) && text.length > 1) { text.forEach(function (t, i) { lines = lines.concat(_process(keyword, t, i)); }); } else { - text = util.isArray(text) ? text.join() : text; + text = isArray(text) ? text.join() : text; lines = lines.concat(_process(keyword, text)); } } diff --git a/package.json b/package.json index 0ceed20..f3f8ca0 100644 --- a/package.json +++ b/package.json @@ -24,5 +24,8 @@ "grunt-contrib-watch": "~0.5.3", "grunt-contrib-jshint": "~0.7.2", "grunt-mocha-cli": "~1.4.0" + }, + "dependencies": { + "lodash.isarray": "~2.4.1" } } diff --git a/test/write.js b/test/write.js index 0c0f610..9aece24 100644 --- a/test/write.js +++ b/test/write.js @@ -23,4 +23,18 @@ describe('Write', function () { var str = po.toString(); assertHasLine(str, "#, fuzzy"); }); + + it('write msgid', function () { + var input = fs.readFileSync(__dirname + '/fixtures/fuzzy.po', 'utf8'); + var po = PO.parse(input); + var str = po.toString(); + assertHasLine(str, "msgid \"Sources\""); + }); + + it('write msgstr', function () { + var input = fs.readFileSync(__dirname + '/fixtures/fuzzy.po', 'utf8'); + var po = PO.parse(input); + var str = po.toString(); + assertHasLine(str, "msgstr \"Source\""); + }); });