Comparing version
18
base.js
@@ -12,8 +12,3 @@ var deprecate = require('deprecate') | ||
var nested = val instanceof Base ? val[VALUE] : val | ||
var parsed = nested instanceof Error ? nested : this.constructor.parse(nested, ...args) | ||
var value = parsed === undefined | ||
? new TypeError(`Value ${val} cannot be parsed as ${this.constructor.name}`) | ||
: parsed | ||
var value = parse.call(this, val, ...args) | ||
prop(this, VALUE, value) | ||
@@ -28,2 +23,13 @@ prop(this, 'isError', value instanceof Error, 'e') | ||
function parse (val, ...args) { | ||
var nested, value | ||
nested = val instanceof Base ? val[VALUE] : val | ||
value = nested instanceof Error ? nested : this.constructor.parse(nested, ...args) | ||
if (typeof value === 'undefined') { | ||
return new TypeError(`Value ${val} cannot be parsed as ${this.constructor.name}`) | ||
} | ||
return value instanceof Base ? value[VALUE] : value | ||
} | ||
Base.extract = function (opt) { | ||
@@ -30,0 +36,0 @@ return Base.prototype.extract.call(opt) |
{ | ||
"name": "stdopt", | ||
"version": "8.0.0", | ||
"version": "8.1.0", | ||
"description": "Wrap and validate optional values", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -38,2 +38,10 @@ var test = require('tape') | ||
var Nested = apply(function Nested (input) { | ||
Base.call(this, input) | ||
}) | ||
Nested.parse = function (input) { | ||
return Custom(input) | ||
} | ||
var Fail = apply(function Fail (input) { | ||
@@ -51,4 +59,6 @@ Base.call(this, input) | ||
t.equal(Custom('stuff').or('Custom').value(), 'Custom') | ||
t.equal(Nested('custom').value(), 'custom') | ||
t.throws(() => Custom(365).value(), /Custom should be string/) | ||
t.throws(() => Custom('stuff').value(), /Custom should contain/) | ||
t.throws(() => Nested('stuff').value(), /Custom should contain/) | ||
t.throws(() => Fail('stuff').or('bleh').value(), /No parser for Fail/) | ||
@@ -55,0 +65,0 @@ t.throws(() => Fail('stuff').value(), /No parser for Fail/) |
25164
1.63%352
3.83%