mm_func 1.0.0 → 1.0.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/README.md CHANGED
@@ -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/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 - 函数名称
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_func",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "函数钩子管理模块:提供函数钩子 addFunc/delFunc/runFunc 的注册与按名执行能力",
5
5
  "main": "index.js",
6
6
  "repository": {
package/test.js CHANGED
@@ -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');