mm_matchs 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/matchs.js ADDED
@@ -0,0 +1,351 @@
1
+ require('mm_helper');
2
+ const dict_regex = require('../regex.js');
3
+ let { change, changeFunc, changeArr } = require('./util');
4
+
5
+ /**
6
+ * 匹配拓展
7
+ * @class
8
+ */
9
+ let dict = {
10
+ '{日期时间}': 'datetime',
11
+ '{日期}': 'date',
12
+ '{时间}': 'time',
13
+ // "{年}": "year",
14
+ // "{月}": "month",
15
+ // "{日}": "day",
16
+ '{年月日}': 'year_month_day',
17
+ '{月日}': 'month_day',
18
+ '{时分秒}': 'hour_minute_second',
19
+ '{时分}': 'hour_minute',
20
+ '{时态}': 'tense',
21
+ '{星期}': 'week',
22
+ '{周期}': 'cycle',
23
+
24
+ '{国家}': 'country',
25
+ '{省份}': 'province',
26
+ '{城市}': 'city',
27
+ '{管辖区}': 'district',
28
+ '{方位}': 'bearing',
29
+ '{性别}': 'gender',
30
+ '{年龄}': 'age',
31
+ '{不对}': 'wrong',
32
+ '{职位}': 'job',
33
+
34
+ '{网址}': 'url',
35
+ '{域名}': 'web',
36
+ '{邮箱}': 'email',
37
+ '{账号}': 'account',
38
+ '{强账号}': 'strong_account',
39
+ '{密码}': 'password',
40
+ '{强密码}': 'strong_password',
41
+
42
+ '{身份证号}': 'idcard',
43
+ '{车牌号}': 'car_number',
44
+ '{邮政编码}': 'post_code',
45
+ '{疑问}': 'question',
46
+ '{颜色}': 'color',
47
+ '{中文}': 'chinese',
48
+ '{英文}': 'english',
49
+ '{中英文}': 'chinese_english',
50
+ '{中文或英文}': 'chinese_or_english',
51
+ '{手机号码}': 'phone',
52
+ '{电话号码}': 'telephone',
53
+ '{数字}': 'digits',
54
+ '{数值}': 'number',
55
+ '{浮点数}': 'float',
56
+
57
+ '{IP}': 'ip',
58
+ '{QQ}': 'qq',
59
+ '{a标签}': 'a_tag',
60
+ '{img标签}': 'img_tag',
61
+ '{html标签}': 'html_tag',
62
+ '{标签之间}': 'htmlBetween',
63
+ '{链接}': 'href',
64
+ };
65
+
66
+ /**
67
+ * 用作拓展的函数
68
+ * @class
69
+ */
70
+ class Matchs {
71
+ /**
72
+ * 匹配拓展
73
+ */
74
+ constructor(config) {
75
+ this.dict = dict;
76
+ this.dict_regex = dict_regex;
77
+ }
78
+ }
79
+
80
+ Matchs.prototype.change = change;
81
+ Matchs.prototype.changeFunc = changeFunc;
82
+ Matchs.prototype.changeArr = changeArr;
83
+
84
+ /**
85
+ * 匹配{国家}
86
+ * @param {string} str 匹配的字符串
87
+ * @param {string} format 匹配格式
88
+ * @returns {string} 返回新的格式
89
+ */
90
+ Matchs.prototype.country = function (str, format) {
91
+ let arr = ['中国', '蒙古', '朝鲜', '韩国', '日本', '菲律宾', '越南', '老挝', '柬埔寨', '缅甸', '泰国', '马来西亚', '文莱', '新加坡', '印度尼西亚',
92
+ ' 东帝汶',
93
+ '尼泊尔', '不丹', '孟加拉国', '印度', '巴基斯坦', '斯里兰卡', '马尔代夫', '哈萨克斯坦', '吉尔吉斯斯坦', '塔吉克斯坦', '乌兹别克斯坦', '土库曼斯坦', '阿富汗',
94
+ '伊拉克',
95
+ '伊朗', '叙利亚', '约旦', '黎巴嫩', '以色列', '巴勒斯坦', '沙特阿拉伯', '巴林', '卡塔尔', '科威特', '阿拉伯联合酋长国', '阿联酋', '阿曼', '也门',
96
+ '格鲁吉亚', '亚美尼亚',
97
+ '阿塞拜疆', '土耳其', '塞浦路斯', '芬兰', '瑞典', '挪威', '冰岛', '丹麦 法罗群岛', '爱沙尼亚', '拉脱维亚', '立陶宛', '白俄罗斯', '俄罗斯', '乌克兰',
98
+ '摩尔多瓦', '波兰',
99
+ '捷克', '斯洛伐克', '匈牙利', '德国', '奥地利', '瑞士', '列支敦士登', '英国', '爱尔兰', '荷兰', '比利时', '卢森堡', '法国', '摩纳哥', '罗马尼亚',
100
+ '保加利亚',
101
+ '塞尔维亚', '黑山', '马其顿', '阿尔巴尼亚', '希腊', '斯洛文尼亚', '克罗地亚', '波斯尼亚', '墨塞哥维那', '意大利', '梵蒂冈', '圣马力诺', '马耳他',
102
+ '西班牙', '葡萄牙',
103
+ '安道尔', '埃及', '利比亚', '苏丹', '突尼斯', '阿尔及利亚', '摩洛哥', '亚速尔群岛', '马德拉群岛', '埃塞俄比亚', '厄立特里亚', '索马里', '吉布提',
104
+ '肯尼亚', '坦桑尼亚',
105
+ '乌干达', '卢旺达', '布隆迪', '塞舌尔', '乍得', '中非', '喀麦隆', '赤道几内亚', '加蓬', '刚果共和国', '刚果', '刚果民主共和国', '圣多美及普林西比',
106
+ '毛里塔尼亚', '西撒哈拉',
107
+ '塞内加尔', '冈比亚', '马里', '布基纳法索', '几内亚', '几内亚比绍', '佛得角', '塞拉利昂', '利比里亚', '科特迪瓦', '加纳', '多哥', '贝宁', '尼日尔',
108
+ '加那利群岛',
109
+ '赞比亚', '安哥拉', '津巴布韦', '马拉维', '莫桑比克', '博茨瓦纳', '纳米比亚', '南非', '斯威士兰', '莱索托', '马达加斯加', '科摩罗', '毛里求斯', '留尼旺',
110
+ '圣赫勒拿',
111
+ '澳大利亚', '新西兰', '巴布亚新几内亚', '所罗门群岛', '瓦努阿图', '密克罗尼西亚', '马绍尔群岛', '帕劳', '瑙鲁', '基里巴斯', '图瓦卢', '萨摩亚', '斐济群岛',
112
+ '汤加',
113
+ '库克群岛', '关岛', '新喀里多尼亚', '法属波利尼西亚', '皮特凯恩岛', '瓦利斯与富图纳', '纽埃', '托克劳', '美属萨摩亚', '北马里亚纳', '加拿大', '美国',
114
+ '墨西哥', '格陵兰',
115
+ '危地马拉', '伯利兹', '萨尔瓦多', '洪都拉斯', '尼加拉瓜', '哥斯达黎加', '巴拿马', '巴哈马', '古巴', '牙买加', '海地', '多米尼加共和国', '安提瓜和巴布达',
116
+ '圣基茨和尼维斯',
117
+ '多米尼克', '圣卢西亚', '圣文森特和格林纳丁斯', '格林纳达', '巴巴多斯', '特立尼达和多巴哥', '波多黎各', '英属维尔京群岛', '美属维尔京群岛', '安圭拉', '蒙特塞拉特',
118
+ '瓜德罗普',
119
+ '马提尼克', '荷属安的列斯', '阿鲁巴', '特克斯和凯科斯群岛', '开曼群岛', '百慕大', '哥伦比亚', '委内瑞拉', '圭亚那', '法属圭亚那', '苏里南', '厄瓜多尔',
120
+ '秘鲁', '玻利维亚',
121
+ '智利', '阿根廷', '乌拉圭', '巴拉圭'
122
+ ];
123
+ return changeArr(str, format, arr, '{国家}');
124
+ };
125
+
126
+ /**
127
+ * 匹配{域名}
128
+ * @param {string} str 匹配的字符串
129
+ * @param {string} format 匹配格式
130
+ * @returns {string} 返回新的格式
131
+ */
132
+ Matchs.prototype.web = function (str, format) {
133
+ let rx = this.dict_regex.domain;
134
+ return change(str, format, rx, '{域名}').replace(/((https|http|ftp|rtsp|mms)?:\/\/)?/, '');
135
+ };
136
+
137
+
138
+ /**
139
+ * 匹配{链接}
140
+ * @param {string} str 匹配的字符串
141
+ * @param {string} format 匹配格式
142
+ * @returns {string} 返回新的格式
143
+ */
144
+ Matchs.prototype.href = function (str, format) {
145
+ let rx = this.dict_regex.href_attr;
146
+ let f = changeFunc(str, format, rx, '{链接}', (o) => {
147
+ return o.between('"', '"');
148
+ });
149
+ return f;
150
+ };
151
+
152
+ /**
153
+ * 匹配{手机号码}
154
+ * @param {string} str 匹配的字符串
155
+ * @param {string} format 匹配格式
156
+ * @returns {string} 返回新的格式
157
+ */
158
+ Matchs.prototype.phone = function (str, format) {
159
+ let rx = this.dict_regex.mobile;
160
+ let f = changeFunc(str, format, rx, '{手机号码}', (o) => {
161
+ return o.replace(/[ -]/g, '');
162
+ });
163
+ return f;
164
+ };
165
+
166
+ /**
167
+ * 匹配{标签之间}
168
+ * @param {string} str 匹配的字符串
169
+ * @param {string} format 匹配格式
170
+ * @returns {string} 返回新的格式
171
+ */
172
+ Matchs.prototype.htmlBetween = function (str, format) {
173
+ let rx = this.dict_regex.html_tag;
174
+ let f = changeFunc(str, format, rx, '{标签之间}', (o) => {
175
+ let mh = o.match(
176
+ '^<[a-zA-Z!][a-zA-Z\-_]*([ ]+[a-zA-Z][a-zA-Z0-9]+="[ ]*[a-zA-Z0-9/.%&#:?]+[ ]*"[ ]*)(\/>|>)'
177
+ );
178
+ if (mh) {
179
+ let left = mh[0];
180
+ // $.log.debug('标签?', left)
181
+ let mh = o.match('</[a-zA-Z!][a-zA-Z\-_]*>$');
182
+ if (mh) {
183
+ let right = mh[0];
184
+ // $.log.debug('标签?', right)
185
+ let str = o.substring(0, o.length - right.length).replace(left, '');
186
+ // $.log.debug('标签?', str);
187
+ return str;
188
+ }
189
+ }
190
+ return '';
191
+ });
192
+ return f;
193
+ };
194
+
195
+ /**
196
+ * 获取匹配的内容
197
+ * @param {string} str 要匹配的字符串
198
+ * @param {string} format 用作匹配的字符串格式
199
+ * @returns {string} 返回匹配的结果
200
+ */
201
+ Matchs.prototype.get = function (str, format) {
202
+ let parsed = this._parseFormat(format);
203
+ let fmt = this._processRegexReplace(format, parsed.rxs);
204
+ fmt = this._processDictReplace(str, fmt, parsed.arr);
205
+ return this._executeRegexMatch(str, fmt);
206
+ };
207
+
208
+ /**
209
+ * 解析格式字符串
210
+ * @param {string} format 格式字符串
211
+ * @returns {object} 返回解析结果对象
212
+ */
213
+ Matchs.prototype._parseFormat = function (format) {
214
+ let rxs = [];
215
+ let arr = [];
216
+ let ft = format.replace(/\{/g, '`{').replace(/\}/g, '}`');
217
+
218
+ ft.split('`').map((o) => {
219
+ if (o.indexOf('{') !== -1) {
220
+ if (!/\{[0-9]+,?[0-9]+\}/.test(o)) {
221
+ let rx = this.toRegex(o);
222
+ if (rx) {
223
+ rxs.push({
224
+ word: o,
225
+ rx: rx.toString().trim('/')
226
+ });
227
+ } else {
228
+ arr.push(o);
229
+ }
230
+ }
231
+ }
232
+ });
233
+
234
+ return {
235
+ rxs: rxs,
236
+ arr: arr
237
+ };
238
+ };
239
+
240
+ /**
241
+ * 处理正则表达式替换
242
+ * @param {string} fmt 格式字符串
243
+ * @param {Array} rxs 正则表达式数组
244
+ * @returns {string} 返回处理后的格式字符串
245
+ */
246
+ Matchs.prototype._processRegexReplace = function (fmt, rxs) {
247
+ let proc_fmt = fmt.replace(/\r/g, '\\r').replace(/\n/g, '\\n').replace(/\*/g, '.*').replace(/\//g, '\\/');
248
+
249
+ try {
250
+ rxs.map((o) => {
251
+ proc_fmt = proc_fmt.replace(o.word, o.rx);
252
+ });
253
+ } catch (e) {
254
+ console.error('错误!', e);
255
+ }
256
+
257
+ return proc_fmt;
258
+ };
259
+
260
+ /**
261
+ * 处理字典方法替换
262
+ * @param {string} str 要匹配的字符串
263
+ * @param {string} fmt 格式字符串
264
+ * @param {Array} arr 字典方法数组
265
+ * @returns {string} 返回处理后的格式字符串
266
+ */
267
+ Matchs.prototype._processDictReplace = function (str, fmt, arr) {
268
+ let proc_fmt = fmt;
269
+
270
+ for (let i = 0; i < arr.length; i++) {
271
+ let key = arr[i];
272
+ let method = this.dict[key];
273
+ if (method) {
274
+ if (this[method]) {
275
+ let f = this[method](str, key);
276
+ proc_fmt = proc_fmt.replace(key, f.replace(/\\/g, '\\\\').replace(/\//g, '\\/').replace(/\./g, '\\.')
277
+ .replace(/\r/g, '\\r').replace(/\n/g, '\\n'));
278
+ }
279
+ }
280
+ }
281
+
282
+ return proc_fmt;
283
+ };
284
+
285
+ /**
286
+ * 执行正则匹配
287
+ * @param {string} str 要匹配的字符串
288
+ * @param {string} fmt 格式字符串
289
+ * @returns {string} 返回匹配结果
290
+ */
291
+ Matchs.prototype._executeRegexMatch = function (str, fmt) {
292
+ let txt = '';
293
+ if (fmt) {
294
+ try {
295
+ let proc_fmt = fmt.replace(/\]\+\{/g, ']{');
296
+ let rx = eval('/' + proc_fmt + '/');
297
+ let mh = str.match(rx);
298
+ if (mh) {
299
+ txt = mh[0];
300
+ }
301
+ } catch (e) {
302
+ console.error('错误!', e);
303
+ }
304
+ }
305
+ return txt;
306
+ };
307
+
308
+ /**
309
+ * 转正则表达式
310
+ * @param {string} format 格式
311
+ * @returns 返回正则表达式
312
+ * @example
313
+ * let matchs = new Matchs();
314
+ * let rx = matchs.toRegex("{数字}");
315
+ */
316
+ Matchs.prototype.toRegex = function (format) {
317
+ let key = this.dict[format];
318
+ if (!key) {
319
+ return '';
320
+ }
321
+ return this.dict_regex[key];
322
+ };
323
+
324
+
325
+ /**
326
+ * 匹配字符串
327
+ * @param {string} format 用作匹配的字符串格式
328
+ * @returns {string} 匹配成功返回匹配的字符串,否则返回空
329
+ */
330
+ String.prototype.matchs = function (format) {
331
+ let rx;
332
+ let ret = '';
333
+ let str = this + '';
334
+ if (format.startsWith('/') && (format.endsWith('/') || format.endsWith('/g') || format.endsWith('/gi'))) {
335
+ try {
336
+ rx = eval(format);
337
+ let mh = str.match(rx);
338
+ if (mh) {
339
+ ret = mh[0];
340
+ }
341
+ } catch {
342
+
343
+ }
344
+ } else {
345
+ let matchs = new Matchs();
346
+ ret = matchs.get(str, format);
347
+ }
348
+ return ret;
349
+ };
350
+
351
+ module.exports = Matchs;
package/lib/util.js ADDED
@@ -0,0 +1,75 @@
1
+ /**
2
+ * 改变格式
3
+ * @param {string} str 匹配的字符串
4
+ * @param {string} format 匹配格式
5
+ * @param {object} rx 正则表达式
6
+ * @param {string} key 替换名
7
+ * @returns {string} 返回新的格式
8
+ */
9
+ function change(str, format, rx, key) {
10
+ let t = str;
11
+ let f = format;
12
+ let mh = str.match(rx);
13
+ if (mh) {
14
+ let len = mh.length;
15
+ for (let i = 0; i < len; i++) {
16
+ let k = mh[i];
17
+ if (t.indexOf(k) !== -1) {
18
+ t = t.replace(k, '');
19
+ f = f.replace(key, k);
20
+ }
21
+ }
22
+ }
23
+ return f;
24
+ };
25
+ /**
26
+ * 改变格式(可变形)
27
+ * @param {string} str 匹配的字符串
28
+ * @param {string} format 匹配格式
29
+ * @param {object} rx 正则表达式
30
+ * @param {string} key 替换名
31
+ * @param {function} func 回调函数(用于匹配项变形)
32
+ * @returns {string} 返回新的格式
33
+ */
34
+ function changeFunc(str, format, rx, key, func) {
35
+ let t = str;
36
+ let f = format;
37
+ let mh = str.match(rx);
38
+ if (mh) {
39
+ let len = mh.length;
40
+ for (let i = 0; i < len; i++) {
41
+ let k = mh[i];
42
+ if (t.indexOf(k) !== -1) {
43
+ t = t.replace(k, '');
44
+ if (func) {
45
+ f = f.replace(key, func(k));
46
+ } else {
47
+ f = f.replace(key, k);
48
+ }
49
+ }
50
+ }
51
+ }
52
+ return f;
53
+ };
54
+ /**
55
+ * 改变格式(通过数组)
56
+ * @param {string} str 匹配的字符串
57
+ * @param {string} format 匹配格式
58
+ * @param {Array} arr 数组
59
+ * @param {string} key 替换名
60
+ * @returns {string} 返回新的格式
61
+ */
62
+ function changeArr(str, format, arr, key) {
63
+ let t = str;
64
+ let f = format;
65
+ for (let i = 0; i < arr.length; i++) {
66
+ let k = arr[i];
67
+ if (t.indexOf(k) !== -1) {
68
+ t = t.replace(k, '');
69
+ f = f.replace(key, k);
70
+ }
71
+ }
72
+ return f;
73
+ };
74
+
75
+ module.exports = { change, changeFunc, changeArr };
package/package.json CHANGED
@@ -1,8 +1,14 @@
1
1
  {
2
2
  "name": "mm_matchs",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "这是用来匹配复杂的字符串结构的函数,可以匹配前缀包含、后缀包含、以及时间、地点等",
5
5
  "main": "index.js",
6
+ "files": [
7
+ "index.js",
8
+ "lib",
9
+ "regex.js",
10
+ "tool"
11
+ ],
6
12
  "scripts": {
7
13
  "dev": "nodemon test.js",
8
14
  "test": "node test.js"
@@ -27,8 +33,8 @@
27
33
  "run",
28
34
  "exec"
29
35
  ],
30
- "layer": "shared",
31
- "group": "validation",
36
+ "layer": "util",
37
+ "group": "no_io",
32
38
  "author": "邱文武",
33
39
  "license": "ISC",
34
40
  "bugs": {
@@ -37,7 +43,7 @@
37
43
  "homepage": "https://gitee.com/qiuwenwu91/mm_matchs#readme",
38
44
  "dependencies": {
39
45
  "eslint-plugin-jsdoc": "^64.0.1",
40
- "mm_admin": "^1.2.0",
46
+ "mm_admin": "^1.2.0",
41
47
  "mm_eslint": "^1.7.7",
42
48
  "mm_helper": "^1.0.0"
43
49
  }
@@ -1,7 +1,79 @@
1
- 'use strict';
2
1
  /**
3
- * match —— 工具契约适配层
4
- * 业务实现归位于模块 lib/tools/match/,本文件只把模块能力暴露给加载器。
5
- * 其它模块可直接 require('mm_matchs').tools.match 复用。
2
+ * 文本模式匹配器
3
+ * 基于 Matchs 把中文占位符格式转为正则,并从文本中提取匹配内容
4
+ * @param {object} params 步骤参数
5
+ * @param {object} ctx 上下文对象
6
+ * @returns {object} 匹配结果
6
7
  */
7
- module.exports = require('../../lib/tools/match');
8
+ let { matchAdmin } = require('../../index.js');
9
+
10
+ async function main(params, ctx, options) {
11
+ void options;
12
+ let action = params.action || 'get';
13
+ let matchs = matchAdmin(params.scope || 'os');
14
+
15
+ try {
16
+ if (action !== 'dict' && !params.format) {
17
+ throw new TypeError('缺少 format 匹配格式参数');
18
+ }
19
+
20
+ switch (action) {
21
+ case 'get': {
22
+ if (typeof params.text !== 'string') {
23
+ throw new TypeError('get 需要 text 字符串参数');
24
+ }
25
+ ctx.output = { text: matchs.get(params.text, params.format) };
26
+ break;
27
+ }
28
+
29
+ case 'to_regex':
30
+ ctx.output = { regex: String(matchs.toRegex(params.format)) };
31
+ break;
32
+
33
+ case 'change': {
34
+ if (typeof params.text !== 'string') {
35
+ throw new TypeError('change 需要 text 字符串参数');
36
+ }
37
+ if (!params.regex) {
38
+ throw new TypeError('change 需要 regex 参数');
39
+ }
40
+ if (!params.key) {
41
+ throw new TypeError('change 需要 key 参数');
42
+ }
43
+ ctx.output = { text: matchs.change(params.text, params.format, new RegExp(params.regex), params.key) };
44
+ break;
45
+ }
46
+
47
+ case 'change_arr': {
48
+ if (typeof params.text !== 'string') {
49
+ throw new TypeError('change_arr 需要 text 字符串参数');
50
+ }
51
+ if (!Array.isArray(params.arr)) {
52
+ throw new TypeError('change_arr 需要 arr 数组参数');
53
+ }
54
+ if (!params.key) {
55
+ throw new TypeError('change_arr 需要 key 参数');
56
+ }
57
+ ctx.output = { text: matchs.changeArr(params.text, params.format, params.arr, params.key) };
58
+ break;
59
+ }
60
+
61
+ case 'dict':
62
+ ctx.output = { dict: matchs.dict };
63
+ break;
64
+
65
+ default:
66
+ ctx.output = {
67
+ error: { code: 'MTC_UNKNOWN_ACTION', message: '不支持的操作: ' + action }
68
+ };
69
+ }
70
+ } catch (e) {
71
+ ctx.output = {
72
+ error: { code: 'MATCH_ERROR', message: e.message }
73
+ };
74
+ }
75
+
76
+ return ctx.output;
77
+ }
78
+
79
+ module.exports = { main };
@@ -2,17 +2,20 @@
2
2
  "name": "match",
3
3
  "handler": "matcher",
4
4
  "title": "文本模式匹配器",
5
- "description": "按中文占位符格式从文本中提取指定内容,或把占位符转为正则表达式。支持 {日期}、{时间}、{手机号码}、{邮箱}、{身份证号}、{网址}、{数字}、{中文} 等内置格式。适用场景:从自然语言中抽取手机号、日期、金额等信息。工作流提示:action 选 get/to_regex,get 传 text 与 format,结果在 text;to_regex 传 format,结果在 regex。",
5
+ "description": "按中文占位符格式从文本中提取指定内容、把占位符转为正则,或按格式做替换,并列出全部内置占位符。支持 {日期}、{时间}、{手机号码}、{邮箱}、{身份证号}、{网址}、{数字}、{中文} 等内置格式。适用场景:从自然语言中抽取手机号、日期、金额等信息,或按模板整理文本。工作流提示:action 选 get/to_regex/change/change_arr/dict,get 传 text 与 format,结果在 text;to_regex 传 format,结果在 regex;change 传 text/format/regex/key,结果在 text;change_arr 传 text/format/arr/key,结果在 text;dict 无入参,结果在 dict。",
6
6
  "type": "data",
7
7
  "params": {
8
8
  "type": "object",
9
9
  "properties": {
10
10
  "action": {
11
11
  "type": "string",
12
- "description": "操作类型: get(提取匹配内容)/to_regex(占位符转正则)",
12
+ "description": "操作类型: get(提取匹配内容)/to_regex(占位符转正则)/change(按格式替换)/change_arr(按数组替换)/dict(列出内置占位符)",
13
13
  "enum": [
14
14
  "get",
15
- "to_regex"
15
+ "to_regex",
16
+ "change",
17
+ "change_arr",
18
+ "dict"
16
19
  ],
17
20
  "default": "get"
18
21
  },
@@ -27,26 +30,44 @@
27
30
  },
28
31
  "format": {
29
32
  "type": "string",
30
- "description": "匹配格式,可混用固定文本与占位符,如 \"手机号码是{手机号码}\""
33
+ "description": "匹配格式,可混用固定文本与占位符,如 \"手机号码是{手机号码}\"(dict 以外的操作必填)"
34
+ },
35
+ "regex": {
36
+ "type": "string",
37
+ "description": "用于匹配的正则表达式字符串(change 必填)"
38
+ },
39
+ "key": {
40
+ "type": "string",
41
+ "description": "format 中被替换的占位符,如 {数字}(change/change_arr 必填)"
42
+ },
43
+ "arr": {
44
+ "type": "array",
45
+ "description": "用于替换的候选值数组(change_arr 必填)",
46
+ "items": {
47
+ "type": "string"
48
+ }
31
49
  }
32
50
  },
33
51
  "required": [
34
- "action",
35
- "format"
52
+ "action"
36
53
  ]
37
54
  },
38
55
  "returns": {
39
- "description": "匹配结果,get 返回匹配到的文本,to_regex 返回正则表达式字符串",
56
+ "description": "匹配结果,get/change/change_arr 返回处理后的文本,to_regex 返回正则表达式字符串,dict 返回占位符映射",
40
57
  "type": "object",
41
58
  "properties": {
42
59
  "text": {
43
60
  "type": "string",
44
- "description": "匹配到的内容(get操作)"
61
+ "description": "匹配或替换后的内容(get/change/change_arr 操作)"
45
62
  },
46
63
  "regex": {
47
64
  "type": "string",
48
65
  "description": "正则表达式字符串(to_regex操作)"
49
66
  },
67
+ "dict": {
68
+ "type": "object",
69
+ "description": "内置占位符到匹配方法的映射(dict操作)"
70
+ },
50
71
  "error": {
51
72
  "type": "object",
52
73
  "description": "错误对象 { code, message }"
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+ let { admin } = require('../../index.js');
3
+
4
+ /**
5
+ * 管理匹配实例池
6
+ * @param {object} params 参数:action/scope
7
+ * @param {object} ctx 执行上下文
8
+ * @returns {Promise<object>} 执行结果
9
+ */
10
+ async function main(params, ctx) {
11
+ let p = params || {};
12
+ let action = p.action || 'list';
13
+ let scope = p.scope || 'os';
14
+ /**
15
+ * 输出结果并写入上下文
16
+ * @param {object} data 结果数据
17
+ * @returns {object} 结果数据
18
+ */
19
+ const done = (data) => {
20
+ if (ctx) ctx.output = data;
21
+ return data;
22
+ };
23
+ try {
24
+ if (action === 'list') {
25
+ let scopes = [...admin.keys()];
26
+ return done({ code: 0, msg: '获取成功', action, count: scopes.length, scopes });
27
+ }
28
+ if (action === 'inspect') {
29
+ let exists = admin.has(scope);
30
+ let instance = exists ? admin.get(scope) : null;
31
+ return done({ code: 0, msg: exists ? '获取成功' : '实例不存在', action, scope, exists, config: instance && instance.config ? instance.config : null });
32
+ }
33
+ if (action === 'del') return done({ code: 0, msg: '删除成功', action, scope, ok: admin.del(scope) });
34
+ if (action === 'clear') { admin.clear(); return done({ code: 0, msg: '实例池已清空', action }); }
35
+ return done({ code: 10001, msg: '不支持的操作: ' + action });
36
+ } catch (error) {
37
+ return done({ code: 10000, msg: '执行失败:' + error.message, error: { name: error.name, message: error.message } });
38
+ }
39
+ }
40
+
41
+ module.exports = { main };
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "match_admin",
3
+ "title": "匹配实例管理",
4
+ "description": "管理 mm_matchs 的实例池:list 列出实例、inspect 查看配置、del 删除实例、clear 清空实例池。",
5
+ "category": "匹配引擎",
6
+ "group": "operator",
7
+ "type": "function",
8
+ "main": "./index.js",
9
+ "params": { "type": "object", "properties": { "action": { "type": "string", "description": "操作类型: list/inspect/del/clear", "default": "list" }, "scope": { "type": "string", "description": "实例作用域,缺省 os", "default": "os" } }, "required": ["action"] },
10
+ "handler": "manager"
11
+ }
package/.gitattributes DELETED
@@ -1,5 +0,0 @@
1
- # Auto detect text files and perform LF normalization
2
- * text=auto
3
- *.js linguist-language=JavaScript
4
- *.css linguist-language=JavaScript
5
- *.html linguist-language=JavaScript