Skip to content

Commit 5363080

Browse files
authored
Add linter and bundler (#8)
* Add bundler and linter * Update opencc-data to 1.0.5 * Remove old speed test * Add linter to check * Update README * Update documentation
1 parent 48f2195 commit 5363080

File tree

15 files changed

+1969
-396
lines changed

15 files changed

+1969
-396
lines changed

.eslintrc.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
es2021: true,
5+
node: true,
6+
},
7+
extends: [
8+
'airbnb-base',
9+
],
10+
parserOptions: {
11+
ecmaVersion: 12,
12+
sourceType: 'module',
13+
},
14+
rules: {
15+
'no-restricted-syntax': ['error', 'ForInStatement', 'LabeledStatement', 'WithStatement'],
16+
},
17+
};

.github/workflows/npm-publish.yml

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
1-
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2-
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3-
41
name: Node.js Package
52

63
on:
74
release:
85
types: [created]
96

107
jobs:
11-
build:
12-
runs-on: ubuntu-latest
13-
steps:
14-
- uses: actions/checkout@v2
15-
- uses: actions/setup-node@v1
16-
with:
17-
node-version: 14
18-
- run: npm ci
19-
- run: npm test
20-
218
publish-npm:
22-
needs: build
239
runs-on: ubuntu-latest
2410
steps:
25-
- uses: actions/checkout@v2
26-
- uses: actions/setup-node@v1
27-
with:
28-
node-version: 14
29-
registry-url: https://registry.npmjs.org/
30-
- run: npm ci
31-
- run: npm publish
32-
env:
33-
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
11+
- uses: actions/checkout@v2
12+
- uses: actions/setup-node@v1
13+
with:
14+
node-version: 14
15+
registry-url: https://registry.npmjs.org/
16+
- name: Install bundler
17+
run: npm install -g rollup
18+
- name: Install dependencies
19+
run: npm ci
20+
- name: Bundle
21+
run: npm run bundle
22+
- name: Publish
23+
run: npm publish
24+
env:
25+
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

.github/workflows/test.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,17 @@ jobs:
2020
- uses: actions/setup-node@v1
2121
with:
2222
node-version: 14.x
23-
- run: npm ci
23+
- name: Install bundler
24+
run: npm install -g rollup
25+
- name: Install dependencies
26+
run: npm ci
27+
- name: Bundle
28+
run: npm run bundle
2429
- name: Test
2530
run: npm test
2631
- name: Speed Test
2732
run: |
2833
test/prepare.sh
29-
node test/speed.js
34+
npm run speedtest
35+
- name: Lint
36+
run: npm run lint

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ node_modules
22

33
/test/神雕侠侣.txt
44
/test/天龙八部.txt
5+
6+
/bundle.js

README-en.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ Pure JavaScript implementation of OpenCC
44

55
## Import
66

7-
In HTML file:
7+
In HTML:
88

99
```html
10-
<script src="https://cdn.jsdelivr.net/npm/opencc-js@0.3.6"></script>
10+
<script src="https://cdn.jsdelivr.net/npm/opencc-js@0.3.7"></script>
1111
```
1212

1313
Or in Node.js:
@@ -19,7 +19,6 @@ const OpenCC = require('opencc-js');
1919
## Usage
2020

2121
```javascript
22-
2322
OpenCC.Converter('hk', 'cn') // Traditional Chinese (Hong Kong) to Simplified Chinese
2423
.then(convert => console.log(convert('漢字,簡體字'))); // output: 汉字,简体字
2524
```
@@ -33,25 +32,31 @@ The first argument is the source type, the second argument is the destination ty
3332
- Simplified Chinese (Mainland China):`cn`
3433
- Japanese _Shinjitai_`jp`
3534

36-
Trad (Hong Kong, with Hong Kong phrases) is currently not supported.
35+
Traditional Chinese (Hong Kong, with Hong Kong phrases) is currently not supported.
3736

3837
## Custom Converter
3938

4039
```javascript
41-
const convert = OpenCC.CustomConverter({ '香蕉': '🍌️', '蘋果': '🍎️', '': '🍐️' });
42-
console.log(convert('香蕉蘋果梨')); // output: 🍌️🍎️🍐️
40+
const dict = {
41+
'香蕉': 'banana',
42+
'蘋果': 'apple',
43+
'': 'pear',
44+
};
45+
const convert = OpenCC.CustomConverter(dict);
46+
console.log(convert('香蕉 蘋果 梨'));
47+
// outputs: banana apple pear
4348
```
4449

4550
## DOM operation
4651

4752
```javascript
48-
(async () => {
49-
const convert = await OpenCC.Converter('hk', 'cn');
50-
const startNode = document.documentElement; // Convert the whole page
51-
const HTMLConvertHandler = OpenCC.HTMLConverter(convert, startNode, 'zh-HK', 'zh-CN'); // Convert all zh-HK to zh-CN
52-
HTMLConvertHandler.convert(); // Start conversion
53-
HTMLConvertHandler.restore(); // Restore
54-
})()
53+
((async () => {
54+
const convert = await OpenCC.Converter('hk', 'cn');
55+
const startNode = document.documentElement; // Convert the whole page
56+
const HTMLConvertHandler = OpenCC.HTMLConverter(convert, startNode, 'zh-HK', 'zh-CN'); // Convert all zh-HK to zh-CN
57+
HTMLConvertHandler.convert(); // Start conversion
58+
HTMLConvertHandler.restore(); // Restore
59+
})());
5560
```
5661

5762
The conversion is skipped if the class list of a node contains `ignore-opencc`. All child nodes of the node will not be converted.

README.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
在 HTML 中引入:
1010

1111
```html
12-
<script src="https://cdn.jsdelivr.net/npm/opencc-js@0.3.6"></script>
12+
<script src="https://cdn.jsdelivr.net/npm/opencc-js@0.3.7"></script>
1313
```
1414

1515
或在 Node.js 中引入:
@@ -39,20 +39,26 @@ OpenCC.Converter('hk', 'cn') // 香港繁體轉簡體
3939
## 自訂轉換器
4040

4141
```javascript
42-
const convert = OpenCC.CustomConverter({ '香蕉': '🍌️', '蘋果': '🍎️', '': '🍐️' });
43-
console.log(convert('香蕉蘋果梨')); // output: 🍌️🍎️🍐️
42+
const dict = {
43+
'香蕉': 'banana',
44+
'蘋果': 'apple',
45+
'': 'pear',
46+
};
47+
const convert = OpenCC.CustomConverter(dict);
48+
console.log(convert('香蕉 蘋果 梨'));
49+
// outputs: banana apple pear
4450
```
4551

4652
## DOM 操作
4753

4854
```javascript
49-
(async () => {
50-
const convert = await OpenCC.Converter('hk', 'cn');
51-
const startNode = document.documentElement; // 轉換整個頁面
52-
const HTMLConvertHandler = OpenCC.HTMLConverter(convert, startNode, 'zh-HK', 'zh-CN'); // 將所有 zh-HK 標籤轉為 zh-CN 標籤
53-
HTMLConvertHandler.convert(); // 開始轉換
54-
HTMLConvertHandler.restore(); // 回到原貌
55-
})()
55+
((async () => {
56+
const convert = await OpenCC.Converter('hk', 'cn');
57+
const startNode = document.documentElement; // 轉換整個頁面
58+
const HTMLConvertHandler = OpenCC.HTMLConverter(convert, startNode, 'zh-HK', 'zh-CN'); // 將所有 zh-HK 標籤轉為 zh-CN 標籤
59+
HTMLConvertHandler.convert(); // 開始轉換
60+
HTMLConvertHandler.restore(); // 回到原貌
61+
})());
5662
```
5763

5864
class list 包含 `ignore-opencc` 的元素會跳過後續的轉換,該節點的所有子節點都不會被轉換。

0 commit comments

Comments
 (0)