translatte
Advanced tools
Comparing version 2.3.3 to 2.4.3
10
index.js
@@ -31,3 +31,3 @@ const querystring = require('querystring'); | ||
'The language «[lang]» is not supported', | ||
'Text must not exceed 2900 characters', | ||
'Text must not exceed 5000 bytes', | ||
'The server returned an empty response', | ||
@@ -46,2 +46,3 @@ 'Could not get token from google', | ||
let bytes = languages.utf8Length(text); | ||
opts.client = opts.client || 't'; | ||
@@ -74,6 +75,9 @@ opts.tld = opts.tld || 'com'; | ||
if (text.length > 2900) { | ||
if (bytes > 5000) { | ||
let chars = Math.ceil(text.length / Math.ceil(bytes / 4700)) + 100; | ||
let texts = []; | ||
['\\.\\s', ',\\s', '\\s'].forEach(t => { | ||
texts = text.match(new RegExp('[^]{1,2900}(' + t + '|$)', 'ig')) | ||
if (!texts || !texts.length) { | ||
texts = text.match(new RegExp('[^]{1,' + chars + '}(' + t + '|$)', 'ig')); | ||
} | ||
}); | ||
@@ -80,0 +84,0 @@ if (!texts) return Promise.reject(errors[1]); |
@@ -151,4 +151,36 @@ /** | ||
/** | ||
* Returns utf8 length | ||
* @param str – string | ||
* @returns {number} | ||
*/ | ||
function utf8Length(str) { | ||
var utf8 = []; | ||
for (var i = 0; i < str.length; i++) { | ||
var charcode = str.charCodeAt(i); | ||
if (charcode < 0x80) utf8.push(charcode); | ||
else if (charcode < 0x800) { | ||
utf8.push(0xc0 | (charcode >> 6), | ||
0x80 | (charcode & 0x3f)); | ||
} else if (charcode < 0xd800 || charcode >= 0xe000) { | ||
utf8.push(0xe0 | (charcode >> 12), | ||
0x80 | ((charcode >> 6) & 0x3f), | ||
0x80 | (charcode & 0x3f)); | ||
} | ||
else { | ||
i++; | ||
charcode = 0x10000 + (((charcode & 0x3ff) << 10) | ||
| (str.charCodeAt(i) & 0x3ff)); | ||
utf8.push(0xf0 | (charcode >> 18), | ||
0x80 | ((charcode >> 12) & 0x3f), | ||
0x80 | ((charcode >> 6) & 0x3f), | ||
0x80 | (charcode & 0x3f)); | ||
} | ||
} | ||
return utf8.length; | ||
} | ||
module.exports = langs; | ||
module.exports.isSupported = isSupported; | ||
module.exports.getCode = getCode; | ||
module.exports.getCode = getCode; | ||
module.exports.utf8Length = utf8Length; |
{ | ||
"name": "translatte", | ||
"version": "2.3.3", | ||
"version": "2.4.3", | ||
"description": "A free and unlimited translate for NodeJS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
641567
570