-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from Axosoft/feature/actually-async
Rewrite cld to actually be async with a promise API
- Loading branch information
Showing
4 changed files
with
288 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
}; |
Oops, something went wrong.