Comparing version
@@ -7,3 +7,3 @@ var deprecate = require('deprecate') | ||
function Base (val) { | ||
function Base (val, ...args) { | ||
if (typeof this.constructor.parse !== 'function') { | ||
@@ -14,3 +14,3 @@ throw new TypeError('No parser for ' + this.constructor.name) | ||
var nested = val instanceof Base ? val[VALUE] : val | ||
var parsed = nested instanceof Error ? nested : this.constructor.parse(nested) | ||
var parsed = nested instanceof Error ? nested : this.constructor.parse(nested, ...args) | ||
var value = parsed === undefined | ||
@@ -17,0 +17,0 @@ ? new TypeError(`Value ${val} cannot be parsed as ${this.constructor.name}`) |
38
hash.js
var Base = require('./base') | ||
var apply = require('./util/apply') | ||
var isArrayish = require('is-arrayish') | ||
function hash (o) { | ||
Base.call(this, o) | ||
function hash (obj, struct) { | ||
if (struct && typeof struct !== 'object') { | ||
throw new Error('Struct should be object') | ||
} | ||
Base.call(this, obj, struct) | ||
} | ||
hash.parse = function (o) { | ||
if (isArrayish(o)) { | ||
return new TypeError(`Value ${o} is a list, should be hash`) | ||
hash.struct = function (s) { | ||
return function (obj) { | ||
return new hash(obj, s) // eslint-disable-line new-cap | ||
} | ||
if (typeof o === 'object') { | ||
return o | ||
} | ||
hash.parse = function (obj, struct) { | ||
if (typeof obj !== 'object') return | ||
if (!struct) return obj | ||
var prop, opt, result | ||
result = {} | ||
for (prop of Object.keys(struct)) { | ||
if (typeof struct[prop] !== 'function') { | ||
throw new Error('Type should be function') | ||
} | ||
opt = struct[prop](obj[prop]) | ||
if (opt.isError) { | ||
var err = opt.extract() | ||
return new err.constructor(`${prop} -> ${err.message}`) | ||
} else { | ||
result[prop] = opt.value() | ||
} | ||
} | ||
return result | ||
} | ||
module.exports = apply(hash) |
33
list.js
@@ -5,11 +5,34 @@ var Base = require('./base') | ||
function list (l) { | ||
Base.call(this, l) | ||
function list (l, type) { | ||
if (type && typeof type !== 'function') { | ||
throw new Error('Type should be function') | ||
} | ||
Base.call(this, l, type) | ||
} | ||
list.parse = function (l) { | ||
if (Array.isArray(l)) return l | ||
if (isArrayish(l)) return Array.from(l) | ||
list.of = function (type) { | ||
return function (l) { | ||
return new list(l, type) // eslint-disable-line new-cap | ||
} | ||
} | ||
list.parse = function (l, type) { | ||
if (!isArrayish(l)) return | ||
if (!type) return Array.from(l) | ||
var idx, opt, result | ||
result = [] | ||
for (idx = 0; idx < l.length; idx++) { | ||
opt = type(l[idx]) | ||
if (opt.isError) { | ||
var err = opt.extract() | ||
return new err.constructor(`[${idx}] -> ${err.message}`) | ||
} else { | ||
result.push(opt.value()) | ||
} | ||
} | ||
return result | ||
} | ||
module.exports = apply(list) |
{ | ||
"name": "stdopt", | ||
"version": "7.1.1", | ||
"version": "8.0.0", | ||
"description": "Wrap and validate optional values", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
var test = require('tape') | ||
test('opt', t => { | ||
var { opt, string } = require('../') | ||
var { opt } = require('../') | ||
t.equal(opt('value').value(), 'value') | ||
@@ -15,8 +14,2 @@ t.throws(() => opt(null).value(), /Value should be defined/) | ||
t.ok(opt('some').use().isValid) | ||
t.equal(opt(opt('some')).value(), 'some') | ||
t.equal(opt(opt(opt('other'))).value(), 'other') | ||
t.equal(opt(string('value')).value(), 'value') | ||
t.throws(() => opt(string()).value(), /Value undefined cannot be parsed as string/) | ||
t.throws(() => string(opt()).value(), /Value should be defined/) | ||
t.end() | ||
@@ -23,0 +16,0 @@ }) |
@@ -7,8 +7,6 @@ var { hash } = require('../') | ||
t.deepEqual(hash('barf').or({}).value(), {}) | ||
t.deepEqual(hash([]).or({}).value(), {}) | ||
t.deepEqual(hash({ 0: 1, length: 1 }).or({}).value(), {}) | ||
t.deepEqual(hash([]).or({}).value(), []) | ||
t.deepEqual(hash({ length: 1 }).value(), { length: 1 }) | ||
t.throws(() => hash([1, 2]).value(), /Value 1,2 is a list, should be hash/) | ||
t.throws(() => hash('no').value(), /Value no cannot be parsed as hash/) | ||
t.end() | ||
}) |
24761
8.69%21
10.53%339
31.4%