Skip to content

Commit

Permalink
refactor: migrate typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Nov 1, 2022
2 parents 8c29937 + 94e8d0c commit e9d8ebc
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
coverage/
tmp/
tmp/
dist/
8 changes: 6 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"extends": "hexo",
"root": true
"root": true,
"extends": "hexo/ts.js",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2020
}
}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ node_modules/
tmp/
*.log
.idea/
coverage
coverage
dist
package-lock.json
27 changes: 16 additions & 11 deletions lib/i18n.js → lib/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use strict';
import { vsprintf } from 'sprintf-js';

const { vsprintf } = require('sprintf-js');
interface Options {
languages?: string[];
}

class i18n {
constructor(options = {}) {
data: object;
languages: string[];

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

Expand All @@ -12,7 +17,7 @@ class i18n {
}
}

get(languages) {
get(languages?: string[] | string) {
const { data } = this;
const result = {};

Expand All @@ -39,7 +44,7 @@ class i18n {
return result;
}

set(lang, data) {
set(lang: string, data: object) {
if (typeof lang !== 'string') throw new TypeError('lang must be a string!');
if (typeof data !== 'object') throw new TypeError('data is required!');

Expand All @@ -48,7 +53,7 @@ class i18n {
return this;
}

remove(lang) {
remove(lang: string) {
if (typeof lang !== 'string') throw new TypeError('lang must be a string!');

delete this.data[lang];
Expand All @@ -60,10 +65,10 @@ class i18n {
return Object.keys(this.data);
}

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

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

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

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

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

const number = args.length ? +args[0] : 0;
Expand Down Expand Up @@ -110,4 +115,4 @@ function flattenObject(data, obj = {}, parent = '') {
return obj;
}

module.exports = i18n;
export = i18n;
23 changes: 17 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@
"name": "hexo-i18n",
"version": "1.0.0",
"description": "i18n module for Hexo.",
"main": "lib/i18n.js",
"main": "dist/i18n.js",
"scripts": {
"prepublish ": "npm run clean && npm run build",
"build": "tsc -b",
"clean": "tsc -b --clean",
"eslint": "eslint .",
"test": "mocha test/index.js",
"pretest": "npm run clean && npm run build",
"test": "mocha test/index.js --require ts-node/register",
"test-cov": "c8 --reporter=lcovonly npm run test"
},
"directories": {
"lib": "./lib"
},
"files": [
"lib/"
"dist/**"
],
"types": "./dist/i18n.d.ts",
"engines": {
"node": ">=14"
},
Expand All @@ -30,10 +35,16 @@
"sprintf-js": "^1.1.2"
},
"devDependencies": {
"@types/node": "^18.11.8",
"@types/sprintf-js": "^1.1.2",
"@typescript-eslint/eslint-plugin": "^5.41.0",
"@typescript-eslint/parser": "^5.41.0",
"c8": "^7.12.0",
"chai": "^4.3.4",
"eslint": "^8.6.0",
"chai": "^4.3.6",
"eslint": "^8.26.0",
"eslint-config-hexo": "^5.0.0",
"mocha": "^10.1.0"
"mocha": "^10.1.0",
"ts-node": "^10.9.1",
"typescript": "^4.8.4"
}
}
5 changes: 4 additions & 1 deletion test/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "hexo/test"
"extends": "hexo/test",
"rules": {
"@typescript-eslint/no-var-requires": 0
}
}
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const should = require('chai').should();

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

const i18n = new Ctor({
languages: ['zh-TW', 'en']
Expand Down
19 changes: 19 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"sourceMap": true,
"outDir": "dist",
"declaration": true,
"esModuleInterop": true,
"types": [
"node"
]
},
"include": [
"lib/i18n.ts"
],
"exclude": [
"node_modules"
]
}

0 comments on commit e9d8ebc

Please sign in to comment.