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 all 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
/@types
npm-debug.log
.DS_Store
54 changes: 54 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Transform } from "readable-stream";

import {Buffer} from "safe-buffer";

export declare module 'encoding' {
export function convert(buf: Buffer, toCharset: string, fromCharset: 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 po {
parse: (buffer: Buffer | string, defaultCharset?: string) => GetTextTranslations;
compile: (table: GetTextTranslations, options?: parserOptions) => Buffer;
createParseStream: (options?: parserOptions, transformOptions?: import('readable-stream').TransformOptions) => Transform;
}

export interface mo {
parse: (buffer: Buffer | string, defaultCharset?: string) => GetTextTranslations;
compile: (table: GetTextTranslations, options?: parserOptions) => Buffer;
}

export * from "./@types";

export default { po, mo } as { po: po, mo: mo };
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@ import poCompiler from './lib/pocompiler.js';
import moParser from './lib/moparser.js';
import moCompiler from './lib/mocompiler.js';

/**
* Translation parser and compiler for PO files
* @see https://www.gnu.org/software/gettext/manual/html_node/PO.html
*
* @type {import("./index.d.ts").po} 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.d.ts").mo} mo
*/
export const mo = {
parse: moParser,
compile: moCompiler
Expand Down
11 changes: 6 additions & 5 deletions lib/mocompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ 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.d.ts').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.d.ts').GetTextTranslations} table Translation table as defined in the README
* @return {import('./index.d.ts').Compiler} Compiler
*/
function Compiler (table = {}) {
this._table = table;
Expand Down Expand Up @@ -148,7 +149,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.d.ts').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 +184,7 @@ Compiler.prototype._calculateSize = function (list) {
/**
* Generates the binary MO object from the translation list
*
* @param {Array} list translation list
* @param {import('./index.d.ts').GetTextTranslations} list translation list
* @param {Object} size Byte size information
* @return {Buffer} Compiled MO object
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/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
2 changes: 1 addition & 1 deletion lib/pocompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function (table, options) {
const compiler = new Compiler(table, options);

return compiler.compile();
};
}

/**
* Creates a PO compiler object.
Expand Down
8 changes: 4 additions & 4 deletions lib/poparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
* @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 @@ -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 Down
6 changes: 3 additions & 3 deletions lib/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export function parseHeader (str = '') {
* @param {Object} [headers = {}] An object with parsed headers
* @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;
Expand Down Expand Up @@ -101,7 +101,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
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
"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"
"postversion": "git push && git push --tags",
"prepublish": "tsc && npm run lint && npm run test"
},
"main": "./index.js",
"types": "./index.d.ts",
"license": "MIT",
"dependencies": {
"content-type": "^1.0.5",
Expand All @@ -33,13 +35,20 @@
"safe-buffer": "^5.2.1"
},
"devDependencies": {
"@types/chai": "latest",
"@types/content-type": "^1.1.8",
"@types/mocha": "latest",
"@types/readable-stream": "^4.0.11",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"@typescript-eslint/parser": "^6.14.0",
"chai": "^5.0.3",
"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.3.0",
"typescript": "^5.4.4"
erikyo marked this conversation as resolved.
Show resolved Hide resolved
},
"keywords": [
"i18n",
Expand Down
36 changes: 36 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"compilerOptions": {
"noImplicitAny": true,
"removeComments": false,
"module": "Node16",
"moduleResolution": "Node16",
"target": "ES2015",

// Strict mode
"strict": true,

// Allow javascript files
"allowJs": true,

// Check js files for errors
"checkJs": false,

// Output d.ts files to @types
"outDir": "@types",

// Generate d.ts files
"declaration": true,

// This compiler run should
// only output d.ts files
"emitDeclarationOnly": true,
// Minify
"pretty": false,
// Skip lib check when compiling
"skipLibCheck": true
},
"include": [
"lib/**/*.js",
"index.d.ts"
]
}