Stop using util.format.

This will massively shrink the browserified footprint.
This commit is contained in:
Ruben Vermeersch 2013-12-18 16:46:09 +01:00
parent 0d4f80e7f6
commit 6b2cb2e71e
3 changed files with 27 additions and 10 deletions

View File

@ -1,5 +1,5 @@
var fs = require('fs'), var fs = require('fs'),
util = require('util'); isArray = require('lodash.isarray');
function trim(string) { function trim(string) {
return string.replace(/^\s+|\s+$/g, ''); return string.replace(/^\s+|\s+$/g, '');
@ -30,7 +30,7 @@ PO.prototype.toString = function () {
var keys = Object.keys(this.headers); var keys = Object.keys(this.headers);
keys.forEach(function (key) { keys.forEach(function (key) {
lines.push(util.format('"%s: %s\\n"', key, that.headers[key])); lines.push('"' + key + ': ' + that.headers[key] + '\\n"');
}); });
lines.push(''); lines.push('');
@ -174,40 +174,40 @@ PO.Item.prototype.toString = function () {
var _process = function (keyword, text, i) { var _process = function (keyword, text, i) {
var lines = [], var lines = [],
parts = text.split(/\n/), parts = text.split(/\n/),
index = typeof i !== 'undefined' ? util.format('[%d]', i) : ''; index = typeof i !== 'undefined' ? '[' + i + ']' : '';
if (parts.length > 1) { if (parts.length > 1) {
lines.push(util.format('%s%s ""', keyword, index)); lines.push(keyword + index + ' ""');
parts.forEach(function (part) { parts.forEach(function (part) {
lines.push(util.format('"%s"', part)); lines.push('"' + part + '"');
}); });
} }
else { else {
lines.push(util.format('%s%s "%s"', keyword, index, text)); lines.push(keyword + index + ' "' + text + '"');
} }
return lines; return lines;
}; };
if (this.references.length > 0) { if (this.references.length > 0) {
this.references.forEach(function (ref) { this.references.forEach(function (ref) {
lines.push(util.format('#: %s', ref)); lines.push('#: ' + ref);
}); });
} }
var flags = Object.keys(this.flags); var flags = Object.keys(this.flags);
if (flags.length > 0) { if (flags.length > 0) {
lines.push(util.format('#, %s', flags.join(","))); lines.push('#, ' + flags.join(","));
} }
['msgid', 'msgid_plural', 'msgstr'].forEach(function (keyword) { ['msgid', 'msgid_plural', 'msgstr'].forEach(function (keyword) {
var text = that[keyword]; var text = that[keyword];
if (text != null) { if (text != null) {
if (util.isArray(text) && text.length > 1) { if (isArray(text) && text.length > 1) {
text.forEach(function (t, i) { text.forEach(function (t, i) {
lines = lines.concat(_process(keyword, t, i)); lines = lines.concat(_process(keyword, t, i));
}); });
} }
else { else {
text = util.isArray(text) ? text.join() : text; text = isArray(text) ? text.join() : text;
lines = lines.concat(_process(keyword, text)); lines = lines.concat(_process(keyword, text));
} }
} }

View File

@ -24,5 +24,8 @@
"grunt-contrib-watch": "~0.5.3", "grunt-contrib-watch": "~0.5.3",
"grunt-contrib-jshint": "~0.7.2", "grunt-contrib-jshint": "~0.7.2",
"grunt-mocha-cli": "~1.4.0" "grunt-mocha-cli": "~1.4.0"
},
"dependencies": {
"lodash.isarray": "~2.4.1"
} }
} }

View File

@ -23,4 +23,18 @@ describe('Write', function () {
var str = po.toString(); var str = po.toString();
assertHasLine(str, "#, fuzzy"); 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\"");
});
}); });