|
1 | | -var _ = require('underscore'); |
2 | | -var cld2 = require('./build/Release/cld'); |
| 1 | +const _ = require('underscore'); |
| 2 | +const cld2 = require('./build/Release/cld'); |
3 | 3 |
|
4 | 4 | module.exports = { |
5 | 5 | LANGUAGES : cld2.LANGUAGES, |
6 | 6 | DETECTED_LANGUAGES : cld2.DETECTED_LANGUAGES, |
7 | 7 | ENCODINGS : cld2.ENCODINGS, |
8 | 8 |
|
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') { |
14 | 12 | cb = options; |
15 | 13 | options = {}; |
16 | 14 | } |
17 | | - if (!_.isFunction(cb)) { |
18 | | - return; |
19 | | - } |
20 | 15 |
|
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 | + } |
24 | 20 |
|
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 | + } |
33 | 24 |
|
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); |
51 | 33 |
|
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)) { |
57 | 51 |
|
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)) { |
60 | 57 |
|
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 | + } |
69 | 60 |
|
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 | + } |
73 | 73 |
|
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 | + } |
75 | 86 | } |
76 | 87 | }; |
0 commit comments