diff --git a/README.md b/README.md index b9dce510e..18061ae8f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/node/demo.js b/node/demo.js index e86a743e0..3e0a61b9d 100644 --- a/node/demo.js +++ b/node/demo.js @@ -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; }); diff --git a/node/demo.ts b/node/demo.ts new file mode 100644 index 000000000..9d9114950 --- /dev/null +++ b/node/demo.ts @@ -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 { + console.log('OpenCC version', OpenCC.version); + + const converter = new OpenCC('s2t.json'); + + console.log('Sync API:', converter.convertSync(input)); + + const callbackResult = await new Promise((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; +}); diff --git a/node/ts-demo.ts b/node/ts-demo.ts deleted file mode 100644 index 96a4603b3..000000000 --- a/node/ts-demo.ts +++ /dev/null @@ -1,31 +0,0 @@ -/** - * @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 } from './opencc'; - -async function main() { - const converter: OpenCC = new OpenCC('s2t.json'); - const result: string = await converter.convertPromise('汉字'); - console.log(result); -} - -main();