Skip to content

Commit fe6532d

Browse files
committed
utils.getParamNames(): get a function parameters names
1 parent 7af3cd0 commit fe6532d

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ utils.getIPv4(); // "10.7.68.72"
5959
utils.getIP('ppp'); // "10.2.0.231"
6060

6161
utils.getIPv6(); // "fe80::cabc:c8ff:feef:f996"
62+
63+
// get a function parameter's names
64+
utils.getParamNames(function (key1, key2) {}); // ['key1', 'key2']
6265
```
6366

6467
## benchmark

benchmark/get_paramnames.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*!
2+
* utility - benchmark/get_paramnames.js
3+
* Copyright(c) 2013 fengmk2 <fengmk2@gmail.com>
4+
* MIT Licensed
5+
*/
6+
7+
"use strict";
8+
9+
/**
10+
* Module dependencies.
11+
*/
12+
13+
var utils = require('../');
14+
var Benchmark = require('benchmark');
15+
var suite = new Benchmark.Suite();
16+
17+
var foo = function (cid, startDate, endDate, rate, callback) {
18+
console.log('Date.now(): %j', Date.now());
19+
console.log('Date.now(): %j', Date.now());
20+
console.log('Date.now(): %j', Date.now());
21+
console.log('Date.now(): %j', Date.now());
22+
console.log('Date.now(): %j', Date.now());
23+
console.log('Date.now(): %j', Date.now());
24+
console.log('Date.now(): %j', Date.now());
25+
console.log('Date.now(): %j', Date.now());
26+
console.log('Date.now(): %j', Date.now());
27+
console.log('Date.now(): %j', Date.now());
28+
console.log('Date.now(): %j', Date.now());
29+
console.log('Date.now(): %j', Date.now());
30+
console.log('Date.now(): %j', Date.now());
31+
console.log('Date.now(): %j', Date.now());
32+
console.log('Date.now(): %j', Date.now());
33+
console.log('Date.now(): %j', Date.now());
34+
console.log('Date.now(): %j', Date.now());
35+
console.log('Date.now(): %j', Date.now());
36+
};
37+
38+
console.log('cache:', utils.getParamNames(foo));
39+
console.log('no cache:', utils.getParamNames(foo, false));
40+
console.log('------------------------');
41+
42+
suite
43+
.add("utils.getParamNames(foo)", function () {
44+
utils.getParamNames(foo);
45+
})
46+
.add("utils.getParamNames(foo, false) no cache", function () {
47+
utils.getParamNames(foo, false);
48+
})
49+
50+
// add listeners
51+
.on('cycle', function (event) {
52+
console.log(String(event.target));
53+
})
54+
.on('complete', function () {
55+
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
56+
})
57+
.run({ async: false });

lib/utility.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* utility - lib/utility.js
3-
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
3+
* Copyright(c) 2012 - 2013 fengmk2 <fengmk2@gmail.com>
44
* MIT Licensed
55
*/
66

@@ -300,3 +300,21 @@ exports.getIP = exports.getIPv4 = function (interfaceName) {
300300
exports.getIPv6 = function (interfaceName) {
301301
return _getIP('IPv6', interfaceName);
302302
};
303+
304+
/**
305+
* Get a function parameter's names.
306+
*
307+
* @param {Function} func
308+
* @param {Boolean} [useCache], default is true
309+
* @return {Array} names
310+
*/
311+
exports.getParamNames = function (func, cache) {
312+
cache = cache !== false;
313+
if (cache && func.__cache_names) {
314+
return func.__cache_names;
315+
}
316+
var str = func.toString();
317+
var names = str.slice(str.indexOf('(') + 1, str.indexOf(')')).match(/([^\s,]+)/g) || [];
318+
func.__cache_names = names;
319+
return names;
320+
};

test/utility.test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* utility - test/utility.test.js
3-
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
3+
* Copyright(c) 2012 - 2013 fengmk2 <fengmk2@gmail.com>
44
* MIT Licensed
55
*/
66

@@ -37,6 +37,28 @@ describe('utility.test.js', function () {
3737
});
3838
});
3939

40+
describe('getParamNames()', function () {
41+
it('should return parameter names', function () {
42+
utils.getParamNames(function () {}).should.eql([]);
43+
utils.getParamNames(function (key1) {}).should.eql(['key1']);
44+
utils.getParamNames(function (key1,key2) {}).should.eql(['key1', 'key2']);
45+
utils.getParamNames(function (key1, key2) {}).should.eql(['key1', 'key2']);
46+
utils.getParamNames(function ( key1 , key2, key3
47+
,key4, callback) {
48+
console.log('foo');
49+
}).should.eql(['key1', 'key2', 'key3', 'key4', 'callback']);
50+
51+
utils.getParamNames(utils.getParamNames).should.eql(['func', 'cache']);
52+
utils.getParamNames(utils.getParamNames, false).should.eql(['func', 'cache']);
53+
utils.getParamNames(utils.md5).should.eql(['s']);
54+
utils.getParamNames(utils.hmac).should.eql(['algorithm', 'key', 'data', 'encoding']);
55+
utils.getParamNames(utils.hmac).should.eql(['algorithm', 'key', 'data', 'encoding']);
56+
utils.getParamNames(utils.base64encode).should.eql(['s', 'urlsafe']);
57+
utils.getParamNames(utils.base64decode).should.eql(['encode', 'urlsafe']);
58+
utils.getParamNames(utils.escape).should.eql(['html']);
59+
});
60+
});
61+
4062
describe('base64encode() and base64decode()', function () {
4163
it('should return base64 encode string', function () {
4264
var s = '你好¥啊!@#)(_ +/\/\\\

0 commit comments

Comments
 (0)