8 * Copyright 2010-2013 BYVoid <byvoid@byvoid.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
24 * @defgroup node_api Node.js API
26 * Node.js language binding
29 var path = require('path');
30 var binding = require('../build/Release/binding');
32 var assetsPath = path.resolve(__dirname, '../build/Release');
33 var getConfigPath = function (config) {
34 var configPath = config;
35 if (config[0] !== '/' && config[1] !== ':') {
36 // Resolve relative path
37 configPath = path.join(assetsPath, config);
49 var OpenCC = module.exports = function (config) {
51 config = 'zhs2zht.ini';
53 config = getConfigPath(config);
54 this.handler = new binding.Opencc(config);
59 * Default conversion mode.
63 OpenCC.CONVERSION_FAST = 0;
66 * Only converts text into segments.
70 OpenCC.CONVERSION_SEGMENT_ONLY = 1;
73 * List all candidates of every segment.
77 OpenCC.CONVERSION_LIST_CANDIDATES = 2;
80 * Converts input text.
82 * @fn void convert(string input, function callback)
84 * @param input Input text.
85 * @param callback Callback function(err, convertedText).
88 OpenCC.prototype.convert = function (input, callback) {
89 return this.handler.convert(input.toString(), callback);
93 * Converts input text.
95 * @fn string convertSync(string input)
97 * @param input Input text.
98 * @return Converted text.
101 OpenCC.prototype.convertSync = function (input) {
102 return this.handler.convertSync(input.toString());
106 * Sets conversion mode.
108 * @fn void setConversionMode(int conversionMode)
110 * @param conversionMode Conversion mode.
113 OpenCC.prototype.setConversionMode = function (conversionMode) {
114 return this.handler.setConversionMode(conversionMode);