Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Typescript for type checking (but still with js files) #81

Merged
merged 9 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"es2021": true,
"node": true
},
"extends": "standard",
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules
lib
npm-debug.log
.DS_Store
17 changes: 0 additions & 17 deletions index.js

This file was deleted.

24 changes: 20 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@
"engines": {
"node": ">=18"
},
"files": [
erikyo marked this conversation as resolved.
Show resolved Hide resolved
"lib",
"test",
"*.json",
"*.md",
"license.txt"
],
"scripts": {
"lint": "eslint lib/*.js test/*.js index.js",
"start": "tsc --watch",
"build": "tsc",
"lint": "eslint src/*.js test/*.js",
"test-generate-mo": "msgfmt test/fixtures/latin13.po -o test/fixtures/latin13.mo & msgfmt test/fixtures/utf8.po -o test/fixtures/utf8.mo & msgfmt test/fixtures/obsolete.po -o test/fixtures/obsolete.mo",
"test": "mocha",
"preversion": "npm run lint && npm test",
"postversion": "git push && git push --tags"
},
"main": "./index.js",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"license": "MIT",
"dependencies": {
"content-type": "^1.0.5",
Expand All @@ -33,13 +43,19 @@
"safe-buffer": "^5.2.1"
},
"devDependencies": {
"chai": "^5.0.3",
"@types/chai": "latest",
"@types/content-type": "^1.1.8",
"@types/mocha": "latest",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"@typescript-eslint/parser": "^6.14.0",
"chai": "^5.1.0",
"eslint": "^8.56.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-n": "^16.6.2",
"eslint-plugin-promise": "^6.1.1",
"mocha": "^10.3.0"
"mocha": "^10.4.0",
"typescript": "^5.4.4"
erikyo marked this conversation as resolved.
Show resolved Hide resolved
},
"keywords": [
"i18n",
Expand Down
49 changes: 49 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Transform } from "readable-stream";
import {Buffer} from "safe-buffer";

export declare module 'encoding' {
export function convert(buf: Buffer, charset: string): Buffer;
}

export interface Compiler {
_table: GetTextTranslations;
compile(): Buffer;
}

export interface GetTextComment {
translator?: string;
reference?: string;
extracted?: string;
flag?: string;
previous?: string;
}

export interface GetTextTranslation {
msgctxt?: string;
msgid: string;
msgid_plural?: string[];
msgstr: string[];
comments?: GetTextComment;
}

export interface GetTextTranslations {
charset: string;
headers: { [headerName: string]: string };
translations: { [msgctxt: string]: { [msgId: string]: GetTextTranslation } };
}

export interface parserOptions {
defaultCharset?: string;
validation?: boolean;
}

export interface PoParser {
parse: (buffer: Buffer | string, defaultCharset?: string) => GetTextTranslations;
compile: (table: GetTextTranslations, options?: parserOptions) => Buffer;
createParseStream: (options?: parserOptions, transformOptions?: import('readable-stream').TransformOptions) => Transform;
}

export interface MoParser {
parse: (buffer: Buffer | string, defaultCharset?: string) => GetTextTranslations;
compile: (table: GetTextTranslations, options?: parserOptions) => Buffer;
}
27 changes: 27 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as poParser from './poparser.js';
import poCompiler from './pocompiler.js';
import moParser from './moparser.js';
import moCompiler from './mocompiler.js';

/**
* Translation parser and compiler for PO files
* @see https://www.gnu.org/software/gettext/manual/html_node/PO.html
*
* @type {import("./index.js").PoParser} po
*/
export const po = {
parse: poParser.parse,
createParseStream: poParser.stream,
compile: poCompiler
};

/**
* Translation parser and compiler for PO files
* @see https://www.gnu.org/software/gettext/manual/html_node/MO.html
*
* @type {import("./index.js").MoParser} mo
*/
export const mo= {
parse: moParser,
compile: moCompiler
};
19 changes: 11 additions & 8 deletions lib/mocompiler.js → src/mocompiler.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import { Buffer } from 'safe-buffer';
import encoding from 'encoding';
import { HEADERS, formatCharset, generateHeader, compareMsgid } from './shared.js';
import { compareMsgid, formatCharset, generateHeader, HEADERS } from './shared.js';
import contentType from 'content-type';

/**
* Exposes general compiler function. Takes a translation
* object as a parameter and returns binary MO object
*
* @param {Object} table Translation object
* @param {import('./index.js').gettextTranslations} table Translation object
* @return {Buffer} Compiled binary MO object
*/
export default function (table) {
const compiler = new Compiler(table);

return compiler.compile();
};
}

/**
* Creates a MO compiler object.
*
* @constructor
* @param {Object} table Translation table as defined in the README
* @param {import('./index.js').gettextTranslations} table Translation table as defined in the README
* @return {import('./index.js').Compiler} Compiler
*/
function Compiler (table = {}) {
erikyo marked this conversation as resolved.
Show resolved Hide resolved
function Compiler (table ) {
this._table = table;

let { headers = {}, translations = {} } = this._table;
Expand Down Expand Up @@ -70,6 +71,8 @@ function Compiler (table = {}) {
this._writeFunc = 'writeUInt32LE';

this._handleCharset();

return this;
}

/**
Expand Down Expand Up @@ -148,7 +151,7 @@ Compiler.prototype._generateList = function () {
/**
* Calculate buffer size for the final binary object
*
* @param {Array} list An array of translation strings from _generateList
* @param {import('./index.js').GettextTranslations} list An array of translation strings from _generateList
* @return {Object} Size data of {msgid, msgstr, total}
*/
Compiler.prototype._calculateSize = function (list) {
Expand Down Expand Up @@ -183,7 +186,7 @@ Compiler.prototype._calculateSize = function (list) {
/**
* Generates the binary MO object from the translation list
*
* @param {Array} list translation list
* @param {import('./index.js').GettextTranslations} list translation list
* @param {Object} size Byte size information
* @return {Buffer} Compiled MO object
*/
Expand Down Expand Up @@ -237,7 +240,7 @@ Compiler.prototype._build = function (list, size) {
};

/**
* Compiles translation object into a binary MO object
* Compiles the translation object into a binary MO object
erikyo marked this conversation as resolved.
Show resolved Hide resolved
*
* @return {Buffer} Compiled MO object
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/moparser.js → src/moparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function (buffer, defaultCharset) {
const parser = new Parser(buffer, defaultCharset);

return parser.parse();
};
}

/**
* Creates a MO parser object.
Expand Down Expand Up @@ -135,7 +135,7 @@ Parser.prototype._handleCharset = function (headers) {
* Adds a translation to the translation object
*
* @param {String} msgid Original string
* @params {String} msgstr Translation for the original string
* @param {String} msgstr Translation for the original string
erikyo marked this conversation as resolved.
Show resolved Hide resolved
*/
Parser.prototype._addString = function (msgid, msgstr) {
const translation = {};
Expand Down
10 changes: 5 additions & 5 deletions lib/pocompiler.js → src/pocompiler.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Buffer } from 'safe-buffer';
import encoding from 'encoding';
import { HEADERS, foldLine, compareMsgid, formatCharset, generateHeader } from './shared.js';
import { compareMsgid, foldLine, formatCharset, generateHeader, HEADERS } from './shared.js';
import contentType from 'content-type';
import encoding from 'encoding';

/**
* Exposes general compiler function. Takes a translation
* object as a parameter and returns PO object
*
* @param {Object} table Translation object
* @param options
* @return {Buffer} Compiled PO object
*/
export default function (table, options) {
const compiler = new Compiler(table, options);

return compiler.compile();
};
}

/**
* Creates a PO compiler object.
Expand Down Expand Up @@ -72,7 +72,7 @@ function Compiler (table = {}, options = {}) {
* @param {Object} comments A comments object
* @return {String} A comment string for the PO file
*/
Compiler.prototype._drawComments = function (comments) {
Compiler.prototype._drawComments = function (comments){
const lines = [];
const types = [{
key: 'translator',
Expand Down
18 changes: 10 additions & 8 deletions lib/poparser.js → src/poparser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import encoding from 'encoding';
import { formatCharset, parseNPluralFromHeadersSafely, parseHeader } from './shared.js';
import { formatCharset, parseHeader, parseNPluralFromHeadersSafely } from './shared.js';
import { Transform } from 'readable-stream';
import util from 'util';

Expand All @@ -14,18 +14,18 @@ export function parse (input, options = {}) {
const parser = new Parser(input, options);

return parser.parse();
};
}

/**
* Parses a PO stream, emits translation table in object mode
*
* @typedef {{ defaultCharset: strubg, validation: boolean }} Options
* @typedef {{ defaultCharset: string, validation: boolean }} Options
smhg marked this conversation as resolved.
Show resolved Hide resolved
* @param {Options} [options] Optional options with defaultCharset and validation
* @param {import('readable-stream').TransformOptions} [transformOptions] Optional stream options
*/
export function stream (options = {}, transformOptions = {}) {
return new PoParserTransform(options, transformOptions);
};
}

/**
* Creates a PO parser object. If PO object is a string,
Expand Down Expand Up @@ -68,7 +68,7 @@ Parser.prototype.parse = function () {
/**
* Detects charset for PO strings from the header
*
* @param {Buffer} headers Header value
* @param buf PO string buffer to be parsed
*/
Parser.prototype._handleCharset = function (buf = '') {
const str = buf.toString();
Expand All @@ -77,8 +77,8 @@ Parser.prototype._handleCharset = function (buf = '') {
let match;

if ((pos = str.search(/^\s*msgid/im)) >= 0) {
pos = pos + str.substr(pos + 5).search(/^\s*(msgid|msgctxt)/im);
headers = str.substr(0, pos >= 0 ? pos + 5 : str.length);
pos = pos + str.substring(pos + 5).search(/^\s*(msgid|msgctxt)/im);
headers = str.substring(0, pos >= 0 ? pos + 5 : str.length);
smhg marked this conversation as resolved.
Show resolved Hide resolved
}

if ((match = headers.match(/[; ]charset\s*=\s*([\w-]+)(?:[\s;]|\\n)*"\s*$/mi))) {
Expand Down Expand Up @@ -515,7 +515,7 @@ Parser.prototype._finalize = function (tokens) {
/**
* Creates a transform stream for parsing PO input
*
* @typedef {{ defaultCharset: strubg, validation: boolean }} Options
* @typedef {{ defaultCharset: string, validation: boolean }} Options
* @constructor
* @param {Options} options Optional options with defaultCharset and validation
* @param {import('readable-stream').TransformOptions} transformOptions Optional stream options
Expand All @@ -533,6 +533,8 @@ function PoParserTransform (options, transformOptions) {
Transform.call(this, transformOptions);
this._writableState.objectMode = false;
this._readableState.objectMode = true;

return this;
}
util.inherits(PoParserTransform, Transform);

Expand Down
10 changes: 7 additions & 3 deletions lib/shared.js → src/shared.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// see https://www.gnu.org/software/gettext/manual/html_node/Header-Entry.html

const PLURAL_FORMS = 'Plural-Forms';

export const HEADERS = new Map([
['project-id-version', 'Project-Id-Version'],
['report-msgid-bugs-to', 'Report-Msgid-Bugs-To'],
Expand Down Expand Up @@ -43,10 +45,11 @@ export function parseHeader (str = '') {
* Attempts to safely parse 'nplurals" value from "Plural-Forms" header
*
* @param {Object} [headers = {}] An object with parsed headers
* @param fallback {Number} Fallback value
* @returns {number} Parsed result
*/
export function parseNPluralFromHeadersSafely (headers = {}, fallback = 1) {
const pluralForms = headers[PLURAL_FORMS];
export function parseNPluralFromHeadersSafely (headers, fallback = 1) {
const pluralForms = headers ? headers[PLURAL_FORMS] : false;

if (!pluralForms) {
return fallback;
erikyo marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -83,6 +86,7 @@ export function generateHeader (header = {}) {
* Normalizes charset name. Converts utf8 to utf-8, WIN1257 to windows-1257 etc.
*
* @param {String} charset Charset name
* @param defaultCharset Default charset (default: 'iso-8859-1')
* @return {String} Normalized charset name
*/
export function formatCharset (charset = 'iso-8859-1', defaultCharset = 'iso-8859-1') {
Expand All @@ -101,7 +105,7 @@ export function formatCharset (charset = 'iso-8859-1', defaultCharset = 'iso-885
*
* @param {String} str PO formatted string to be folded
* @param {Number} [maxLen=76] Maximum allowed length for folded lines
* @return {Array} An array of lines
* @return {string[]} An array of lines
*/
export function foldLine (str, maxLen = 76) {
const lines = [];
Expand Down
2 changes: 1 addition & 1 deletion test/mo-compiler-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as chai from 'chai';
import { promisify } from 'util';
import path from 'path';
import { mo } from '../index.js';
import { mo } from '../src/index.js';
erikyo marked this conversation as resolved.
Show resolved Hide resolved
import { readFile as fsReadFile } from 'fs';
import { fileURLToPath } from 'url';

Expand Down
Loading