diff --git a/lib/i18n.ts b/lib/i18n.ts index e2bc6d3..c595e76 100644 --- a/lib/i18n.ts +++ b/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; languages: string[]; constructor(options: Options = {}) { this.data = {}; - this.languages = options.languages || ['default']; + this.languages = options.languages as any || ['default']; if (!Array.isArray(this.languages)) { this.languages = [this.languages]; @@ -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; @@ -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; diff --git a/package.json b/package.json index 09c2071..c8f7dfd 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -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", diff --git a/test/.eslintrc b/test/.eslintrc index a938940..7fe2934 100644 --- a/test/.eslintrc +++ b/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 } } \ No newline at end of file diff --git a/test/index.js b/test/index.ts similarity index 95% rename from test/index.js rename to test/index.ts index 8cfe097..173749f 100644 --- a/test/index.js +++ b/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'] }); @@ -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!'); @@ -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!'); @@ -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!'); diff --git a/tsconfig.json b/tsconfig.json index 4db8461..dfac976 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,7 +7,8 @@ "declaration": true, "esModuleInterop": true, "types": [ - "node" + "node", + "mocha" ] }, "include": [