all files / dist/ yandex.js

94.29% Statements 33/35
47.37% Branches 9/19
92.86% Functions 13/14
96.77% Lines 30/31
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                      12× 12×                                                                            
'use strict';
 
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
 
Object.defineProperty(exports, "__esModule", {
	value: true
});
 
var _got = require('got');
 
var _got2 = _interopRequireDefault(_got);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function Yandex(translateKey, dictionaryKey) {
	this.translateKey = translateKey;
	this.dictionaryKey = dictionaryKey;
};
 
var get = function get(url, query) {
	return (0, _got2.default)(url, { json: true, query: query }).then(function (_) {
		return _.body;
	});
};
 
// https://tech.yandex.ru/translate/doc/dg/reference/translate-docpage/
Yandex.prototype.translate = function (fromLang, toLang, text) {
	return get('https://translate.yandex.net/api/v1.5/tr.json/translate', {
		key: this.translateKey,
		lang: fromLang + '-' + toLang,
		text: text
	}).then(function (_) {
		return _.text.join(' ');
	});
};
 
// https://tech.yandex.ru/dictionary/doc/dg/reference/lookup-docpage/
Yandex.prototype.dictionary = function (fromLang, toLang, text) {
	return get('https://dictionary.yandex.net/api/v1/dicservice.json/lookup?key=API-ключ&lang=en-ru&text=time', {
		key: this.dictionaryKey,
		lang: fromLang + '-' + toLang,
		flags: 4, // MORPHO = 0x0004 - включает поиск по форме слова;
		text: text
	}).then(function (_) {
		return _.def;
	});
};
 
Yandex.prototype.spellCheck = function (lang, text) {
	return get('https://speller.yandex.net/services/spellservice.json/checkText', {
		options: 256,
		lang: lang,
		text: text
	}).then(function (corrections) {
		Eif (corrections.length > 0) {
			var _ret = function () {
				var prev = 0;
				var result = '';
 
				corrections.forEach(function (correction) {
					result += text.slice(prev, correction.pos);
					result += correction.s;
 
					prev = correction.pos + correction.len;
				});
 
				return {
					v: result
				};
			}();
 
			Eif ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
		} else {
			return text;
		}
	});
};
 
exports.default = Yandex;