Skip to content
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
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ The npm package supports Node.js `>=20.17`. It uses bundled Node-API
prebuilds when available and falls back to a local `node-gyp` build when the
current platform does not have a matching prebuild.

Bun and Deno can also use the npm package through their npm compatibility
support.

To install the npm CLI:

```sh
Expand All @@ -98,7 +101,20 @@ async function main() {
}
```

See [demo.js](https://github.com/BYVoid/OpenCC/blob/master/node/demo.js) and [ts-demo.ts](https://github.com/BYVoid/OpenCC/blob/master/node/ts-demo.ts).
Inline configurations can be passed with `OpenCC.fromConfig`:

```ts
import { OpenCC } from 'opencc';
const converter = OpenCC.fromConfig({
name: 'Demo Inline Config',
conversion_chain: [{
dict: { type: 'inline', entries: { '鼠标': '滑鼠' } },
}],
});
console.log(converter.convertSync('鼠标')); // 滑鼠
```

See [demo.js](https://github.com/BYVoid/OpenCC/blob/master/node/demo.js) and [demo.ts](https://github.com/BYVoid/OpenCC/blob/master/node/demo.ts).

### Python

Expand Down
55 changes: 41 additions & 14 deletions node/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,50 @@
*/

// In your project you should replace './opencc' with 'opencc'
const OpenCC = require('./opencc');
const { OpenCC } = require('./opencc');

console.log('OpenCC version', OpenCC.version);
const input = '汉字';

// Load the default Simplified to Traditional config
const opencc = new OpenCC('s2t.json');
async function main() {
console.log('OpenCC version', OpenCC.version);

// Sync API
const converted = opencc.convertSync("汉字");
console.log(converted);
const converter = new OpenCC('s2t.json');

// Async API
opencc.convert("汉字", (err, converted) => {
console.log(err, converted);
});
console.log('Sync API:', converter.convertSync(input));

const callbackResult = await new Promise((resolve, reject) => {
converter.convert(input, (err, converted) => {
if (err) {
reject(new Error(err));
return;
}
resolve(converted);
});
});
console.log('Callback API:', callbackResult);

console.log('Promise API:', await converter.convertPromise(input));

const config = {
name: 'Demo Inline Config',
conversion_chain: [
{
dict: {
type: 'inline',
entries: {
'鼠标': '滑鼠',
'软件': '軟體',
},
},
},
],
};

const inlineConverter = OpenCC.fromConfig(config);
console.log('Inline config:', inlineConverter.convertSync('鼠标和软件'));
}

// Async API with Promise
opencc.convertPromise("汉字").then(converted => {
console.log(converted);
main().catch((err) => {
console.error(err);
process.exitCode = 1;
});
73 changes: 73 additions & 0 deletions node/demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* @file
* Example of Node.js API.
*
* @license
* Open Chinese Convert
*
* Copyright 2010-2026 Carbo Kuo and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { OpenCC, OpenCCConfig } from './opencc';

declare const process: {
exitCode?: number;
};

const input = '汉字';

async function main(): Promise<void> {
console.log('OpenCC version', OpenCC.version);

const converter = new OpenCC('s2t.json');

console.log('Sync API:', converter.convertSync(input));

const callbackResult = await new Promise<string>((resolve, reject) => {
converter.convert(input, (err: string | undefined, converted: string) => {
if (err) {
reject(new Error(err));
return;
}
resolve(converted);
});
});
console.log('Callback API:', callbackResult);

console.log('Promise API:', await converter.convertPromise(input));

const config: OpenCCConfig = {
name: 'Demo Inline Config',
conversion_chain: [
{
dict: {
type: 'inline',
entries: {
'鼠标': '滑鼠',
'软件': '軟體',
},
},
},
],
};

const inlineConverter = OpenCC.fromConfig(config);
console.log('Inline config:', inlineConverter.convertSync('鼠标和软件'));
}

main().catch((err: unknown) => {
console.error(err);
process.exitCode = 1;
});
31 changes: 0 additions & 31 deletions node/ts-demo.ts

This file was deleted.

Loading