Skip to content

Commit

Permalink
Fix printing recursive value or pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
bas080 committed Dec 3, 2021
1 parent 44d78ff commit b9f58e4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ oneIsTwo(3)
```
```
[ 3 ]
/home/ant/projects/patroon/src/index.js:92
/home/ant/projects/patroon/src/index.js:93
throw error
^
Expand All @@ -534,12 +534,12 @@ Another error that occurs is when the patroon function is not used correctly.
patroon(1)
```
```
/home/ant/projects/patroon/src/index.js:79
/home/ant/projects/patroon/src/index.js:80
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:79:37)
at patroon (/home/ant/projects/patroon/src/index.js:80:37)
```

#### PatroonError
Expand Down Expand Up @@ -594,10 +594,10 @@ npx nyc check-coverage
walkable.js | 100 | 100 | 100 | 100 |
-------------|---------|----------|---------|---------|-------------------
total: 16
passing: 16
total: 19
passing: 19
duration: 1.6s
duration: 1.4s
```

Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { mapLeaves, path, PathError } = require('./walkable')()
const { isFunction, equals, T, is, tryCatch, isEven, isNil, toPairs, always } = require('./helpers')
const { inspect } = require('util')

const deprecated = (fn, message) => (...args) => {
console.error(`[patroon] deprecated: ${message}`)
Expand Down Expand Up @@ -87,7 +88,7 @@ const patroon = (...list) => {
const error = new NoMatchError('Not able to match any pattern for arguments')
error.arguments = args

console.error(args)
console.error(inspect(args))

throw error
}
Expand Down
19 changes: 19 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,22 @@ test('Deprecated functions', ts => {
console.error = consoleError
ts.end()
})

// Circular reference
const circular = {}
circular.a = circular

test('At the moment recursive patterns throw and print', t => {
t.plan(1)
t.throws(() => patroon(circular, true)({}))
})

test('Throws and prints because path is not defined', t => {
t.plan(1)
t.throws(() => patroon({ b: _ }, true)(circular))
})

test('Does not throw when value is recursive', t => {
t.plan(1)
t.ok(patroon({ a: _ }, true)(circular))
})
2 changes: 1 addition & 1 deletion src/walkable.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function path (pth, v, errorData) {

if (!(key in v)) {
const error = new PathError(
`Cannot read property '${JSON.stringify(pth)}' of ${JSON.stringify(v)}`)
`Cannot read path ${pth}.`)

Object.assign(error, errorData)

Expand Down

0 comments on commit b9f58e4

Please sign in to comment.