mm_func 1.0.0 → 1.2.0

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/README.md CHANGED
@@ -37,7 +37,7 @@ func.delFunc('list', 'makeStatic');
37
37
  func.clear('list'); // 清空指定函数
38
38
  func.clear(); // 清空全部
39
39
 
40
- // 通过 Admin 工厂按作用域取实例(需安装 mm_corejs)
40
+ // 通过 Admin 工厂按作用域取实例(需安装 mm_admin)
41
41
  var admin = funcAdmin('os', { /* config */ });
42
42
  ```
43
43
 
@@ -47,6 +47,7 @@ var admin = funcAdmin('os', { /* config */ });
47
47
  | --- | --- |
48
48
  | `addFunc(name, func, sort = 100, alias = '')` | 添加函数钩子,返回别名 |
49
49
  | `delFunc(name, alias = '')` | 删除函数钩子,别名省略时清空该名称下全部 |
50
+ | `getFunc(name)` | 获取第一个函数体,未注册时返回 undefined |
50
51
  | `hasFunc(name)` | 判断函数是否存在 |
51
52
  | `listFuncs(name)` | 列出函数名称(省略 name)或函数项列表 |
52
53
  | `runFunc(name, ...args)` | 异步按序执行,支持 Promise,返回最后一个真值 |
package/index.js CHANGED
@@ -1,27 +1,17 @@
1
1
  const Func = require('./lib/func');
2
- let Admin = null;
3
- try {
4
- Admin = require('mm_corejs').Admin;
5
- } catch (e) {
6
- // mm_corejs 未安装时仅提供核心类,Admin 工厂不可用
7
- }
2
+ const { reg } = require('mm_admin');
8
3
 
9
- let admin = null;
10
- if (Admin) {
11
- admin = new Admin();
12
- admin.setClass(Func);
13
- }
4
+ const admin = reg('mm_func');
5
+ admin.setClass(Func);
14
6
 
15
7
  module.exports = {
16
8
  Func,
17
9
  admin,
18
10
  funcAdmin(scope, config = {}) {
19
- if (!admin) {
20
- throw new Error('mm_func: mm_corejs 未安装,Admin 工厂不可用');
21
- }
22
- if (!scope) {
23
- scope = 'os';
24
- }
25
- return admin.get(scope, config);
11
+ return admin.get(scope || 'os', config);
26
12
  }
27
13
  };
14
+
15
+ /* mm-tool-logic-export 工具能力层:其它模块可 require("<pkg>").tools.<name> 直接引用 */
16
+ Object.defineProperty(module.exports, 'tools', { enumerable: true, configurable: true,
17
+ get: function () { return require('./lib/tools'); } });
package/lib/func.js CHANGED
@@ -110,6 +110,16 @@ class Func {
110
110
  return removed;
111
111
  }
112
112
 
113
+ /**
114
+ * 获取函数
115
+ * @param {string} name - 函数名称
116
+ * @returns {Function|undefined} 第一个函数体;未注册时为 undefined
117
+ */
118
+ getFunc(name) {
119
+ var arr = this._methods[name];
120
+ return arr && arr.length ? arr[0].func : undefined;
121
+ }
122
+
113
123
  /**
114
124
  * 判断函数是否存在
115
125
  * @param {string} name - 函数名称
@@ -0,0 +1,41 @@
1
+ /**
2
+ * 函数调用器
3
+ * 调用 mm_func 全局作用域(funcAdmin('os'))中注册的具名函数
4
+ */
5
+ const { funcAdmin } = require('../../../index.js');
6
+
7
+ async function main(params, ctx, options) {
8
+ const func_name = params.function || '';
9
+ const args = params.args || [];
10
+
11
+ if (!func_name) {
12
+ ctx.output = {
13
+ error: {
14
+ code: 'EXEC_MISSING_PARAM',
15
+ message: '函数名不能为空'
16
+ }
17
+ };
18
+ return ctx.output;
19
+ }
20
+
21
+ var func_store = funcAdmin('os');
22
+ if (!func_store.hasFunc(func_name)) {
23
+ ctx.output = {
24
+ error: {
25
+ code: 'EXEC_NOT_FOUND',
26
+ message: '函数 ' + func_name + ' 未找到'
27
+ }
28
+ };
29
+ return ctx.output;
30
+ }
31
+
32
+ const result = await func_store.runFunc(func_name, ...args);
33
+
34
+ ctx.output = {
35
+ msg: '函数执行完成',
36
+ output: result
37
+ };
38
+ return ctx.output;
39
+ }
40
+
41
+ module.exports = { main };
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ /** 模块能力层:懒加载工具实现,避免模块入口被 require 时加载全部实现 */
5
+ module.exports = new Proxy({}, {
6
+ get(_t, key) {
7
+ if (typeof key !== 'string') return undefined;
8
+ if (!fs.existsSync(path.join(__dirname, key, 'index.js'))) return undefined;
9
+ return require('./' + key);
10
+ },
11
+ has(_t, key) {
12
+ return typeof key === 'string' && fs.existsSync(path.join(__dirname, key, 'index.js'));
13
+ },
14
+ ownKeys() { return fs.readdirSync(__dirname, { withFileTypes: true }).filter(e => e.isDirectory()).map(e => e.name); },
15
+ getOwnPropertyDescriptor(_t, key) { return this.has(_t, key) ? { enumerable: true, configurable: true } : undefined; }
16
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_func",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "函数钩子管理模块:提供函数钩子 addFunc/delFunc/runFunc 的注册与按名执行能力",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -14,8 +14,8 @@
14
14
  "hook",
15
15
  "函数"
16
16
  ],
17
- "layer": "infra",
18
- "group": "common",
17
+ "layer": "shared",
18
+ "group": "hooks",
19
19
  "author": "邱文武",
20
20
  "license": "ISC",
21
21
  "engines": {
@@ -25,6 +25,6 @@
25
25
  "test": "node test.js"
26
26
  },
27
27
  "dependencies": {
28
- "mm_corejs": "^1.1.6"
28
+ "mm_admin": "^1.1.0"
29
29
  }
30
30
  }
package/test.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  const assert = require('assert');
4
4
  const mod = require('./index.js');
5
- const Func = require('./lib/func');
5
+ const Func = require('./lib/func.js');
6
6
 
7
7
  // 1. 导出结构校验
8
8
  assert.strictEqual(typeof mod.Func, 'function', '应导出 Func 类');
@@ -28,7 +28,14 @@ async function test() {
28
28
  assert.strictEqual(await func.runFunc('none'), undefined);
29
29
  console.log('√ 未注册返回 undefined');
30
30
 
31
- // 4. 别名:具名函数自动取函数名
31
+ // 4. getFunc:取函数体
32
+ var got = func.getFunc('list');
33
+ assert.strictEqual(typeof got, 'function');
34
+ assert.strictEqual(got.name, 'makeList');
35
+ assert.strictEqual(func.getFunc('none'), undefined);
36
+ console.log('√ getFunc 取函数体');
37
+
38
+ // 5. 别名:具名函数自动取函数名
32
39
  var func2 = new Func();
33
40
  var alias = func2.addFunc('test', function myFunc() {});
34
41
  assert.strictEqual(alias, 'myFunc');
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * function —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/function/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_func').tools.function 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/function');
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "function",
3
+ "handler": "executor",
4
+ "title": "函数调用器",
5
+ "description": "调用全局注册函数 global.$[funcName],支持传入参数数组。适用场景:需要复用全局函数、避免重复实现公共逻辑时。工作流提示:function 必填指定全局函数名,args 按序传参数数组,函数返回值在 output。",
6
+ "type": "core",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "function": {
11
+ "type": "string",
12
+ "description": "全局函数名"
13
+ },
14
+ "args": {
15
+ "type": "array",
16
+ "description": "传入函数的参数数组"
17
+ }
18
+ },
19
+ "required": [
20
+ "function"
21
+ ]
22
+ },
23
+ "returns": {
24
+ "type": "object",
25
+ "description": "返回执行结果,正常时包含 msg 和函数输出,异常时包含 error",
26
+ "properties": {
27
+ "msg": {
28
+ "type": "string",
29
+ "description": "函数执行完成"
30
+ },
31
+ "output": {
32
+ "description": "函数调用返回值"
33
+ },
34
+ "error": {
35
+ "type": "object",
36
+ "description": "错误信息,仅调用出错时返回",
37
+ "properties": {
38
+ "code": {
39
+ "type": "string",
40
+ "description": "错误码 (EXEC_ 前缀)"
41
+ },
42
+ "message": {
43
+ "type": "string",
44
+ "description": "错误描述"
45
+ }
46
+ }
47
+ }
48
+ }
49
+ },
50
+ "options": {},
51
+ "group": "core"
52
+ }