Socket
Socket
Sign inDemoInstall

gettext-parser

Package Overview
Dependencies
Maintainers
2
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gettext-parser - npm Package Compare versions

Comparing version 3.1.0 to 3.1.1

.eslintrc.js

4

CHANGELOG.md
# Change Log
## [3.1.0] - 2018-11-19
- Add error when PO contains unescaped double quotes (thx @probertson)
## [3.0.0] - 2018-11-10

@@ -61,2 +64,3 @@ - Remove support for old node versions

[3.1.0]: https://github.com/smhg/gettext-parser/compare/v3.0.0...v3.1.0
[3.0.0]: https://github.com/smhg/gettext-parser/compare/v2.1.0...v3.0.0

@@ -63,0 +67,0 @@ [2.1.0]: https://github.com/smhg/gettext-parser/compare/v2.0.0...v2.1.0

84

lib/mocompiler.js

@@ -1,7 +0,5 @@

'use strict';
const { Buffer } = require('safe-buffer');
const encoding = require('encoding');
const sharedFuncs = require('./shared');
var Buffer = require('safe-buffer').Buffer;
var encoding = require('encoding');
var sharedFuncs = require('./shared');
/**

@@ -15,3 +13,4 @@ * Exposes general compiler function. Takes a translation

module.exports = function (table) {
var compiler = new Compiler(table);
const compiler = new Compiler(table);
return compiler.compile();

@@ -26,4 +25,4 @@ };

*/
function Compiler (table) {
this._table = table || {};
function Compiler (table = {}) {
this._table = table;
this._table.headers = this._table.headers || {};

@@ -48,11 +47,10 @@ this._table.translations = this._table.translations || {};

Compiler.prototype._handleCharset = function () {
var parts = (this._table.headers['content-type'] || 'text/plain').split(';');
var contentType = parts.shift();
var charset = sharedFuncs.formatCharset(this._table.charset);
var params = [];
const parts = (this._table.headers['content-type'] || 'text/plain').split(';');
const contentType = parts.shift();
let charset = sharedFuncs.formatCharset(this._table.charset);
params = parts.map(function (part) {
var parts = part.split('=');
var key = parts.shift().trim();
var value = parts.join('=');
const params = parts.map(part => {
const parts = part.split('=');
const key = parts.shift().trim();
const value = parts.join('=');

@@ -63,3 +61,4 @@ if (key.toLowerCase() === 'charset') {

}
return 'charset=' + charset;
return `charset=${charset}`;
}

@@ -72,7 +71,7 @@

charset = this._table.charset || 'utf-8';
params.push('charset=' + charset);
params.push(`charset=${charset}`);
}
this._table.charset = charset;
this._table.headers['content-type'] = contentType + '; ' + params.join('; ');
this._table.headers['content-type'] = `${contentType}; ${params.join('; ')}`;

@@ -89,3 +88,3 @@ this._charset = charset;

Compiler.prototype._generateList = function () {
var list = [];
const list = [];

@@ -97,10 +96,12 @@ list.push({

Object.keys(this._table.translations).forEach(function (msgctxt) {
Object.keys(this._table.translations).forEach(msgctxt => {
if (typeof this._table.translations[msgctxt] !== 'object') {
return;
}
Object.keys(this._table.translations[msgctxt]).forEach(function (msgid) {
Object.keys(this._table.translations[msgctxt]).forEach(msgid => {
if (typeof this._table.translations[msgctxt][msgid] !== 'object') {
return;
}
if (msgctxt === '' && msgid === '') {

@@ -110,5 +111,4 @@ return;

var msgidPlural = this._table.translations[msgctxt][msgid].msgid_plural;
var key = msgid;
var value;
const msgidPlural = this._table.translations[msgctxt][msgid].msgid_plural;
let key = msgid;

@@ -123,3 +123,3 @@ if (msgctxt) {

value = [].concat(this._table.translations[msgctxt][msgid].msgstr || []).join('\u0000');
let value = [].concat(this._table.translations[msgctxt][msgid].msgstr || []).join('\u0000');

@@ -130,4 +130,4 @@ list.push({

});
}.bind(this));
}.bind(this));
});
});

@@ -144,7 +144,7 @@ return list;

Compiler.prototype._calculateSize = function (list) {
var msgidLength = 0;
var msgstrLength = 0;
var totalLength = 0;
let msgidLength = 0;
let msgstrLength = 0;
let totalLength = 0;
list.forEach(function (translation) {
list.forEach(translation => {
msgidLength += translation.msgid.length + 1; // + extra 0x00

@@ -181,6 +181,6 @@ msgstrLength += translation.msgstr.length + 1; // + extra 0x00

Compiler.prototype._build = function (list, size) {
var returnBuffer = Buffer.alloc(size.total);
var curPosition = 0;
var i;
var len;
const returnBuffer = Buffer.alloc(size.total);
let curPosition = 0;
let i;
let len;

@@ -236,13 +236,15 @@ // magic

Compiler.prototype.compile = function () {
var list = this._generateList();
var size = this._calculateSize(list);
const list = this._generateList();
const size = this._calculateSize(list);
// sort by msgid
list.sort(function (a, b) {
if (a.msgid > b.msgid) {
list.sort((left, right) => {
if (left.msgid > right.msgid) {
return 1;
}
if (a.msgid < b.msgid) {
if (left.msgid < right.msgid) {
return -1;
}
return 0;

@@ -249,0 +251,0 @@ });

@@ -1,6 +0,4 @@

'use strict';
const encoding = require('encoding');
const sharedFuncs = require('./shared');
var encoding = require('encoding');
var sharedFuncs = require('./shared');
/**

@@ -14,3 +12,4 @@ * Parses a binary MO object into translation table

module.exports = function (buffer, defaultCharset) {
var parser = new Parser(buffer, defaultCharset);
const parser = new Parser(buffer, defaultCharset);
return parser.parse();

@@ -62,2 +61,3 @@ };

this._writeFunc = 'writeUInt32LE';
return true;

@@ -67,6 +67,7 @@ } else if (this._fileContents.readUInt32BE(0) === this.MAGIC) {

this._writeFunc = 'writeUInt32BE';
return true;
} else {
return false;
}
return false;
};

@@ -79,10 +80,10 @@

Parser.prototype._loadTranslationTable = function () {
var offsetOriginals = this._offsetOriginals;
var offsetTranslations = this._offsetTranslations;
var position;
var length;
var msgid;
var msgstr;
let offsetOriginals = this._offsetOriginals;
let offsetTranslations = this._offsetTranslations;
let position;
let length;
let msgid;
let msgstr;
for (var i = 0; i < this._total; i++) {
for (let i = 0; i < this._total; i++) {
// msgid string

@@ -122,4 +123,4 @@ length = this._fileContents[this._readFunc](offsetOriginals);

Parser.prototype._handleCharset = function (headers) {
var headersStr = headers.toString();
var match;
const headersStr = headers.toString();
let match;

@@ -142,6 +143,6 @@ if ((match = headersStr.match(/[; ]charset\s*=\s*([\w-]+)/i))) {

Parser.prototype._addString = function (msgid, msgstr) {
var translation = {};
var parts;
var msgctxt;
var msgidPlural;
const translation = {};
let parts;
let msgctxt;
let msgidPlural;

@@ -148,0 +149,0 @@ msgid = msgid.split('\u0004');

@@ -1,7 +0,5 @@

'use strict';
const { Buffer } = require('safe-buffer');
const encoding = require('encoding');
const sharedFuncs = require('./shared');
var Buffer = require('safe-buffer').Buffer;
var encoding = require('encoding');
var sharedFuncs = require('./shared');
/**

@@ -13,9 +11,11 @@ * Comparator function for comparing msgid

*/
function compare (r1, r2) {
if (r1.msgid > r2.msgid) {
function compare ({ msgid: left }, { msgid: right }) {
if (left > right) {
return 1;
}
if (r2.msgid > r1.msgid) {
if (right > left) {
return -1;
}
return 0;

@@ -31,3 +31,4 @@ }

module.exports = function (table, options) {
var compiler = new Compiler(table, options);
const compiler = new Compiler(table, options);
return compiler.compile();

@@ -42,14 +43,18 @@ };

*/
function Compiler (table, options) {
this._table = table || {};
function Compiler (table = {}, options = {}) {
this._table = table;
this._table.headers = this._table.headers || {};
this._table.translations = this._table.translations || {};
this._options = options || {};
this._options = options;
if (!('foldLength' in this._options)) {
this._options.foldLength = 76;
}
if (!('sort' in this._options)) {
this._options.sort = false;
}
this._translations = [];
this._handleCharset();

@@ -66,4 +71,4 @@ }

Compiler.prototype._drawComments = function (comments) {
var lines = [];
var types = [{
const lines = [];
const types = [{
key: 'translator',

@@ -85,8 +90,9 @@ prefix: '# '

types.forEach(function (type) {
types.forEach(type => {
if (!comments[type.key]) {
return;
}
comments[type.key].split(/\r?\n|\r/).forEach(function (line) {
lines.push(type.prefix + line);
comments[type.key].split(/\r?\n|\r/).forEach(line => {
lines.push(`${type.prefix}${line}`);
});

@@ -105,12 +111,10 @@ });

*/
Compiler.prototype._drawBlock = function (block, override) {
override = override || {};
Compiler.prototype._drawBlock = function (block, override = {}) {
const response = [];
const msgctxt = override.msgctxt || block.msgctxt;
const msgid = override.msgid || block.msgid;
const msgidPlural = override.msgid_plural || block.msgid_plural;
const msgstr = [].concat(override.msgstr || block.msgstr);
let comments = override.comments || block.comments;
var response = [];
var comments = override.comments || block.comments;
var msgctxt = override.msgctxt || block.msgctxt;
var msgid = override.msgid || block.msgid;
var msgidPlural = override.msgid_plural || block.msgid_plural;
var msgstr = [].concat(override.msgstr || block.msgstr);
// add comments

@@ -129,5 +133,6 @@ if (comments && (comments = this._drawComments(comments))) {

response.push(this._addPOString('msgid_plural', msgidPlural));
msgstr.forEach(function (msgstr, i) {
response.push(this._addPOString('msgstr[' + i + ']', msgstr || ''));
}.bind(this));
msgstr.forEach((msgstr, i) => {
response.push(this._addPOString(`msgstr[${i}]`, msgstr || ''));
});
} else {

@@ -147,7 +152,7 @@ response.push(this._addPOString('msgstr', msgstr[0] || ''));

*/
Compiler.prototype._addPOString = function (key, value) {
key = (key || '').toString();
Compiler.prototype._addPOString = function (key = '', value = '') {
key = key.toString();
// escape newlines and quotes
value = (value || '').toString()
value = value.toString()
.replace(/\\/g, '\\\\')

@@ -159,3 +164,3 @@ .replace(/"/g, '\\"')

var lines = [value];
let lines = [value];

@@ -167,6 +172,6 @@ if (this._options.foldLength > 0) {

if (lines.length < 2) {
return key + ' "' + (lines.shift() || '') + '"';
} else {
return key + ' ""\n"' + lines.join('"\n"') + '"';
return `${key} "${lines.shift() || ''}"`;
}
return `${key} ""\n"${lines.join('"\n"')}"`;
};

@@ -178,11 +183,10 @@

Compiler.prototype._handleCharset = function () {
var parts = (this._table.headers['content-type'] || 'text/plain').split(';');
var contentType = parts.shift();
var charset = sharedFuncs.formatCharset(this._table.charset);
var params = [];
const parts = (this._table.headers['content-type'] || 'text/plain').split(';');
const contentType = parts.shift();
let charset = sharedFuncs.formatCharset(this._table.charset);
params = parts.map(function (part) {
var parts = part.split('=');
var key = parts.shift().trim();
var value = parts.join('=');
const params = parts.map(part => {
const parts = part.split('=');
const key = parts.shift().trim();
const value = parts.join('=');

@@ -193,3 +197,4 @@ if (key.toLowerCase() === 'charset') {

}
return 'charset=' + charset;
return `charset=${charset}`;
}

@@ -202,7 +207,7 @@

charset = this._table.charset || 'utf-8';
params.push('charset=' + charset);
params.push(`charset=${charset}`);
}
this._table.charset = charset;
this._table.headers['content-type'] = contentType + '; ' + params.join('; ');
this._table.headers['content-type'] = `${contentType}; ${params.join('; ')}`;

@@ -218,13 +223,15 @@ this._charset = charset;

Compiler.prototype.compile = function () {
var response = [];
var headerBlock = (this._table.translations[''] && this._table.translations['']['']) || {};
const headerBlock = (this._table.translations[''] && this._table.translations['']['']) || {};
let response = [];
Object.keys(this._table.translations).forEach(function (msgctxt) {
Object.keys(this._table.translations).forEach(msgctxt => {
if (typeof this._table.translations[msgctxt] !== 'object') {
return;
}
Object.keys(this._table.translations[msgctxt]).forEach(function (msgid) {
Object.keys(this._table.translations[msgctxt]).forEach(msgid => {
if (typeof this._table.translations[msgctxt][msgid] !== 'object') {
return;
}
if (msgctxt === '' && msgid === '') {

@@ -235,4 +242,4 @@ return;

response.push(this._table.translations[msgctxt][msgid]);
}.bind(this));
}.bind(this));
});
});

@@ -246,6 +253,5 @@ if (this._options.sort !== false) {

}
response = response.map(function (r) {
return this._drawBlock(r);
}.bind(this));
response = response.map(r => this._drawBlock(r));
response.unshift(this._drawBlock(headerBlock, {

@@ -257,5 +263,5 @@ msgstr: sharedFuncs.generateHeader(this._table.headers)

return Buffer.from(response.join('\n\n'), 'utf-8');
} else {
return encoding.convert(response.join('\n\n'), this._charset);
}
return encoding.convert(response.join('\n\n'), this._charset);
};

@@ -1,8 +0,6 @@

'use strict';
const encoding = require('encoding');
const sharedFuncs = require('./shared');
const Transform = require('readable-stream').Transform;
const util = require('util');
var encoding = require('encoding');
var sharedFuncs = require('./shared');
var Transform = require('readable-stream').Transform;
var util = require('util');
/**

@@ -16,3 +14,4 @@ * Parses a PO object into translation table

module.exports.parse = function (buffer, defaultCharset) {
var parser = new Parser(buffer, defaultCharset);
const parser = new Parser(buffer, defaultCharset);
return parser.parse();

@@ -72,7 +71,7 @@ };

*/
Parser.prototype._handleCharset = function (buf) {
var str = (buf || '').toString();
var pos;
var headers = '';
var match;
Parser.prototype._handleCharset = function (buf = '') {
const str = buf.toString();
let pos;
let headers = '';
let match;

@@ -136,5 +135,5 @@ if ((pos = str.search(/^\s*msgid/im)) >= 0) {

Parser.prototype._lexer = function (chunk) {
var chr;
let chr;
for (var i = 0, len = chunk.length; i < len; i++) {
for (let i = 0, len = chunk.length; i < len; i++) {
chr = chunk.charAt(i);

@@ -210,4 +209,6 @@

if (!this._node.value.match(this.symbols.keyNames)) {
var err = new SyntaxError('Error parsing PO data: Invalid key name "' + this._node.value + '" at line ' + this._lineNumber + '. This can be caused by an unescaped quote character in a msgid or msgstr value.');
const err = new SyntaxError(`Error parsing PO data: Invalid key name "${this._node.value}" at line ${this._lineNumber}. This can be caused by an unescaped quote character in a msgid or msgstr value.`);
err.lineNumber = this._lineNumber;
throw err;

@@ -232,6 +233,6 @@ }

Parser.prototype._joinStringValues = function (tokens) {
var lastNode;
var response = [];
const response = [];
let lastNode;
for (var i = 0, len = tokens.length; i < len; i++) {
for (let i = 0, len = tokens.length; i < len; i++) {
if (lastNode && tokens[i].type === this.types.string && lastNode.type === this.types.string) {

@@ -257,4 +258,5 @@ lastNode.value += tokens[i].value;

// parse comments
tokens.forEach(function (node) {
var comment, lines;
tokens.forEach(node => {
let comment;
let lines;

@@ -269,4 +271,6 @@ if (node && node.type === this.types.comments) {

};
lines = (node.value || '').split(/\n/);
lines.forEach(function (line) {
lines.forEach(line => {
switch (line.charAt(0) || '') {

@@ -292,3 +296,3 @@ case ':':

Object.keys(comment).forEach(function (key) {
Object.keys(comment).forEach(key => {
if (comment[key] && comment[key].length) {

@@ -299,3 +303,3 @@ node.value[key] = comment[key].join('\n');

}
}.bind(this));
});
};

@@ -310,6 +314,6 @@

Parser.prototype._handleKeys = function (tokens) {
var response = [];
var lastNode;
const response = [];
let lastNode;
for (var i = 0, len = tokens.length; i < len; i++) {
for (let i = 0, len = tokens.length; i < len; i++) {
if (tokens[i].type === this.types.key) {

@@ -339,8 +343,8 @@ lastNode = {

Parser.prototype._handleValues = function (tokens) {
var response = [];
var lastNode;
var curContext;
var curComments;
const response = [];
let lastNode;
let curContext;
let curComments;
for (var i = 0, len = tokens.length; i < len; i++) {
for (let i = 0, len = tokens.length; i < len; i++) {
if (tokens[i].key.toLowerCase() === 'msgctxt') {

@@ -404,4 +408,3 @@ curContext = tokens[i].value;

Parser.prototype._normalize = function (tokens) {
var msgctxt;
var table = {
const table = {
charset: this._charset,

@@ -411,4 +414,5 @@ headers: undefined,

};
let msgctxt;
for (var i = 0, len = tokens.length; i < len; i++) {
for (let i = 0, len = tokens.length; i < len; i++) {
msgctxt = tokens[i].msgctxt || '';

@@ -437,4 +441,6 @@

Parser.prototype._finalize = function (tokens) {
var data = this._joinStringValues(tokens);
let data = this._joinStringValues(tokens);
this._parseComments(data);
data = this._handleKeys(data);

@@ -478,4 +484,4 @@ data = this._handleValues(data);

PoParserTransform.prototype._transform = function (chunk, encoding, done) {
var i;
var len = 0;
let i;
let len = 0;

@@ -530,5 +536,6 @@ if (!chunk || !chunk.length) {

} catch (error) {
setImmediate(function () {
setImmediate(() => {
done(error);
});
return;

@@ -545,3 +552,3 @@ }

PoParserTransform.prototype._flush = function (done) {
var chunk;
let chunk;

@@ -560,5 +567,6 @@ if (this._cacheSize) {

} catch (error) {
setImmediate(function () {
setImmediate(() => {
done(error);
});
return;

@@ -565,0 +573,0 @@ }

@@ -1,4 +0,1 @@

'use strict';
// Expose to the world
module.exports.parseHeader = parseHeader;

@@ -15,13 +12,15 @@ module.exports.generateHeader = generateHeader;

*/
function parseHeader (str) {
var lines = (str || '').split('\n');
var headers = {};
function parseHeader (str = '') {
const lines = str.split('\n');
const headers = {};
lines.forEach(function (line) {
var parts = line.trim().split(':');
var key = (parts.shift() || '').trim().toLowerCase();
var value = parts.join(':').trim();
lines.forEach(line => {
const parts = line.trim().split(':');
const key = (parts.shift() || '').trim().toLowerCase();
const value = parts.join(':').trim();
if (!key) {
return;
}
headers[key] = value;

@@ -39,9 +38,9 @@ });

*/
function upperCaseWords (str) {
return (str || '')
.toLowerCase()
function upperCaseWords (str = '') {
return str.toLowerCase()
.trim()
.replace(/^(MIME|POT?(?=-)|[a-z])|-[a-z]/gi, function (str) {
return str.toUpperCase();
});
.replace(
/^(MIME|POT?(?=-)|[a-z])|-[a-z]/gi,
str => str.toUpperCase()
);
}

@@ -55,12 +54,14 @@

*/
function generateHeader (header) {
var lines = [];
function generateHeader (header = {}) {
const keys = Object.keys(header)
.filter(key => !!key);
Object.keys(header || {}).forEach(function (key) {
if (key) {
lines.push(upperCaseWords(key) + ': ' + (header[key] || '').trim());
}
});
if (!keys.length) {
return '';
}
return lines.join('\n') + (lines.length ? '\n' : '');
return keys.map(key =>
`${upperCaseWords(key)}: ${(header[key] || '').trim()}`
)
.join('\n') + '\n';
}

@@ -74,4 +75,5 @@

*/
function formatCharset (charset, defaultCharset) {
return (charset || 'iso-8859-1').toString().toLowerCase()
function formatCharset (charset = 'iso-8859-1', defaultCharset = 'iso-8859-1') {
return charset.toString()
.toLowerCase()
.replace(/^utf[-_]?(\d+)$/, 'utf-$1')

@@ -81,3 +83,3 @@ .replace(/^win(?:dows)?[-_]?(\d+)$/, 'windows-$1')

.replace(/^(us[-_]?)?ascii$/, 'ascii')
.replace(/^charset$/, defaultCharset || 'iso-8859-1')
.replace(/^charset$/, defaultCharset)
.trim();

@@ -93,11 +95,9 @@ }

*/
function foldLine (str, maxLen) {
maxLen = maxLen || 76;
function foldLine (str, maxLen = 76) {
const lines = [];
const len = str.length;
let curLine = '';
let pos = 0;
let match;
var lines = [];
var curLine = '';
var pos = 0;
var len = str.length;
var match;
while (pos < len) {

@@ -104,0 +104,0 @@ curLine = str.substr(pos, maxLen);

{
"name": "gettext-parser",
"description": "Parse and compile gettext po and mo files to/from json, nothing more, nothing less",
"version": "3.1.0",
"version": "3.1.1",
"author": "Andris Reinman",

@@ -26,3 +26,3 @@ "contributors": [

"encoding": "^0.1.12",
"readable-stream": "^3.0.6",
"readable-stream": "^3.2.0",
"safe-buffer": "^5.1.2"

@@ -32,9 +32,9 @@ },

"chai": "^4.2.0",
"eslint": "^5.9.0",
"eslint": "^5.15.1",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"mocha": "^5.2.0"
"mocha": "^6.0.2"
},

@@ -41,0 +41,0 @@ "keywords": [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc