UNPKG

what-is-surprise

Version:

translate tool

144 lines (143 loc) 4.7 kB
var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; (function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseFanyi = void 0; var BaseFanyi = /** @class */ (function () { function BaseFanyi() { this.config = {}; this.url = ''; this.contents = new Set(); } /** * 获得配置,可以从 API 实例进行更新,或者直接操作 */ BaseFanyi.prototype.getConfig = function () { return this.config; }; /** * 更新 API 地址,通常在 config 配置 * @param url API地址 */ BaseFanyi.prototype.setApiUrl = function (url) { this.url = url; return this; }; /** * 设置源语言 * @param from 源语言 */ BaseFanyi.prototype.setLangFrom = function (from) { this.config.from = from; return this; }; /** * 设置目标语言 * @param to 目标语言 */ BaseFanyi.prototype.setLangTo = function (to) { this.config.to = to; return this; }; /** * 交换配置中 from 和 to 的语言设置 */ BaseFanyi.prototype.swapLang = function () { var _a; _a = __read([this.config.from, this.config.to], 2), this.config.to = _a[0], this.config.from = _a[1]; return this; }; BaseFanyi.prototype.createRequestBody = function () { this.config.updateQuery(this.generateContent()); this.config.updateSign(); return this.config.release(); }; BaseFanyi.prototype.generateContent = function () { var e_1, _a; var content = ''; try { for (var _b = __values(this.contents.values()), _c = _b.next(); !_c.done; _c = _b.next()) { var part = _c.value; content += part + '\n'; } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } return content; }; /** * 添加内容 * @param content 要添加的内容 */ BaseFanyi.prototype.addContent = function (content) { this.contents.add(content); return this; }; /** * 删除内容 * @param content 删除的内容 */ BaseFanyi.prototype.removeContent = function (content) { this.contents.delete(content); return this; }; /** * 清除所有已添加内容 */ BaseFanyi.prototype.clearContent = function () { this.contents.clear(); return this; }; /** * 更换内容 * @param newContent 新的内容 * @param oldContent 旧内容 */ BaseFanyi.prototype.replaceContent = function (newContent, oldContent) { this.contents.delete(oldContent); this.contents.add(newContent); return this; }; return BaseFanyi; }()); exports.BaseFanyi = BaseFanyi; });