Skip to content

Commit

Permalink
switch to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
smhg committed Feb 9, 2024
1 parent e9ab952 commit a26053d
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 162 deletions.
27 changes: 0 additions & 27 deletions .eslintrc.js

This file was deleted.

14 changes: 14 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"env": {
"es2021": true,
"node": true
},
"extends": "standard",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
"semi": ["error", "always"]
}
}
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 16
- 18
- 20
os:
- ubuntu-latest
- windows-latest
Expand Down
19 changes: 11 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const { parse, stream } = require('./lib/poparser');
import * as poParser from './lib/poparser.js';
import poCompiler from './lib/pocompiler.js';
import moParser from './lib/moparser.js';
import moCompiler from './lib/mocompiler.js';

module.exports.po = {
parse,
createParseStream: stream,
compile: require('./lib/pocompiler')
export const po = {
parse: poParser.parse,
createParseStream: poParser.stream,
compile: poCompiler
};

module.exports.mo = {
parse: require('./lib/moparser'),
compile: require('./lib/mocompiler')
export const mo = {
parse: moParser,
compile: moCompiler
};
22 changes: 11 additions & 11 deletions lib/mocompiler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Buffer } = require('safe-buffer');
const encoding = require('encoding');
const sharedFuncs = require('./shared');
const contentType = require('content-type');
import { Buffer } from 'safe-buffer';
import encoding from 'encoding';
import { HEADERS, formatCharset, generateHeader, compareMsgid } from './shared.js';
import contentType from 'content-type';

/**
* Exposes general compiler function. Takes a translation
Expand All @@ -10,7 +10,7 @@ const contentType = require('content-type');
* @param {Object} table Translation object
* @return {Buffer} Compiled binary MO object
*/
module.exports = function (table) {
export default function (table) {
const compiler = new Compiler(table);

return compiler.compile();
Expand All @@ -30,10 +30,10 @@ function Compiler (table = {}) {
headers = Object.keys(headers).reduce((result, key) => {
const lowerKey = key.toLowerCase();

if (sharedFuncs.HEADERS.has(lowerKey)) {
if (HEADERS.has(lowerKey)) {
// POT-Creation-Date is removed in MO (see https://savannah.gnu.org/bugs/?49654)
if (lowerKey !== 'pot-creation-date') {
result[sharedFuncs.HEADERS.get(lowerKey)] = headers[key];
result[HEADERS.get(lowerKey)] = headers[key];
}
} else {
result[key] = headers[key];
Expand Down Expand Up @@ -83,11 +83,11 @@ Compiler.prototype.MAGIC = 0x950412de;
Compiler.prototype._handleCharset = function () {
const ct = contentType.parse(this._table.headers['Content-Type'] || 'text/plain');

const charset = sharedFuncs.formatCharset(this._table.charset || ct.parameters.charset || 'utf-8');
const charset = formatCharset(this._table.charset || ct.parameters.charset || 'utf-8');

// clean up content-type charset independently using fallback if missing
if (ct.parameters.charset) {
ct.parameters.charset = sharedFuncs.formatCharset(ct.parameters.charset);
ct.parameters.charset = formatCharset(ct.parameters.charset);
}

this._table.charset = charset;
Expand All @@ -105,7 +105,7 @@ Compiler.prototype._generateList = function () {

list.push({
msgid: Buffer.alloc(0),
msgstr: encoding.convert(sharedFuncs.generateHeader(this._table.headers), this._table.charset)
msgstr: encoding.convert(generateHeader(this._table.headers), this._table.charset)
});

Object.keys(this._table.translations).forEach(msgctxt => {
Expand Down Expand Up @@ -245,7 +245,7 @@ Compiler.prototype.compile = function () {
const list = this._generateList();
const size = this._calculateSize(list);

list.sort(sharedFuncs.compareMsgid);
list.sort(compareMsgid);

return this._build(list, size);
};
10 changes: 5 additions & 5 deletions lib/moparser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const encoding = require('encoding');
const sharedFuncs = require('./shared');
import encoding from 'encoding';
import { formatCharset, parseHeader } from './shared.js';

/**
* Parses a binary MO object into translation table
Expand All @@ -8,7 +8,7 @@ const sharedFuncs = require('./shared');
* @param {String} [defaultCharset] Default charset to use
* @return {Object} Translation object
*/
module.exports = function (buffer, defaultCharset) {
export default function (buffer, defaultCharset) {
const parser = new Parser(buffer, defaultCharset);

return parser.parse();
Expand Down Expand Up @@ -122,13 +122,13 @@ Parser.prototype._handleCharset = function (headers) {
let match;

if ((match = headersStr.match(/[; ]charset\s*=\s*([\w-]+)/i))) {
this._charset = this._table.charset = sharedFuncs.formatCharset(match[1], this._charset);
this._charset = this._table.charset = formatCharset(match[1], this._charset);
}

headers = encoding.convert(headers, 'utf-8', this._charset)
.toString('utf8');

this._table.headers = sharedFuncs.parseHeader(headers);
this._table.headers = parseHeader(headers);
};

/**
Expand Down
24 changes: 12 additions & 12 deletions lib/pocompiler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Buffer } = require('safe-buffer');
const encoding = require('encoding');
const sharedFuncs = require('./shared');
const contentType = require('content-type');
import { Buffer } from 'safe-buffer';
import encoding from 'encoding';
import { HEADERS, foldLine, compareMsgid, formatCharset, generateHeader } from './shared.js';
import contentType from 'content-type';

/**
* Exposes general compiler function. Takes a translation
Expand All @@ -10,7 +10,7 @@ const contentType = require('content-type');
* @param {Object} table Translation object
* @return {Buffer} Compiled PO object
*/
module.exports = function (table, options) {
export default function (table, options) {
const compiler = new Compiler(table, options);

return compiler.compile();
Expand All @@ -33,8 +33,8 @@ function Compiler (table = {}, options = {}) {
headers = Object.keys(headers).reduce((result, key) => {
const lowerKey = key.toLowerCase();

if (sharedFuncs.HEADERS.has(lowerKey)) {
result[sharedFuncs.HEADERS.get(lowerKey)] = headers[key];
if (HEADERS.has(lowerKey)) {
result[HEADERS.get(lowerKey)] = headers[key];
} else {
result[key] = headers[key];
}
Expand Down Expand Up @@ -178,7 +178,7 @@ Compiler.prototype._addPOString = function (key = '', value = '', obsolete = fal
}

if (foldLength > 0) {
lines = sharedFuncs.foldLine(value, foldLength);
lines = foldLine(value, foldLength);
} else {
// split only on new lines
if (escapeCharacters) {
Expand All @@ -205,11 +205,11 @@ Compiler.prototype._addPOString = function (key = '', value = '', obsolete = fal
Compiler.prototype._handleCharset = function () {
const ct = contentType.parse(this._table.headers['Content-Type'] || 'text/plain');

const charset = sharedFuncs.formatCharset(this._table.charset || ct.parameters.charset || 'utf-8');
const charset = formatCharset(this._table.charset || ct.parameters.charset || 'utf-8');

// clean up content-type charset independently using fallback if missing
if (ct.parameters.charset) {
ct.parameters.charset = sharedFuncs.formatCharset(ct.parameters.charset);
ct.parameters.charset = formatCharset(ct.parameters.charset);
}

this._table.charset = charset;
Expand Down Expand Up @@ -249,7 +249,7 @@ Compiler.prototype._prepareSection = function (section) {
if (typeof sort === 'function') {
response = response.sort(sort);
} else {
response = response.sort(sharedFuncs.compareMsgid);
response = response.sort(compareMsgid);
}
}

Expand Down Expand Up @@ -278,7 +278,7 @@ Compiler.prototype.compile = function () {
const { eol } = this._options;

response.unshift(this._drawBlock(headerBlock, {
msgstr: sharedFuncs.generateHeader(this._table.headers)
msgstr: generateHeader(this._table.headers)
}));

if (this._table.charset === 'utf-8' || this._table.charset === 'ascii') {
Expand Down
21 changes: 9 additions & 12 deletions lib/poparser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const encoding = require('encoding');
const sharedFuncs = require('./shared');
const Transform = require('readable-stream').Transform;
const util = require('util');

module.exports.parse = parse;
module.exports.stream = stream;
import encoding from 'encoding';
import { formatCharset, parseNPluralFromHeadersSafely, parseHeader } from './shared.js';
import { Transform } from 'readable-stream';
import util from 'util';

/**
* Parses a PO object into translation table
Expand All @@ -13,7 +10,7 @@ module.exports.stream = stream;
* @param {string | Buffer} input PO object
* @param {Options} [options] Optional options with defaultCharset and validation
*/
function parse (input, options = {}) {
export function parse (input, options = {}) {
const parser = new Parser(input, options);

return parser.parse();
Expand All @@ -26,7 +23,7 @@ function parse (input, options = {}) {
* @param {Options} [options] Optional options with defaultCharset and validation
* @param {import('readable-stream').TransformOptions} [transformOptions] Optional stream options
*/
function stream (options = {}, transformOptions = {}) {
export function stream (options = {}, transformOptions = {}) {
return new PoParserTransform(options, transformOptions);
};

Expand Down Expand Up @@ -85,7 +82,7 @@ Parser.prototype._handleCharset = function (buf = '') {
}

if ((match = headers.match(/[; ]charset\s*=\s*([\w-]+)(?:[\s;]|\\n)*"\s*$/mi))) {
this._charset = sharedFuncs.formatCharset(match[1], this._charset);
this._charset = formatCharset(match[1], this._charset);
}

if (this._charset === 'utf-8') {
Expand Down Expand Up @@ -486,8 +483,8 @@ Parser.prototype._normalize = function (tokens) {
}

if (!table.headers && !msgctxt && !tokens[i].msgid) {
table.headers = sharedFuncs.parseHeader(tokens[i].msgstr[0]);
nplurals = sharedFuncs.parseNPluralFromHeadersSafely(table.headers, nplurals);
table.headers = parseHeader(tokens[i].msgstr[0]);
nplurals = parseNPluralFromHeadersSafely(table.headers, nplurals);
}

this._validateToken(tokens[i], table.translations, msgctxt, nplurals);
Expand Down
23 changes: 7 additions & 16 deletions lib/shared.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
module.exports.parseHeader = parseHeader;
module.exports.parseNPluralFromHeadersSafely = parseNPluralFromHeadersSafely;
module.exports.generateHeader = generateHeader;
module.exports.formatCharset = formatCharset;
module.exports.foldLine = foldLine;
module.exports.compareMsgid = compareMsgid;

// see https://www.gnu.org/software/gettext/manual/html_node/Header-Entry.html
const PLURAL_FORMS = 'Plural-Forms';
const HEADERS = new Map([
export const HEADERS = new Map([
['project-id-version', 'Project-Id-Version'],
['report-msgid-bugs-to', 'Report-Msgid-Bugs-To'],
['pot-creation-date', 'POT-Creation-Date'],
Expand All @@ -20,8 +13,6 @@ const HEADERS = new Map([
['plural-forms', PLURAL_FORMS]
]);

module.exports.HEADERS = HEADERS;

const PLURAL_FORM_HEADER_NPLURALS_REGEX = /nplurals\s*=\s*(?<nplurals>\d+)/;

/**
Expand All @@ -30,7 +21,7 @@ const PLURAL_FORM_HEADER_NPLURALS_REGEX = /nplurals\s*=\s*(?<nplurals>\d+)/;
* @param {String} str Header string
* @return {Object} An object of key-value pairs
*/
function parseHeader (str = '') {
export function parseHeader (str = '') {
return str.split('\n')
.reduce((headers, line) => {
const parts = line.split(':');
Expand All @@ -54,7 +45,7 @@ function parseHeader (str = '') {
* @param {Object} [headers = {}] An object with parsed headers
* @returns {number} Parsed result
*/
function parseNPluralFromHeadersSafely (headers = {}, fallback = 1) {
export function parseNPluralFromHeadersSafely (headers = {}, fallback = 1) {
const pluralForms = headers[PLURAL_FORMS];

if (!pluralForms) {
Expand All @@ -74,7 +65,7 @@ function parseNPluralFromHeadersSafely (headers = {}, fallback = 1) {
* @param {Object} header Object of key value pairs
* @return {String} Header string
*/
function generateHeader (header = {}) {
export function generateHeader (header = {}) {
const keys = Object.keys(header)
.filter(key => !!key);

Expand All @@ -94,7 +85,7 @@ function generateHeader (header = {}) {
* @param {String} charset Charset name
* @return {String} Normalized charset name
*/
function formatCharset (charset = 'iso-8859-1', defaultCharset = 'iso-8859-1') {
export function formatCharset (charset = 'iso-8859-1', defaultCharset = 'iso-8859-1') {
return charset.toString()
.toLowerCase()
.replace(/^utf[-_]?(\d+)$/, 'utf-$1')
Expand All @@ -112,7 +103,7 @@ function formatCharset (charset = 'iso-8859-1', defaultCharset = 'iso-8859-1') {
* @param {Number} [maxLen=76] Maximum allowed length for folded lines
* @return {Array} An array of lines
*/
function foldLine (str, maxLen = 76) {
export function foldLine (str, maxLen = 76) {
const lines = [];
const len = str.length;
let curLine = '';
Expand Down Expand Up @@ -157,7 +148,7 @@ function foldLine (str, maxLen = 76) {
* @param {Object} object with msgid next
* @returns {number} comparator index
*/
function compareMsgid ({ msgid: left }, { msgid: right }) {
export function compareMsgid ({ msgid: left }, { msgid: right }) {
if (left < right) {
return -1;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"type": "git",
"url": "http://github.com/smhg/gettext-parser.git"
},
"type": "module",
"engines": {
"node": ">=18"
},
Expand Down
5 changes: 0 additions & 5 deletions test/.eslintrc.js

This file was deleted.

Loading

0 comments on commit a26053d

Please sign in to comment.