Skip to content

Commit

Permalink
Remove the constructor detection
Browse files Browse the repository at this point in the history
This does deprecate part of the patroon library and might require some
fixes to your codebase.
  • Loading branch information
bas080 committed Nov 30, 2021
1 parent b78f721 commit a417db9
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 37 deletions.
59 changes: 44 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ builtin node error constructors in this example.

```js ./tape-test
patroon(
TypeError, 'is a type error',
Error, 'is an error'
instanceOf(TypeError), 'is a type error',
instanceOf(Error), 'is an error'
)(new Error())
```
```
Expand All @@ -213,8 +213,8 @@ Because of this you can match a TypeError with an Error.

```js ./tape-test
patroon(
Error, 'matches on error',
TypeError, 'matches on type error'
instanceOf(Error), 'matches on error',
instanceOf(TypeError), 'matches on type error'
)(new TypeError())
```
```
Expand All @@ -226,9 +226,9 @@ Here you should use the every helper.

```js ./tape-test
patroon(
every(TypeError, { value: 20 }), 'type error where value is 20',
every(Error, { value: 30 }), 'error where value is 30',
every(Error, { value: 20 }), 'error where value is 20'
every(instanceOf(TypeError), { value: 20 }), 'type error where value is 20',
every(instanceOf(Error), { value: 30 }), 'error where value is 30',
every(instanceOf(Error), { value: 20 }), 'error where value is 20'
)(Object.assign(new TypeError(), { value: 20 }))
```
```
Expand All @@ -251,6 +251,35 @@ patroon([], 'is array')([])
patroon(Array, 'is array')([])
```

A less intuitive case:

```js ./tape-test > /dev/null
patroon({}, 'is object')([])
patroon([], 'is array')({})
```

Patroon allows this because Arrays can have properties defined.

```js ./tape-test > /dev/null
const array = []
array.prop = 42

patroon({prop: _}, 'has prop')(array)
```

The other way around is also allowed even if it seems weird.

```js ./tape-test > /dev/null
const object = {0: 42}
patroon([42], 'has 0th')(object)
```

If you do not desire this loose behavior you can use a predicate to make sure
something is an array or object.

```js ./tape-test > /dev/null
patroon(Array.isArray, 'is array')([])
```

### Reference

Expand All @@ -259,7 +288,7 @@ helper.

```js ./tape-test
patroon(
Error, 'is an instance of Error',
instanceOf(Error), 'is an instance of Error',
reference(Error), 'is the Error constructor'
)(Error)
```
Expand Down Expand Up @@ -488,12 +517,12 @@ const oneIsTwo = patroon(1, 2)
oneIsTwo(3)
```
```
/home/ant/projects/patroon/src/index.js:88
/home/ant/projects/patroon/src/index.js:86
if (isNil(found)) { throw new NoMatchError(`Not able to match any pattern for value ${JSON.stringify(args)}`) }
^
NoMatchError: Not able to match any pattern for value [3]
at /home/ant/projects/patroon/src/index.js:88:31
at /home/ant/projects/patroon/src/index.js:86:31
```

#### UnevenArgumentCountError
Expand All @@ -504,20 +533,20 @@ Another error that occurs is when the patroon function is not used correctly.
patroon(1)
```
```
/home/ant/projects/patroon/src/index.js:81
/home/ant/projects/patroon/src/index.js:79
if (!isEven(list.length)) { throw new UnevenArgumentCountError('Patroon should have an even amount of arguments.') }
^
UnevenArgumentCountError: Patroon should have an even amount of arguments.
at patroon (/home/ant/projects/patroon/src/index.js:81:37)
at patroon (/home/ant/projects/patroon/src/index.js:79:37)
```

#### PatroonError

All errors patroon produces can be matched against the PatroonError using `instanceof`.

```js ./tape-test
const isPatroonError = patroon(PatroonError, 'patroon is causing an error')
const isPatroonError = patroon(instanceOf(PatroonError), 'patroon is causing an error')

isPatroonError(new NoMatchError())
isPatroonError(new UnevenArgumentCountError())
Expand Down Expand Up @@ -545,7 +574,7 @@ npx nyc npm t | npx tap-nyc
npx nyc check-coverage
```
```
> patroon@0.4.2 test
> patroon@0.4.3 test
> tape ./src/index.test.js
-------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
Expand All @@ -559,7 +588,7 @@ npx nyc check-coverage
total: 15
passing: 15
duration: 722ms
duration: 1.3s
```

Expand Down
22 changes: 11 additions & 11 deletions README.mz
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ builtin node error constructors in this example.

```js ./tape-test
patroon(
TypeError, 'is a type error',
Error, 'is an error'
instanceOf(TypeError), 'is a type error',
instanceOf(Error), 'is an error'
)(new Error())
```

Expand All @@ -157,8 +157,8 @@ Because of this you can match a TypeError with an Error.

```js ./tape-test
patroon(
Error, 'matches on error',
TypeError, 'matches on type error'
instanceOf(Error), 'matches on error',
instanceOf(TypeError), 'matches on type error'
)(new TypeError())
```

Expand All @@ -167,9 +167,9 @@ Here you should use the every helper.

```js ./tape-test
patroon(
every(TypeError, { value: 20 }), 'type error where value is 20',
every(Error, { value: 30 }), 'error where value is 30',
every(Error, { value: 20 }), 'error where value is 20'
every(instanceOf(TypeError), { value: 20 }), 'type error where value is 20',
every(instanceOf(Error), { value: 30 }), 'error where value is 30',
every(instanceOf(Error), { value: 20 }), 'error where value is 20'
)(Object.assign(new TypeError(), { value: 20 }))
```

Expand Down Expand Up @@ -208,8 +208,8 @@ patroon({prop: _}, 'has prop')(array)
The other way around is also allowed even if it seems weird.

```js ./tape-test > /dev/null
const object = {0: 'wow'}
patroon(['wow'], 'has 0th')(object)
const object = {0: 42}
patroon([42], 'has 0th')(object)
```

If you do not desire this loose behavior you can use a predicate to make sure
Expand All @@ -226,7 +226,7 @@ helper.

```js ./tape-test
patroon(
Error, 'is an instance of Error',
instanceOf(Error), 'is an instance of Error',
reference(Error), 'is the Error constructor'
)(Error)
```
Expand Down Expand Up @@ -419,7 +419,7 @@ patroon(1)
All errors patroon produces can be matched against the PatroonError using `instanceof`.

```js ./tape-test
const isPatroonError = patroon(PatroonError, 'patroon is causing an error')
const isPatroonError = patroon(instanceOf(PatroonError), 'patroon is causing an error')

isPatroonError(new NoMatchError())
isPatroonError(new UnevenArgumentCountError())
Expand Down
5 changes: 0 additions & 5 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ const toPairs = items => {

const isNil = x => !(x != null)

function isConstructor (func) {
return Boolean(func && typeof func === 'function' && func.prototype && func.prototype.constructor)
}

function isDefined (x) {
return x != null
}
Expand All @@ -24,7 +20,6 @@ const is = Ctor => instance => isDefined(instance) && (instance.constructor ===
module.exports = {
isDefined,
always: x => () => x,
isConstructor,
is,
toPairs,
isNil,
Expand Down
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { mapLeaves, path, PathError } = require('./walkable')()
const { isConstructor, isFunction, equals, T, is, tryCatch, isEven, isNil, toPairs, always } = require('./helpers')
const { isFunction, equals, T, is, tryCatch, isEven, isNil, toPairs, always } = require('./helpers')

const deprecated = (fn, message) => (...args) => {
console.error(`[patroon] deprecated: ${message}`)
Expand Down Expand Up @@ -41,8 +41,6 @@ const predicate = pattern => {
const normalize = (value, pth) => {
if (isRegExp(value)) return arg => value.test(arg)

if (isConstructor(value)) { return is(value) }

if (isFunction(value)) { return arg => value(path(pth, arg)) }

return arg => equals(path(pth, arg), value)
Expand Down Expand Up @@ -95,6 +93,10 @@ function reference (any) {
return value => any === value
}

function instanceOf (ctor) {
return value => value instanceof ctor
}

module.exports = Object.assign(patroon, {
NoMatchError,
UnevenArgumentCountError,
Expand All @@ -107,5 +109,6 @@ module.exports = Object.assign(patroon, {
reference,
typed,
t: typed,
instanceOf,
_: T
})
7 changes: 4 additions & 3 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { test } = require('tape')
const { check, gen } = require('tape-check')
const {
reference,
instanceOf,
typed,
t,
ref,
Expand Down Expand Up @@ -39,9 +40,9 @@ test('Match using reference', t => {

test('Matches on type and value', t => {
patroon(
every(Error, { x: 20 }), () => t.fail(),
every(Error, { x: 10 }), () => t.end(),
every(Error), () => t.fail()
every(instanceOf(Error), { x: 20 }), () => t.fail(),
every(instanceOf(Error), { x: 10 }), () => t.end(),
instanceOf(Error), () => t.fail()
)(Object.assign(new Error(), { x: 10 }))
})

Expand Down
1 change: 1 addition & 0 deletions tape-test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ cat <(echo '
const {
_,
every,
instanceOf,
some,
multi,
reference,
Expand Down

0 comments on commit a417db9

Please sign in to comment.