reflect-args
Advanced tools
Comparing version 1.0.1 to 2.0.0
@@ -16,3 +16,3 @@ | ||
let paramString = extractArgs(func) | ||
let pieces = {} | ||
let pieces = [] | ||
@@ -32,3 +32,6 @@ let Mode = Object.freeze({ | ||
let parenth = 0 | ||
let brack = 0 | ||
let escaped = false | ||
let patternMatchedN = 0 | ||
let wasPatternMatched = false | ||
@@ -70,3 +73,3 @@ for (let i = 0; i < paramString.length; i ++) | ||
{ | ||
pieces[varName] = eval(capture.trim()) | ||
pieces.push([varName, eval(capture.trim())]) | ||
capture = '' | ||
@@ -81,3 +84,3 @@ mode = Mode.variable | ||
case Mode.variable: | ||
if (c === '=') | ||
if (curly === 0 && brack === 0 && c === '=') | ||
{ | ||
@@ -91,6 +94,17 @@ mode = Mode.value | ||
} | ||
else if (c === ',') | ||
else if (curly === 0 && brack === 0 && c === ',') | ||
{ | ||
capture = capture.trim() | ||
pieces[capture] = undefined | ||
if (wasPatternMatched === true) | ||
{ | ||
pieces.push(['__' + patternMatchedN, capture.trim()]) | ||
patternMatchedN ++ | ||
wasPatternMatched = false | ||
} | ||
else | ||
{ | ||
capture = capture.trim() | ||
pieces.push([capture, undefined]) | ||
} | ||
capture = '' | ||
@@ -100,2 +114,22 @@ | ||
} | ||
else if (c === '{') | ||
{ | ||
curly ++ | ||
wasPatternMatched = true | ||
capture += c | ||
} | ||
else if (c === '}') | ||
(curly --, capture += c) | ||
else if (c === '[') | ||
{ | ||
brack ++ | ||
wasPatternMatched = true | ||
capture += c | ||
} | ||
else if (c === ']') | ||
(brack --, capture += c) | ||
else | ||
@@ -113,3 +147,3 @@ capture += c | ||
capture = capture.trim() | ||
pieces[capture] = undefined | ||
pieces.push([capture, undefined]) | ||
@@ -119,3 +153,3 @@ break | ||
case Mode.value: | ||
pieces[varName] = eval(capture.trim()) | ||
pieces.push([varName, eval(capture.trim())]) | ||
@@ -125,3 +159,3 @@ break | ||
return pieces | ||
return new Map(pieces) | ||
} | ||
@@ -128,0 +162,0 @@ |
{ | ||
"name": "reflect-args", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Lets you retrieve argument names, including default values from outside a function.", | ||
@@ -31,4 +31,5 @@ "main": "lib/index.js", | ||
"chai": "^4.1.2", | ||
"mocha": "^5.0.4" | ||
"mocha": "^5.0.4", | ||
"nyc": "^11.6.0" | ||
} | ||
} |
@@ -1,2 +0,5 @@ | ||
[![Build Status](https://travis-ci.org/Tabaci/reflect-args.svg?branch=master)](https://travis-ci.org/Tabaci/reflect-args) | ||
<p align="left"> | ||
<a href="https://travis-ci.org/Tabaci/reflect-args"><img src="https://travis-ci.org/Tabaci/reflect-args.svg?branch=master"></a> | ||
<a href="https://codecov.io/gh/Tabaci/reflect-args"><img src="https://codecov.io/gh/Tabaci/reflect-args/branch/master/graph/badge.svg" /></a> | ||
</p> | ||
@@ -23,2 +26,5 @@ # reflect-args | ||
The `getArgs` function takes in a `function` and returns a `Map` containing all | ||
the parameters of that `function`. | ||
### Function or Closure | ||
@@ -34,7 +40,7 @@ | ||
/* Expected output: | ||
* { | ||
* foo: undefined, | ||
* bar: 12, | ||
* test: '12', | ||
* cb: [Function: cb] | ||
* Map { | ||
* 'foo' => undefined, | ||
* 'bar' => 12, | ||
* 'test' => '12', | ||
* 'cb' => [Function: cb] | ||
* } | ||
@@ -60,5 +66,5 @@ */ | ||
/* Expected output: | ||
* { | ||
* foo: undefined, | ||
* bar: undefined | ||
* Map { | ||
* 'foo' => undefined, | ||
* 'bar' => undefined | ||
* } | ||
@@ -68,2 +74,25 @@ */ | ||
### Pattern Matched Parameters | ||
Sometimes you may want to let the end user (programmer) use pattern matching | ||
inside their function whose arguments are reflected. During such cases, the | ||
`getArgs` function will give the keys the names of an incrementing range: | ||
```javascript | ||
let patternMatched = function ({foo, bar}, test, [more, and]) | ||
{ | ||
} | ||
console.log(getArgs(patternMatched)) | ||
/* Expected output: | ||
* Map { | ||
* '__0' => '{foo, bar}', | ||
* 'test' => undefined, | ||
* '__1' => '[more, and]' | ||
* } | ||
*/ | ||
``` | ||
## Discussion | ||
@@ -70,0 +99,0 @@ |
@@ -7,2 +7,12 @@ | ||
function map (...vals) | ||
{ | ||
let arr = [] | ||
for (let i = 0; i < vals.length; i += 2) | ||
arr.push([vals[i], vals[i + 1]]) | ||
return new Map(arr) | ||
} | ||
describe('Getting arguments from outside functions', function () { | ||
@@ -12,12 +22,16 @@ describe('#getArgs (function)', function () { | ||
let func = function (foo, bar) {} | ||
expect(getArgs(func)).to.deep | ||
.equal({ foo: undefined, bar: undefined }) | ||
expect(getArgs(func)).to.deep.equal(map('foo', undefined, 'bar', undefined)) | ||
let clos = (foo) => {} | ||
expect(getArgs(clos)).to.deep.equal({ foo: undefined }) | ||
expect(getArgs(clos)).to.deep.equal(map('foo', undefined)) | ||
}) | ||
it('should allow for default parameters to be escaped', function () { | ||
let func = function(foo = '\ntest') {} | ||
expect(getArgs(func)).to.deep.equal(map('foo', '\ntest')) | ||
}) | ||
it('should list all parameters of the constructor when given a constructor function', function () { | ||
let constFunc = function (test) {} | ||
expect(getArgs(constFunc)).to.deep.equal({ test: undefined }) | ||
expect(getArgs(constFunc)).to.deep.equal(map('test', undefined)) | ||
@@ -29,3 +43,3 @@ let clas = class Test { | ||
let method = (new clas('')).test | ||
expect(getArgs(clas)).to.deep.equal({ test: undefined }) | ||
expect(getArgs(clas)).to.deep.equal(map('test', undefined)) | ||
}) | ||
@@ -35,3 +49,3 @@ | ||
let func = function (test = '12', bar = 2) {} | ||
expect(getArgs(func)).to.deep.equal({ test: '12', bar: 2 }) | ||
expect(getArgs(func)).to.deep.equal(map('test', '12', 'bar', 2)) | ||
@@ -41,3 +55,4 @@ let func2 = (test = () => { return ('hello', 'test') }, bar = () => {}) => { | ||
} | ||
expect(getArgs(func2).test()).to.equal('test') | ||
expect(getArgs(func2).get('test')()).to.equal('test') | ||
}) | ||
@@ -54,6 +69,6 @@ | ||
expect(getArgs(method)).to.deep.equal({ | ||
foo: undefined, | ||
bar: undefined | ||
}) | ||
expect(getArgs(method)).to.deep.equal(map( | ||
'foo', undefined, | ||
'bar', undefined | ||
)) | ||
}) | ||
@@ -71,7 +86,30 @@ | ||
expect(getArgs(test)).to.deep.equal({ | ||
foo: undefined, | ||
bar: undefined | ||
}) | ||
expect(getArgs(test)).to.deep.equal(map( | ||
'foo', undefined, | ||
'bar', undefined | ||
)) | ||
}) | ||
it('should insert a dummy key for pattern matched segments', function () { | ||
function test ({foo, bar}, hello) {} | ||
expect(getArgs(test)).to.deep.equal(map( | ||
'__0', '{foo, bar}', | ||
'hello', undefined)) | ||
function test2 ([foo, bar], hello) {} | ||
expect(getArgs(test2)).to.deep.equal(map( | ||
'__0', '[foo, bar]', | ||
'hello', undefined)) | ||
}) | ||
it('should support multiple pattern matched segments', function () { | ||
function test ({hey, ho}, [foo, bar], hi) {} | ||
expect(getArgs(test)).to.deep.equal(map( | ||
'__0', '{hey, ho}', | ||
'__1', '[foo, bar]', | ||
'hi', undefined)) | ||
}) | ||
}) | ||
@@ -78,0 +116,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15541
6
322
109
3