Skip to content

Commit

Permalink
break out duck typing to a new type
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Aug 10, 2015
1 parent 38db338 commit 3c8499b
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ var otherTypes = {
}
},

quacksLike (type) {

This comment has been minimized.

Copy link
@weilu

weilu Aug 12, 2015

I was expecting this to iterate the own property of the expected type and check that the value has the same set of properties.

This comment has been minimized.

Copy link
@dcousens

dcousens Aug 12, 2015

Author Owner

That is an interesting idea :), I'm not against that actually. It would definitely give a stricter meaning and at least resolve some of the concerns I had around this in our usage.

function quacksLike (value, strict) {
return type === getValueTypeName(value)
}
quacksLike.toJSON = () => type

return quacksLike
},

value (expected) {
return function value (actual) {
return actual === expected
Expand All @@ -123,7 +132,7 @@ function compile (type) {
return otherTypes.maybe(compile(type))
}

return nativeTypes[type] || type
return nativeTypes[type] || otherTypes.quacksLike(type)

} else if (nativeTypes.Object(type)) {
if (nativeTypes.Array(type)) {
Expand All @@ -150,11 +159,7 @@ function typeforce (type, value, strict) {
}

// JIT
type = compile(type)
if (!nativeTypes.String(type)) return typeforce(type, value, strict)
if (type === getValueTypeName(value)) return true

throw new TypeError(tfErrorString(type, value))
return typeforce(compile(type), value, strict)
}

// assign all types to typeforce function
Expand Down

0 comments on commit 3c8499b

Please sign in to comment.