Skip to content

Commit

Permalink
refactor: migrate typescript (#126)
Browse files Browse the repository at this point in the history
* refactor: migrate typescript

* @ts-expect-error
  • Loading branch information
D-Sketon committed Apr 6, 2024
1 parent ac9e4e1 commit 97c1971
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
14 changes: 7 additions & 7 deletions lib/i18n.ts
@@ -1,16 +1,16 @@
import { vsprintf } from 'sprintf-js';

interface Options {
languages?: string[];
languages?: string[] | string;
}

class i18n {
data: object;
data: any;

Check warning on line 8 in lib/i18n.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
languages: string[];

constructor(options: Options = {}) {
this.data = {};
this.languages = options.languages || ['default'];
this.languages = options.languages as any || ['default'];

Check warning on line 13 in lib/i18n.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type

if (!Array.isArray(this.languages)) {
this.languages = [this.languages];
Expand Down Expand Up @@ -65,10 +65,10 @@ class i18n {
return Object.keys(this.data);
}

__(lang?: string[]) {
__(lang?: string | string[]) {
const data = this.get(lang);

return (key: string, ...args) => {
return (key?: string, ...args) => {
if (!key) return '';

const str = data[key] || key;
Expand All @@ -77,10 +77,10 @@ class i18n {
};
}

_p(lang?: string[]) {
_p(lang?: string | string[]) {
const data = this.get(lang);

return (key: string, ...args) => {
return (key?: string, ...args) => {
if (!key) return '';

const number = args.length ? +args[0] : 0;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -9,7 +9,7 @@
"clean": "tsc -b --clean",
"eslint": "eslint .",
"pretest": "npm run clean && npm run build",
"test": "mocha test/index.js --require ts-node/register",
"test": "mocha test/index.ts --require ts-node/register",
"test-cov": "c8 --reporter=lcovonly npm run test"
},
"files": [
Expand All @@ -32,6 +32,8 @@
"sprintf-js": "^1.1.2"
},
"devDependencies": {
"@types/chai": "^4.3.12",
"@types/mocha": "^10.0.6",
"@types/node": "^18.11.8",
"@types/sprintf-js": "^1.1.2",
"c8": "^9.1.0",
Expand Down
5 changes: 3 additions & 2 deletions test/.eslintrc
@@ -1,7 +1,8 @@
{
"extends": "hexo/test",
"extends": "hexo/ts-test",
"rules": {
"@typescript-eslint/no-var-requires": 0,
"node/no-missing-require": 0
"node/no-missing-require": 0,
"@typescript-eslint/ban-ts-comment": 0
}
}
11 changes: 6 additions & 5 deletions test/index.js → test/index.ts
@@ -1,10 +1,8 @@
'use strict';

const should = require('chai').should();
import chai from 'chai';
import Ctor from '../lib/i18n';
const should = chai.should();

describe('i18n', () => {
const Ctor = require('../dist/i18n');

const i18n = new Ctor({
languages: ['zh-TW', 'en']
});
Expand Down Expand Up @@ -78,6 +76,7 @@ describe('i18n', () => {

it('set() - lang must be a string', () => {
try {
// @ts-expect-error
i18n.set();
} catch (err) {
err.should.have.property('message', 'lang must be a string!');
Expand All @@ -86,6 +85,7 @@ describe('i18n', () => {

it('set() - data is required', () => {
try {
// @ts-expect-error
i18n.set('en');
} catch (err) {
err.should.have.property('message', 'data is required!');
Expand Down Expand Up @@ -131,6 +131,7 @@ describe('i18n', () => {

it('remove() - lang must be a string', () => {
try {
// @ts-expect-error
i18n.remove();
} catch (err) {
err.should.have.property('message', 'lang must be a string!');
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Expand Up @@ -7,7 +7,8 @@
"declaration": true,
"esModuleInterop": true,
"types": [
"node"
"node",
"mocha"
]
},
"include": [
Expand Down

0 comments on commit 97c1971

Please sign in to comment.