Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite cld to actually be async with a promise API #62

Merged
merged 2 commits into from
May 6, 2020
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
34 changes: 30 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,53 @@ Linux users, make sure you have g++ >= 4.8. If this is not an option, you should
## Examples
### Simple
```js
require('cld').detect('This is a language recognition example', function(err, result) {
const cld = require('cld');

// As a promise
cld.detect('This is a language recognition example').then((result) => {
console.log(result);
});

// In an async function
async function testCld() {
const result = await cld.detect('This is a language recognition example');
console.log(result);
}
```

### Advanced
```js
var text = 'Това е пример за разпознаване на Български език';
var options = {
const cld = require('cld');
const text = 'Това е пример за разпознаване на Български език';
const options = {
isHTML : false,
languageHint : 'BULGARIAN',
encodingHint : 'ISO_8859_5',
tldHint : 'bg',
httpHint : 'bg'
};

require('cld').detect(text, options, function(err, result) {
// As a promise
cld.detect(text, options).then((result) => {
console.log(result);
});

// In an async function
async function testCld() {
const result = await cld.detect(text, options);
console.log(result);
}
```

### Legacy
Detect can be called leveraging the node callback pattern. If options are provided, the third parameter should be the callback.
```javascript
const cld = require('cld');

cld.detect('This is a language recognition example', (err, result) => {
console.log(result);
});
```

## Options

Expand Down
125 changes: 68 additions & 57 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,87 @@
var _ = require('underscore');
var cld2 = require('./build/Release/cld');
const _ = require('underscore');
const cld2 = require('./build/Release/cld');

module.exports = {
LANGUAGES : cld2.LANGUAGES,
DETECTED_LANGUAGES : cld2.DETECTED_LANGUAGES,
ENCODINGS : cld2.ENCODINGS,

detect : function (text, options, cb) {
if (arguments.length < 2) {
return;
}
if (arguments.length < 3) {
async detect(text, options) {
let cb = arguments[2];
if (typeof cb !== 'function' && typeof options === 'function') {
cb = options;
options = {};
}
if (!_.isFunction(cb)) {
return;
}

if (!_.isString(text) || text.length < 1) {
return cb({message:'Empty or invalid text'});
}
try {
if (arguments.length < 1) {
throw new Error('Not enough arguments provided');
}

var defaults = {
isHTML : false,
languageHint : '',
encodingHint : '',
tldHint : '',
httpHint : ''
};
options = _.defaults(options, defaults);
if (!_.isString(text) || text.length < 1) {
throw new Error('Empty or invalid text');
}

if (!_.isBoolean(options.isHTML)) {
return cb({message:'Invalid isHTML value'});
}
if (!_.isString(options.languageHint)) {
return cb({message:'Invalid languageHint'});
}
if (!_.isString(options.encodingHint)) {
return cb({message:'Invalid encodingHint'});
}
if (!_.isString(options.tldHint)) {
return cb({message:'Invalid tldHint'});
}
if (!_.isString(options.httpHint)) {
return cb({message:'Invalid httpHint'});
}
if (options.encodingHint.length > 0 &&
!~cld2.ENCODINGS.indexOf(options.encodingHint)) {
const defaults = {
isHTML : false,
languageHint : '',
encodingHint : '',
tldHint : '',
httpHint : ''
};
options = _.defaults(options, defaults);

return cb({message:'Invalid encodingHint, see ENCODINGS'});
}
if (options.languageHint.length > 0 &&
!~_.keys(cld2.LANGUAGES).indexOf(options.languageHint) &&
!~_.values(cld2.LANGUAGES).indexOf(options.languageHint)) {
if (!_.isBoolean(options.isHTML)) {
throw new Error('Invalid isHTML value');
}
if (!_.isString(options.languageHint)) {
throw new Error('Invalid languageHint');
}
if (!_.isString(options.encodingHint)) {
throw new Error('Invalid encodingHint');
}
if (!_.isString(options.tldHint)) {
throw new Error('Invalid tldHint');
}
if (!_.isString(options.httpHint)) {
throw new Error('Invalid httpHint');
}
if (options.encodingHint.length > 0 &&
!~cld2.ENCODINGS.indexOf(options.encodingHint)) {

return cb({message:'Invalid languageHint, see LANGUAGES'});
}
throw new Error('Invalid encodingHint, see ENCODINGS');
}
if (options.languageHint.length > 0 &&
!~_.keys(cld2.LANGUAGES).indexOf(options.languageHint) &&
!~_.values(cld2.LANGUAGES).indexOf(options.languageHint)) {

var result = cld2.detect(
text,
!options.isHTML,
options.languageHint,
options.encodingHint,
options.tldHint,
options.httpHint
);
throw new Error('Invalid languageHint, see LANGUAGES');
}

if (result.languages.length < 1) {
return cb({message:'Failed to identify language'});
}
const result = await cld2.detectAsync(
text,
!options.isHTML,
options.languageHint,
options.encodingHint,
options.tldHint,
options.httpHint
);

if (result.languages.length < 1) {
throw new Error('Failed to identify language');
}

return cb(null, result);
if (cb) {
return cb(null, result);
} else {
return result;
}
} catch (error) {
if (cb) {
cb(error);
} else {
throw error;
}
}
}
};
Loading