-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
José Moreira
committed
Sep 1, 2014
1 parent
031bf70
commit b679782
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module.exports = (function ( Util ){ | ||
var fn = Util.function = {}, | ||
|
||
// REGEX | ||
STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg, | ||
ARGUMENT_NAMES = /([^\s,]+)/g; | ||
|
||
// Based on @Jack Allan's response at stackoverflow.com | ||
// http://stackoverflow.com/questions/1007981/how-to-get-function-parameter-names-values-dynamically-from-javascript | ||
fn.getParamNames = function ( func ) { | ||
|
||
var fnStr = func.toString().replace(STRIP_COMMENTS, ''); | ||
var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES); | ||
|
||
if(result === null) { | ||
result = []; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var Util = require('../index'), | ||
|
||
sinon = require('sinon'), | ||
chai = require('chai'), | ||
expect = chai.expect; | ||
|
||
describe( "Util", function () { | ||
|
||
describe( ".function", function () { | ||
|
||
describe( ".getParamNames", function () { | ||
|
||
it( "no parameters provided", function (){ | ||
|
||
var res = Util.function.getParamNames( function ( ) {} ); | ||
|
||
expect( Util.is.Array( res ) ).to.be.ok; | ||
expect( res ).to.have.length( 0 ); | ||
|
||
}); | ||
|
||
it( "'a' and 'b' parameters provided", function (){ | ||
|
||
var res = Util.function.getParamNames( function ( a, b ) {} ); | ||
|
||
expect( Util.is.Array( res ) ).to.be.ok; | ||
expect( res ).to.have.length( 2 ); | ||
expect( res[0] ).to.be.equal( 'a' ); | ||
expect( res[1] ).to.be.equal( 'b' ); | ||
|
||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
}); |