Add JSHint. Fix style and some potential bugs.

This commit is contained in:
Ruben Vermeersch 2013-12-16 16:36:32 +01:00
parent f7d70f302b
commit e61e535a79
5 changed files with 240 additions and 187 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/node_modules

14
.jshintrc Normal file
View File

@ -0,0 +1,14 @@
{
"curly": true,
"eqeqeq": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"sub": true,
"undef": true,
"boss": true,
"eqnull": true,
"node": true,
"white": true
}

18
Gruntfile.coffee Normal file
View File

@ -0,0 +1,18 @@
module.exports = (grunt) ->
@loadNpmTasks('grunt-contrib-jshint')
@loadNpmTasks('grunt-contrib-watch')
@initConfig
jshint:
all: [ 'lib/*.js' ]
options:
jshintrc: '.jshintrc'
watch:
all:
files: ['lib/**.js', 'test/*/*']
tasks: ['test']
@registerTask 'default', ['test']
@registerTask 'build', ['jshint']
@registerTask 'test', ['build']

121
lib/po.js
View File

@ -1,29 +1,34 @@
var fs = require('fs') var fs = require('fs'),
, util = require('util'); util = require('util');
function trim(string) { function trim(string) {
return string.replace(/^\s+|\s+$/g, ''); return string.replace(/^\s+|\s+$/g, '');
}; }
var PO = function() { var PO = function () {
this.comments = []; this.comments = [];
this.headers = {}; this.headers = {};
this.items = []; this.items = [];
}; };
PO.prototype.save = function(filename, callback) { PO.prototype.save = function (filename, callback) {
fs.writeFile(filename, this.toString(), function(err) { fs.writeFile(filename, this.toString(), function (err) {
if (err) throw err; if (err) {
callback && callback(); throw err;
}
if (callback) {
callback();
}
}); });
}; };
PO.prototype.toString = function() { PO.prototype.toString = function () {
var lines = [] var lines = [],
, that = this; that = this;
if (this.comments) { if (this.comments) {
this.comments.forEach(function(comment) { this.comments.forEach(function (comment) {
lines.push('# ' + comment); lines.push('# ' + comment);
}); });
} }
@ -32,13 +37,13 @@ PO.prototype.toString = function() {
lines.push('msgstr ""'); lines.push('msgstr ""');
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(util.format('"%s: %s\\n"', key, that.headers[key]));
}); });
lines.push(''); lines.push('');
this.items.forEach(function(item) { this.items.forEach(function (item) {
lines.push(item.toString()); lines.push(item.toString());
lines.push(''); lines.push('');
}); });
@ -46,21 +51,23 @@ PO.prototype.toString = function() {
return lines.join("\n"); return lines.join("\n");
}; };
PO.load = function(filename, callback) { PO.load = function (filename, callback) {
fs.readFile(filename, 'utf-8', function(err, data) { fs.readFile(filename, 'utf-8', function (err, data) {
if (err) throw err; if (err) {
throw err;
}
var po = PO.parse(data); var po = PO.parse(data);
callback && callback(po); callback(po);
}); });
}; };
PO.parse = function(data) { PO.parse = function (data) {
//support both unix and windows newline formats. //support both unix and windows newline formats.
data = data.replace(/\r\n/g, '\n'); data = data.replace(/\r\n/g, '\n');
var po = new PO var po = new PO(),
, sections = data.split(/\n\n/) sections = data.split(/\n\n/),
, headers = sections.shift() headers = sections.shift(),
, lines = sections.join("\n").split(/\n/); lines = sections.join("\n").split(/\n/);
po.headers = { po.headers = {
'Project-Id-Version': '', 'Project-Id-Version': '',
@ -75,29 +82,29 @@ PO.parse = function(data) {
'Plural-Forms': '', 'Plural-Forms': '',
}; };
headers.split(/\n/).forEach(function(header) { headers.split(/\n/).forEach(function (header) {
if (header.match(/^#/)) { if (header.match(/^#/)) {
po.comments.push(header.replace(/^#\s*/, '')); po.comments.push(header.replace(/^#\s*/, ''));
} }
if (header.match(/^"/)) { if (header.match(/^"/)) {
header = header.trim().replace(/^"/, '').replace(/\\n"$/, ''); header = header.trim().replace(/^"/, '').replace(/\\n"$/, '');
var p = header.split(/:/) var p = header.split(/:/),
, name = p.shift().trim() name = p.shift().trim(),
, value = p.join(':').trim(); value = p.join(':').trim();
po.headers[name] = value; po.headers[name] = value;
} }
}); });
var item = new PO.Item() var item = new PO.Item(),
, context = null context = null,
, plural = 0; plural = 0;
function finish() { function finish() {
if (item.msgid.length > 0) { if (item.msgid.length > 0) {
po.items.push(item); po.items.push(item);
item = new PO.Item(); item = new PO.Item();
} }
}; }
function extract(string) { function extract(string) {
string = trim(string); string = trim(string);
@ -105,11 +112,11 @@ PO.parse = function(data) {
string = string.replace(/\\"/g, '"'); string = string.replace(/\\"/g, '"');
string = string.replace(/\\\\/g, '\\'); string = string.replace(/\\\\/g, '\\');
return string; return string;
}; }
while (lines.length > 0) { while (lines.length > 0) {
var line = trim(lines.shift()) var line = trim(lines.shift()),
, add = false; add = false;
if (line.match(/^#:/)) { // Reference if (line.match(/^#:/)) { // Reference
finish(); finish();
item.references.push(trim(line.replace(/^#:/, ''))); item.references.push(trim(line.replace(/^#:/, '')));
@ -131,28 +138,28 @@ PO.parse = function(data) {
var m = line.match(/^msgstr\[(\d+)\]/); var m = line.match(/^msgstr\[(\d+)\]/);
plural = m && m[1] ? parseInt(m[1]) : 0; plural = m && m[1] ? parseInt(m[1]) : 0;
item.msgstr[plural] = extract(line); item.msgstr[plural] = extract(line);
context = 'msgstr' context = 'msgstr';
} }
else { // Probably multiline string or blank else { // Probably multiline string or blank
if (line.length > 0) { if (line.length > 0) {
if (context == 'msgstr') { if (context === 'msgstr') {
item.msgstr[plural] += extract(line); item.msgstr[plural] += extract(line);
} }
else if (context == 'msgid') { else if (context === 'msgid') {
item.msgid += extract(line); item.msgid += extract(line);
} }
else if (context == 'msgid_plural') { else if (context === 'msgid_plural') {
item.msgid_plural += extract(line); item.msgid_plural += extract(line);
} }
} }
} }
}; }
finish(); finish();
return po; return po;
}; };
PO.Item = function() { PO.Item = function () {
this.msgid = ''; this.msgid = '';
this.references = []; this.references = [];
this.msgid_plural = null; this.msgid_plural = null;
@ -160,37 +167,37 @@ PO.Item = function() {
this.comments = []; this.comments = [];
}; };
PO.Item.prototype.toString = function() { PO.Item.prototype.toString = function () {
var lines = [] var lines = [],
, that = this; that = this;
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' ? util.format('[%d]', i) : '';
if (parts.length > 1) { if (parts.length > 1) {
lines.push(util.format('%s%s ""', keyword, index)); lines.push(util.format('%s%s ""', keyword, index));
parts.forEach(function(part) { parts.forEach(function (part) {
lines.push(util.format('"%s"', part)) lines.push(util.format('"%s"', part));
}); });
} }
else { else {
lines.push(util.format('%s%s "%s"', keyword, index, text)); lines.push(util.format('%s%s "%s"', keyword, index, text));
} }
return lines; 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) { 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]; var text = that[keyword];
if (text != null) { if (text != null) {
if (util.isArray(text) && text.length > 1) { if (util.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));
}); });
} }

View File

@ -1,11 +1,24 @@
{ {
"name" : "node-po", "name": "node-po",
"description" : "Simple library for loading and saving Gettext PO files.", "description": "Simple library for loading and saving Gettext PO files.",
"version" : "0.1.1", "version": "0.1.1",
"author" : "Mike Holly", "author": "Mike Holly",
"homepage" : "http://github.com/mikejholly/node-po", "homepage": "http://github.com/mikejholly/node-po",
"repository" : {"type" : "git", "url" : "http://github.com/mikejholly/node-po.git"}, "repository": {
"dependencies" : [], "type": "git",
"main" : "./lib/po", "url": "http://github.com/mikejholly/node-po.git"
"keywords" : ["i18n", "l10n", "gettext", "mo", "po"] },
"main": "./lib/po",
"keywords": [
"i18n",
"l10n",
"gettext",
"mo",
"po"
],
"devDependencies": {
"grunt": "~0.4.2",
"grunt-contrib-watch": "~0.5.3",
"grunt-contrib-jshint": "~0.7.2"
}
} }