Skip to content

Commit 5e22d2e

Browse files
authored
Merge pull request #62 from Axosoft/feature/actually-async
Rewrite cld to actually be async with a promise API
2 parents 407d31c + c9a89d9 commit 5e22d2e

File tree

4 files changed

+288
-190
lines changed

4 files changed

+288
-190
lines changed

README.md

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,53 @@ Linux users, make sure you have g++ >= 4.8. If this is not an option, you should
1919
## Examples
2020
### Simple
2121
```js
22-
require('cld').detect('This is a language recognition example', function(err, result) {
22+
const cld = require('cld');
23+
24+
// As a promise
25+
cld.detect('This is a language recognition example').then((result) => {
2326
console.log(result);
2427
});
28+
29+
// In an async function
30+
async function testCld() {
31+
const result = await cld.detect('This is a language recognition example');
32+
console.log(result);
33+
}
2534
```
2635

2736
### Advanced
2837
```js
29-
var text = 'Това е пример за разпознаване на Български език';
30-
var options = {
38+
const cld = require('cld');
39+
const text = 'Това е пример за разпознаване на Български език';
40+
const options = {
3141
isHTML : false,
3242
languageHint : 'BULGARIAN',
3343
encodingHint : 'ISO_8859_5',
3444
tldHint : 'bg',
3545
httpHint : 'bg'
3646
};
3747

38-
require('cld').detect(text, options, function(err, result) {
48+
// As a promise
49+
cld.detect(text, options).then((result) => {
3950
console.log(result);
4051
});
52+
53+
// In an async function
54+
async function testCld() {
55+
const result = await cld.detect(text, options);
56+
console.log(result);
57+
}
4158
```
4259

60+
### Legacy
61+
Detect can be called leveraging the node callback pattern. If options are provided, the third parameter should be the callback.
62+
```javascript
63+
const cld = require('cld');
64+
65+
cld.detect('This is a language recognition example', (err, result) => {
66+
console.log(result);
67+
});
68+
```
4369

4470
## Options
4571

index.js

Lines changed: 68 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,87 @@
1-
var _ = require('underscore');
2-
var cld2 = require('./build/Release/cld');
1+
const _ = require('underscore');
2+
const cld2 = require('./build/Release/cld');
33

44
module.exports = {
55
LANGUAGES : cld2.LANGUAGES,
66
DETECTED_LANGUAGES : cld2.DETECTED_LANGUAGES,
77
ENCODINGS : cld2.ENCODINGS,
88

9-
detect : function (text, options, cb) {
10-
if (arguments.length < 2) {
11-
return;
12-
}
13-
if (arguments.length < 3) {
9+
async detect(text, options) {
10+
let cb = arguments[2];
11+
if (typeof cb !== 'function' && typeof options === 'function') {
1412
cb = options;
1513
options = {};
1614
}
17-
if (!_.isFunction(cb)) {
18-
return;
19-
}
2015

21-
if (!_.isString(text) || text.length < 1) {
22-
return cb({message:'Empty or invalid text'});
23-
}
16+
try {
17+
if (arguments.length < 1) {
18+
throw new Error('Not enough arguments provided');
19+
}
2420

25-
var defaults = {
26-
isHTML : false,
27-
languageHint : '',
28-
encodingHint : '',
29-
tldHint : '',
30-
httpHint : ''
31-
};
32-
options = _.defaults(options, defaults);
21+
if (!_.isString(text) || text.length < 1) {
22+
throw new Error('Empty or invalid text');
23+
}
3324

34-
if (!_.isBoolean(options.isHTML)) {
35-
return cb({message:'Invalid isHTML value'});
36-
}
37-
if (!_.isString(options.languageHint)) {
38-
return cb({message:'Invalid languageHint'});
39-
}
40-
if (!_.isString(options.encodingHint)) {
41-
return cb({message:'Invalid encodingHint'});
42-
}
43-
if (!_.isString(options.tldHint)) {
44-
return cb({message:'Invalid tldHint'});
45-
}
46-
if (!_.isString(options.httpHint)) {
47-
return cb({message:'Invalid httpHint'});
48-
}
49-
if (options.encodingHint.length > 0 &&
50-
!~cld2.ENCODINGS.indexOf(options.encodingHint)) {
25+
const defaults = {
26+
isHTML : false,
27+
languageHint : '',
28+
encodingHint : '',
29+
tldHint : '',
30+
httpHint : ''
31+
};
32+
options = _.defaults(options, defaults);
5133

52-
return cb({message:'Invalid encodingHint, see ENCODINGS'});
53-
}
54-
if (options.languageHint.length > 0 &&
55-
!~_.keys(cld2.LANGUAGES).indexOf(options.languageHint) &&
56-
!~_.values(cld2.LANGUAGES).indexOf(options.languageHint)) {
34+
if (!_.isBoolean(options.isHTML)) {
35+
throw new Error('Invalid isHTML value');
36+
}
37+
if (!_.isString(options.languageHint)) {
38+
throw new Error('Invalid languageHint');
39+
}
40+
if (!_.isString(options.encodingHint)) {
41+
throw new Error('Invalid encodingHint');
42+
}
43+
if (!_.isString(options.tldHint)) {
44+
throw new Error('Invalid tldHint');
45+
}
46+
if (!_.isString(options.httpHint)) {
47+
throw new Error('Invalid httpHint');
48+
}
49+
if (options.encodingHint.length > 0 &&
50+
!~cld2.ENCODINGS.indexOf(options.encodingHint)) {
5751

58-
return cb({message:'Invalid languageHint, see LANGUAGES'});
59-
}
52+
throw new Error('Invalid encodingHint, see ENCODINGS');
53+
}
54+
if (options.languageHint.length > 0 &&
55+
!~_.keys(cld2.LANGUAGES).indexOf(options.languageHint) &&
56+
!~_.values(cld2.LANGUAGES).indexOf(options.languageHint)) {
6057

61-
var result = cld2.detect(
62-
text,
63-
!options.isHTML,
64-
options.languageHint,
65-
options.encodingHint,
66-
options.tldHint,
67-
options.httpHint
68-
);
58+
throw new Error('Invalid languageHint, see LANGUAGES');
59+
}
6960

70-
if (result.languages.length < 1) {
71-
return cb({message:'Failed to identify language'});
72-
}
61+
const result = await cld2.detectAsync(
62+
text,
63+
!options.isHTML,
64+
options.languageHint,
65+
options.encodingHint,
66+
options.tldHint,
67+
options.httpHint
68+
);
69+
70+
if (result.languages.length < 1) {
71+
throw new Error('Failed to identify language');
72+
}
7373

74-
return cb(null, result);
74+
if (cb) {
75+
return cb(null, result);
76+
} else {
77+
return result;
78+
}
79+
} catch (error) {
80+
if (cb) {
81+
cb(error);
82+
} else {
83+
throw error;
84+
}
85+
}
7586
}
7687
};

0 commit comments

Comments
 (0)