Browse Source

add anyOf alias, deprecating oneOf

master
Daniel Cousens 6 years ago
parent
commit
0a2d741d48
  1. 6
      README.md
  2. 11
      index.js

6
README.md

@ -35,7 +35,11 @@ typeforce(typeforce.maybe(typeforce.Number), 2)
typeforce(typeforce.maybe(typeforce.Number), null) typeforce(typeforce.maybe(typeforce.Number), null)
// sum types // sum types
typeforce(typeforce.oneOf('String', 'Number')) typeforce(typeforce.anyOf('String', 'Number'), 2)
typeforce(typeforce.allOf({ x: typeforce.Number }, { y: typeforce.Number }), {
x: 1,
y: 2
})
// value types // value types
typeforce(typeforce.value(3.14), 3.14) typeforce(typeforce.value(3.14), 3.14)

11
index.js

@ -132,10 +132,10 @@ var TYPES = {
return _object return _object
}, },
oneOf: function oneOf () { anyOf: function anyOf () {
var types = [].slice.call(arguments).map(compile) var types = [].slice.call(arguments).map(compile)
function _oneOf (value, strict) { function _anyOf (value, strict) {
return types.some(function (type) { return types.some(function (type) {
try { try {
return typeforce(type, value, strict) return typeforce(type, value, strict)
@ -144,9 +144,9 @@ var TYPES = {
} }
}) })
} }
_oneOf.toJSON = function () { return types.map(tfJSON).join('|') } _anyOf.toJSON = function () { return types.map(tfJSON).join('|') }
return _oneOf return _anyOf
}, },
allOf: function allOf () { allOf: function allOf () {
@ -206,6 +206,9 @@ var TYPES = {
} }
} }
// TODO: deprecate
TYPES.oneOf = TYPES.anyOf
function compile (type) { function compile (type) {
if (NATIVE.String(type)) { if (NATIVE.String(type)) {
if (type[0] === '?') return TYPES.maybe(type.slice(1)) if (type[0] === '?') return TYPES.maybe(type.slice(1))

Loading…
Cancel
Save