recogn-parse
Version:
recogn-parse 提供了 识别--解析 的机制,使用者只需要添加识别器 和 解析器,然后就可以对事件进行识别和解析,适合用所有具备 识别--解析 流程的场景中,如:加载并显示各种模型、预览文件等
519 lines (518 loc) • 13.6 kB
JavaScript
var v = Object.defineProperty;
var _ = (o, e, r) => e in o ? v(o, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : o[e] = r;
var d = (o, e, r) => _(o, typeof e != "symbol" ? e + "" : e, r);
import { waitAsyncable as a } from "type-tls";
function g(o, e) {
const r = [];
for (const s of e) {
const t = e.indexOf(s);
t !== -1 && (o.splice(t, 1), r.push(s));
}
return r;
}
class A {
constructor() {
/**
* @internal
*/
d(this, "_preJudgers", []);
/**
* @internal
*/
d(this, "_postJudgers", []);
/**
* 命名的鉴定者
* @remarks
* namedJudgers 中的识别顺序是按照加入 namedJudgers 中时的顺序来识别的
*
* namedJudgers {@link Recognizer.namedJudgers} 的优先级低于 judgers {@link Recognizer.judgers},即:
* 会先执行 judgers 中的识别器,如果不能识别,再执行 namedJudgers 中的识别器
*/
d(this, "namedJudgers", /* @__PURE__ */ new Map());
/**
* 匿名的鉴定者列表
*
* @remarks
* namedJudgers {@link Recognizer.namedJudgers} 的优先级低于 judgers {@link Recognizer.judgers},即:
* 会先执行 judgers 中的识别器,如果不能识别,再执行 namedJudgers 中的识别器
*/
d(this, "judgers", []);
}
/**
* 前置鉴定者
*/
get preJudgers() {
return this._preJudgers || (this._preJudgers = []);
}
set preJudgers(e) {
this._preJudgers = e;
}
/**
* 后置鉴定者
*/
get postJudgers() {
return this._postJudgers || (this._postJudgers = []);
}
set postJudgers(e) {
this._postJudgers = e;
}
/**
* 所有的鉴定者
*/
get allJudgers() {
const e = this.namedJudgers.values();
return [...this.judgers, ...e];
}
/**
* 添加鉴定者
* @param judge - 鉴定者
* @param name - 鉴定者的名字
*/
add(e, r) {
if (r) {
const s = this.namedJudgers, t = s.get(r) ?? [];
s.set(r, t.concat(e));
} else {
const s = this.judgers;
s.includes(e) || s.push(e);
}
}
/**
* 添加多个鉴定者
* @param judgers - 鉴定者列表 或 名字与鉴定者的映射
*/
addJudgers(e) {
if (Array.isArray(e)) {
for (const r of e)
this.add(r);
return;
}
for (const [r, s] of Object.entries(e))
this.add(s, r);
}
/**
* 移除指定名字的鉴定者
* @param name - 鉴定者的名字
* @returns 返回值表示 是否移除成功
*/
removeByName(e) {
const r = typeof e == "string" ? [e] : e, s = this.namedJudgers;
for (const t of r)
s.delete(t);
return !0;
}
/**
* 删除 指定的 judger
*
* @remarks
* 当 judger 为 数组类型时,会把其作为 SerialJudger 来对象,不会对 SerialJudger 中包含的 各个 Judge 进行单独查找与删除
*
* @param judger - 鉴定者
* @returns 表示是否移除成功
*/
remove(e) {
const r = this.judgers, s = r.indexOf(e);
if (s !== -1)
return r.splice(s, 1), !0;
const t = this.namedJudgers, n = Array.from(t);
if (Array.isArray(e)) {
const i = n.find((u) => u[1] === e);
if (i) {
const u = i[0];
return t.delete(u), !0;
}
}
return !1;
}
/**
* 批量删除
*
* @remarks
* 会先按照 {@link Recognizer.remove} 的逻辑进行删除,如果删除不成功,则会把 judges 或 其包含的 所有 Judge 进行单独查找与删除,也会在 SerialJudger 的元素中进行查找与删除
*
* @param judges
* @returns 表示是否成功移除
*/
removeJudges(e) {
if (this.remove(e))
return !0;
const r = Array.isArray(e) ? e : [e];
let s = !1;
const t = this.namedJudgers;
for (const [i, u] of t.entries())
g(u, r).length > 0 && (s = !0), u.length === 0 && t.delete(i);
const n = this.judgers;
g(n, r).length > 0 && (s = !0);
const c = [...n];
for (const i of c)
if (Array.isArray(i) && (g(i, r).length > 0 && (s = !0), i.length === 0)) {
const u = n.indexOf(i);
i.splice(u, 1);
}
return s;
}
/**
* 识别目标
*
* @remarks
* 具体的执行逻辑请看 {@link Recognizer}
*
* @param target - 目标
* @param targetOptions - 目标选项
* @param name - 鉴定者的名字
*
* @return 返回的结果可能是同步的,也可能是异步的
*/
recogn(e, r, s) {
let t;
if (s != null && !(t = this.namedJudgers.get(s))) return null;
const n = h(this.preJudgers, 0, e, r, null), c = a(n, (u) => t ? y(t, e, r, u) : h(this.allJudgers, 0, e, r, u)), i = this.postJudgers;
return i.length === 0 ? c : a(c, (u) => h(i, 0, e, r, u));
}
/**
* 异步识别
*
* @remarks
* 只有当最终有识别结果时,才会 resolve,否则,则会 reject。
* 具体的执行逻辑请看 {@link Recognizer}
*
* @param target - 目标
* @param targetOptions - 目标选项
* @param name - 鉴定者的名字
*/
recognAsync(e, r, s) {
const t = this.recogn(e, r, s);
return t instanceof Promise ? t.then((n) => {
if (n == null)
throw n;
return n;
}) : t == null ? Promise.reject(t) : Promise.resolve(t);
}
}
function m(o) {
return o ? Array.isArray(o) ? o.length > 0 ? o : null : [o] : null;
}
function z(o) {
return o ? o instanceof Promise ? o.then((e) => m(e), () => null) : m(o) : null;
}
function h(o, e, r, s, t) {
if (e >= o.length)
return null;
const n = o[e], c = y(n, r, s, t), i = e + 1;
return c instanceof Promise ? c.then((u) => u ?? h(o, i, r, s, t), () => h(o, i, r, s, t)) : c ?? h(o, i, r, s, t);
}
function y(o, e, r, s) {
const t = typeof o == "function" ? [o] : o;
return t.length === 0 ? null : J(t, 0, e, r, s ?? null);
}
function J(o, e, r, s, t) {
if (e >= o.length)
return t;
const n = o[e], c = n(r, s, t), i = z(c), u = e + 1;
return a(i, (l) => J(o, u, r, s, l ?? null));
}
class j {
constructor() {
/**
* @internal
*/
d(this, "_preParses");
/**
* @internal
*/
d(this, "_postParses", []);
/**
* @internal
*/
d(this, "_typeParsers", null);
}
/**
* 前置解析器
* @remarks
* 详情请看 {@link Resolver}
*/
get preParses() {
return this._preParses || (this._preParses = []);
}
set preParses(e) {
this._preParses = e;
}
/**
* 后置解析器
* @remarks
* 详情请看 {@link Resolver}
*/
get postParses() {
return this._postParses || (this._postParses = []);
}
set postParses(e) {
this._postParses = e;
}
/**
* 类型与解析器的映射
*/
get typeParsers() {
return this._typeParsers || (this._typeParsers = {});
}
set typeParsers(e) {
this._typeParsers = e;
}
/**
* 添加解析器
* @param type - 类型
* @param parser - 解析器
*/
add(e, r) {
const s = this.typeParsers[e] ?? [];
this.typeParsers[e] = s.concat(r);
}
/**
* 添加多个解析器
* @param parsers - 类型 与 解析器的映射集合
*/
addParsers(e) {
const r = this.typeParsers;
for (const [s, t] of Object.entries(e)) {
const n = r[s] ?? [];
r[s] = n.concat(t);
}
}
/**
* 删除 解析器
* @param type - 类型
* @param parser - 被删除的解析器
* @returns 是否成功删除
*/
remove(e, r) {
const s = this.typeParsers;
if (!r)
return delete s[e];
const t = s[e];
if (!t)
return !1;
const n = Array.isArray(r) ? r : [r];
return g(t, n).length > 0 ? (t.length === 0 && delete s[e], !0) : !1;
}
/**
* 移除指定类型的解析器
* @param type - 类型 或 类型列表
* @returns
*/
removeType(e) {
const r = typeof e == "string" ? [e] : e, s = this.typeParsers;
for (const t of r)
delete s[t];
return !0;
}
/**
* 解决
*
* @remarks
* 解决的过程请看 {@link Resolver}
*
* @param type - 类型
* @param target - 目标
* @param targetOptions - 目标相关的选项
* @param recognResult - 识别结果
* @returns
*/
resolve(e, r, s, t) {
const n = this.typeParsers[e];
if (!((n == null ? void 0 : n.length) > 0)) return null;
let c;
const i = this.preParses;
i.length > 0 && (c = p(i, 0, r, s, void 0, t, !0));
const u = a(c, (f) => p(n, 0, r, s, f, t)), l = this.postParses;
return l.length === 0 ? u : a(u, (f, P) => P ? u : p(l, 0, r, s, f, t, !0));
}
/**
* 异步解决
*
* @remarks
* 只有当最终有识别结果时,才会 resolve,否则,则会 reject。
* 解决的过程请看 {@link Resolver}
*
* @param type - 类型
* @param target - 目标
* @param targetOptions - 目标相关的选项
* @param recognResult - 识别结果
* @returns
*/
resolveAsync(e, r, s, t) {
const n = this.resolve(e, r, s, t);
return n instanceof Promise ? n.then((c) => {
if (c == null)
throw c;
return c;
}) : n == null ? Promise.reject(n) : Promise.resolve(n);
}
}
function p(o, e, r, s, t, n, c) {
if (e >= o.length)
return t;
const i = o[e], u = i(r, s, t, n), l = e + 1;
return a(u, (f, P) => P && !c ? null : p(o, l, r, s, f, n));
}
class R {
constructor() {
/**
* @internal
*/
d(this, "_recognizer");
/**
* @internal
*/
d(this, "_resolver");
}
/**
* 识别器
*/
get recognizer() {
return this._recognizer || (this._recognizer = new A());
}
set recognizer(e) {
this._recognizer = e;
}
/**
* 前置鉴定器
*/
get preJudgers() {
return this.recognizer.preJudgers;
}
set preJudgers(e) {
this.recognizer.preJudgers = e;
}
/**
* 后置鉴定器
*/
get postJudgers() {
return this.recognizer.postJudgers;
}
set postJudgers(e) {
this.recognizer.postJudgers = e;
}
/**
* 解决者
*/
get resolver() {
return this._resolver || (this._resolver = new j());
}
set resolver(e) {
this._resolver = e;
}
/**
* 前置解析器
*/
get preParses() {
return this.resolver.preParses;
}
set preParses(e) {
this.resolver.preParses = e;
}
/**
* 后置解析器
*/
get postParses() {
return this.resolver.postParses;
}
set postParses(e) {
this.resolver.postParses = e;
}
/**
* 给指定类型添加鉴定解析器
* @param type - 类型
* @param judgerParser - 包含鉴定器 和 解析器
*/
add(e, r) {
const { judger: s, parser: t } = r;
s && this.recognizer.add(s, e), t && this.resolver.add(e, t);
}
/**
* 批量添加鉴定解析器
* @param recognParsers - 类型 和 鉴定器和解析器对的映射
*/
addRecognParsers(e) {
for (const [r, s] of Object.entries(e))
this.add(r, s);
}
/**
* 移除指定类型的鉴定器和解析器
* @param type - 类型
*/
remove(e) {
this.recognizer.removeByName(e), this.resolver.removeType(e);
}
/**
* {@inheritDoc ./Recognizer#Recognizer.recogn}
*/
recogn(e, r, s) {
return this.recognizer.recogn(e, r, s);
}
/**
* {@inheritDoc ./Recognizer#Recognizer.recognAsync}
*/
recognAsync(e, r, s) {
return this.recognizer.recognAsync(e, r, s);
}
/**
* 识别并解析 target
*
* @param target - 识别解析的目标
* @param targetOpts - 目标选项,主要包含的是与目标相关的选项,大多数场景可能是服务于解析目标的,往往不同类型的目标会有不同的选项,
* @param options - resolve 方法本身的选项,用于控制 识别解析过程的,而不是控制针对目标的具体的鉴定、解析逻辑的,通常与目标无关。
*/
resolve(e, r, s) {
const t = s ?? {}, { type: n, noRecogn: c } = t;
let { recogn: i, parse: u } = t;
u = u ?? n;
let l = null;
if (c)
u && (l = [{
type: u
}]);
else if (i = i ?? n, l = this.recognizer.recogn(e, r, i), l instanceof Promise)
return l.then((f) => this.parse(f, e, r));
return this.parse(l, e, r);
}
/**
* 异步识别并解析
*
* @remarks
* 只有当最终有识别结果时,才会 resolve,否则,则会 reject
*
* @param target - 识别解析的目标
* @param targetOpts - 目标选项,主要包含的是与目标相关的选项,大多数场景可能是服务于解析目标的,往往不同类型的目标会有不同的选项,
* @param options - resolve 方法本身的选项,用于控制 识别解析过程的,而不是控制针对目标的具体的鉴定、解析逻辑的,通常与目标无关。
*/
resolveAsync(e, r, s) {
const t = this.resolve(e, r, s);
return t instanceof Promise ? t.then((n) => {
if (n == null)
throw n;
return n;
}) : t == null ? Promise.reject(t) : Promise.resolve(t);
}
/**
* 解析识别的结果
* @param judgeResult - 识别的结果
* @param target - 目标
* @param targetOptions - 目标选项,主要包含的是与目标相关的选项,大多数场景可能是服务于解析目标的,往往不同类型的目标会有不同的选项,
*/
parse(e, r, s) {
if (e == null) return null;
const t = {};
for (const n of e) {
const c = n.type, i = this.resolver.resolve(c, r, s, n);
t[c] = {
recogn: n,
parse: i
};
}
return t;
}
}
export {
R as RecognParse,
A as Recognizer,
j as Resolver
};