Comparing version
11
hash.js
@@ -21,3 +21,3 @@ var Base = require('./base') | ||
var prop, it, type, opt, err, descr, result | ||
var Type, prop, it, opt, err, descr, result | ||
result = {} | ||
@@ -32,11 +32,12 @@ | ||
if (it) { | ||
for (type of struct[prop]) { | ||
if (typeof type !== 'function') { | ||
for (Type of struct[prop]) { | ||
if (typeof Type !== 'function') { | ||
throw new Error('Type should be function') | ||
} | ||
opt = type(obj ? obj[prop] : obj) | ||
opt = new Type(obj ? obj[prop] : obj) | ||
if (opt.isValid) break | ||
} | ||
} else { | ||
opt = struct[prop](obj ? obj[prop] : obj) | ||
Type = struct[prop] | ||
opt = new Type(obj ? obj[prop] : obj) | ||
} | ||
@@ -43,0 +44,0 @@ |
16
list.js
@@ -19,3 +19,3 @@ var Base = require('./base') | ||
list.parse = function (l, type) { | ||
list.parse = function (l, Type) { | ||
if (!isArrayish(l)) { | ||
@@ -25,18 +25,18 @@ if (some(l).isValid) l = [l] | ||
} | ||
if (!type) return Array.from(l) | ||
if (!Type) return Array.from(l) | ||
var idx, t, opt, err, result | ||
var T, idx, opt, err, result | ||
result = [] | ||
for (idx = 0; idx < l.length; idx++) { | ||
if (type[Symbol.iterator]) { | ||
for (t of type) { | ||
if (typeof t !== 'function') { | ||
if (Type[Symbol.iterator]) { | ||
for (T of Type) { | ||
if (typeof T !== 'function') { | ||
throw new Error('Type should be function') | ||
} | ||
opt = t(l[idx]) | ||
opt = new T(l[idx]) | ||
if (opt.isValid) break | ||
} | ||
} else { | ||
opt = type(l[idx]) | ||
opt = new Type(l[idx]) | ||
} | ||
@@ -43,0 +43,0 @@ |
{ | ||
"name": "stdopt", | ||
"version": "10.2.0", | ||
"version": "10.2.1", | ||
"description": "Wrap and validate optional values", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
var { hash, list, nothing, number, string } = require('../') | ||
var { prop } = require('stdprop') | ||
var Base = require('../base') | ||
var test = require('tape') | ||
class Custom extends Base { | ||
static parse (val) { | ||
return val | ||
} | ||
} | ||
var Item = hash.struct({ | ||
@@ -11,2 +18,3 @@ type: string, | ||
var Struct = { | ||
custom: Custom, | ||
description: string, | ||
@@ -18,2 +26,3 @@ extra: [nothing, string], | ||
var invalid = { | ||
custom: 1, | ||
description: 'invalid struct', | ||
@@ -28,2 +37,3 @@ items: [ | ||
ping: 'pong', | ||
custom: new Custom(false), | ||
description: 'valid struct', | ||
@@ -38,2 +48,3 @@ items: [ | ||
var expected = { | ||
custom: false, | ||
description: 'valid struct', | ||
@@ -40,0 +51,0 @@ items: [ |
27735
0.78%447
2.52%