patroon 0.4.3 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -192,8 +192,8 @@ builtin node error constructors in this example.
192
192
 
193
193
  ```js ./tape-test
194
194
  patroon(
195
- TypeError, 'is a type error',
196
- Error, 'is an error'
195
+ instanceOf(TypeError), 'is a type error',
196
+ instanceOf(Error), 'is an error'
197
197
  )(new Error())
198
198
  ```
199
199
  ```
@@ -213,8 +213,8 @@ Because of this you can match a TypeError with an Error.
213
213
 
214
214
  ```js ./tape-test
215
215
  patroon(
216
- Error, 'matches on error',
217
- TypeError, 'matches on type error'
216
+ instanceOf(Error), 'matches on error',
217
+ instanceOf(TypeError), 'matches on type error'
218
218
  )(new TypeError())
219
219
  ```
220
220
  ```
@@ -226,9 +226,9 @@ Here you should use the every helper.
226
226
 
227
227
  ```js ./tape-test
228
228
  patroon(
229
- every(TypeError, { value: 20 }), 'type error where value is 20',
230
- every(Error, { value: 30 }), 'error where value is 30',
231
- every(Error, { value: 20 }), 'error where value is 20'
229
+ every(instanceOf(TypeError), { value: 20 }), 'type error where value is 20',
230
+ every(instanceOf(Error), { value: 30 }), 'error where value is 30',
231
+ every(instanceOf(Error), { value: 20 }), 'error where value is 20'
232
232
  )(Object.assign(new TypeError(), { value: 20 }))
233
233
  ```
234
234
  ```
@@ -251,6 +251,35 @@ patroon([], 'is array')([])
251
251
  patroon(Array, 'is array')([])
252
252
  ```
253
253
 
254
+ A less intuitive case:
255
+
256
+ ```js ./tape-test > /dev/null
257
+ patroon({}, 'is object')([])
258
+ patroon([], 'is array')({})
259
+ ```
260
+
261
+ Patroon allows this because Arrays can have properties defined.
262
+
263
+ ```js ./tape-test > /dev/null
264
+ const array = []
265
+ array.prop = 42
266
+
267
+ patroon({prop: _}, 'has prop')(array)
268
+ ```
269
+
270
+ The other way around is also allowed even if it seems weird.
271
+
272
+ ```js ./tape-test > /dev/null
273
+ const object = {0: 42}
274
+ patroon([42], 'has 0th')(object)
275
+ ```
276
+
277
+ If you do not desire this loose behavior you can use a predicate to make sure
278
+ something is an array or object.
279
+
280
+ ```js ./tape-test > /dev/null
281
+ patroon(Array.isArray, 'is array')([])
282
+ ```
254
283
 
255
284
  ### Reference
256
285
 
@@ -259,7 +288,7 @@ helper.
259
288
 
260
289
  ```js ./tape-test
261
290
  patroon(
262
- Error, 'is an instance of Error',
291
+ instanceOf(Error), 'is an instance of Error',
263
292
  reference(Error), 'is the Error constructor'
264
293
  )(Error)
265
294
  ```
@@ -488,12 +517,12 @@ const oneIsTwo = patroon(1, 2)
488
517
  oneIsTwo(3)
489
518
  ```
490
519
  ```
491
- /home/ant/projects/patroon/src/index.js:88
492
- if (isNil(found)) { throw new NoMatchError(`Not able to match any pattern for value ${JSON.stringify(args)}`) }
493
- ^
520
+ [ 3 ]
521
+ /home/ant/projects/patroon/src/index.js:92
522
+ throw error
523
+ ^
494
524
 
495
- NoMatchError: Not able to match any pattern for value [3]
496
- at /home/ant/projects/patroon/src/index.js:88:31
525
+ NoMatchError: Not able to match any pattern for arguments
497
526
  ```
498
527
 
499
528
  #### UnevenArgumentCountError
@@ -504,12 +533,12 @@ Another error that occurs is when the patroon function is not used correctly.
504
533
  patroon(1)
505
534
  ```
506
535
  ```
507
- /home/ant/projects/patroon/src/index.js:81
536
+ /home/ant/projects/patroon/src/index.js:79
508
537
  if (!isEven(list.length)) { throw new UnevenArgumentCountError('Patroon should have an even amount of arguments.') }
509
538
  ^
510
539
 
511
540
  UnevenArgumentCountError: Patroon should have an even amount of arguments.
512
- at patroon (/home/ant/projects/patroon/src/index.js:81:37)
541
+ at patroon (/home/ant/projects/patroon/src/index.js:79:37)
513
542
  ```
514
543
 
515
544
  #### PatroonError
@@ -517,7 +546,7 @@ UnevenArgumentCountError: Patroon should have an even amount of arguments.
517
546
  All errors patroon produces can be matched against the PatroonError using `instanceof`.
518
547
 
519
548
  ```js ./tape-test
520
- const isPatroonError = patroon(PatroonError, 'patroon is causing an error')
549
+ const isPatroonError = patroon(instanceOf(PatroonError), 'patroon is causing an error')
521
550
 
522
551
  isPatroonError(new NoMatchError())
523
552
  isPatroonError(new UnevenArgumentCountError())
@@ -545,7 +574,7 @@ npx nyc npm t | npx tap-nyc
545
574
  npx nyc check-coverage
546
575
  ```
547
576
  ```
548
- > patroon@0.4.2 test
577
+ > patroon@1.0.0 test
549
578
  > tape ./src/index.test.js
550
579
  -------------|---------|----------|---------|---------|-------------------
551
580
  File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
@@ -559,7 +588,7 @@ npx nyc check-coverage
559
588
  total: 15
560
589
  passing: 15
561
590
 
562
- duration: 722ms
591
+ duration: 1s
563
592
 
564
593
  ```
565
594
 
package/README.mz CHANGED
@@ -142,8 +142,8 @@ builtin node error constructors in this example.
142
142
 
143
143
  ```js ./tape-test
144
144
  patroon(
145
- TypeError, 'is a type error',
146
- Error, 'is an error'
145
+ instanceOf(TypeError), 'is a type error',
146
+ instanceOf(Error), 'is an error'
147
147
  )(new Error())
148
148
  ```
149
149
 
@@ -157,8 +157,8 @@ Because of this you can match a TypeError with an Error.
157
157
 
158
158
  ```js ./tape-test
159
159
  patroon(
160
- Error, 'matches on error',
161
- TypeError, 'matches on type error'
160
+ instanceOf(Error), 'matches on error',
161
+ instanceOf(TypeError), 'matches on type error'
162
162
  )(new TypeError())
163
163
  ```
164
164
 
@@ -167,9 +167,9 @@ Here you should use the every helper.
167
167
 
168
168
  ```js ./tape-test
169
169
  patroon(
170
- every(TypeError, { value: 20 }), 'type error where value is 20',
171
- every(Error, { value: 30 }), 'error where value is 30',
172
- every(Error, { value: 20 }), 'error where value is 20'
170
+ every(instanceOf(TypeError), { value: 20 }), 'type error where value is 20',
171
+ every(instanceOf(Error), { value: 30 }), 'error where value is 30',
172
+ every(instanceOf(Error), { value: 20 }), 'error where value is 20'
173
173
  )(Object.assign(new TypeError(), { value: 20 }))
174
174
  ```
175
175
 
@@ -189,6 +189,35 @@ patroon([], 'is array')([])
189
189
  patroon(Array, 'is array')([])
190
190
  ```
191
191
 
192
+ A less intuitive case:
193
+
194
+ ```js ./tape-test > /dev/null
195
+ patroon({}, 'is object')([])
196
+ patroon([], 'is array')({})
197
+ ```
198
+
199
+ Patroon allows this because Arrays can have properties defined.
200
+
201
+ ```js ./tape-test > /dev/null
202
+ const array = []
203
+ array.prop = 42
204
+
205
+ patroon({prop: _}, 'has prop')(array)
206
+ ```
207
+
208
+ The other way around is also allowed even if it seems weird.
209
+
210
+ ```js ./tape-test > /dev/null
211
+ const object = {0: 42}
212
+ patroon([42], 'has 0th')(object)
213
+ ```
214
+
215
+ If you do not desire this loose behavior you can use a predicate to make sure
216
+ something is an array or object.
217
+
218
+ ```js ./tape-test > /dev/null
219
+ patroon(Array.isArray, 'is array')([])
220
+ ```
192
221
 
193
222
  ### Reference
194
223
 
@@ -197,7 +226,7 @@ helper.
197
226
 
198
227
  ```js ./tape-test
199
228
  patroon(
200
- Error, 'is an instance of Error',
229
+ instanceOf(Error), 'is an instance of Error',
201
230
  reference(Error), 'is the Error constructor'
202
231
  )(Error)
203
232
  ```
@@ -390,7 +419,7 @@ patroon(1)
390
419
  All errors patroon produces can be matched against the PatroonError using `instanceof`.
391
420
 
392
421
  ```js ./tape-test
393
- const isPatroonError = patroon(PatroonError, 'patroon is causing an error')
422
+ const isPatroonError = patroon(instanceOf(PatroonError), 'patroon is causing an error')
394
423
 
395
424
  isPatroonError(new NoMatchError())
396
425
  isPatroonError(new UnevenArgumentCountError())
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "patroon",
3
- "version": "0.4.3",
3
+ "version": "1.0.0",
4
4
  "description": "Pattern matching library",
5
5
  "repository": "github:bas080/patroon",
6
6
  "main": "./src/index.js",
package/src/helpers.js CHANGED
@@ -8,10 +8,6 @@ const toPairs = items => {
8
8
 
9
9
  const isNil = x => !(x != null)
10
10
 
11
- function isConstructor (func) {
12
- return Boolean(func && typeof func === 'function' && func.prototype && func.prototype.constructor)
13
- }
14
-
15
11
  function isDefined (x) {
16
12
  return x != null
17
13
  }
@@ -24,7 +20,6 @@ const is = Ctor => instance => isDefined(instance) && (instance.constructor ===
24
20
  module.exports = {
25
21
  isDefined,
26
22
  always: x => () => x,
27
- isConstructor,
28
23
  is,
29
24
  toPairs,
30
25
  isNil,
package/src/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const { mapLeaves, path, PathError } = require('./walkable')()
2
- const { isConstructor, isFunction, equals, T, is, tryCatch, isEven, isNil, toPairs, always } = require('./helpers')
2
+ const { isFunction, equals, T, is, tryCatch, isEven, isNil, toPairs, always } = require('./helpers')
3
3
 
4
4
  const deprecated = (fn, message) => (...args) => {
5
5
  console.error(`[patroon] deprecated: ${message}`)
@@ -41,8 +41,6 @@ const predicate = pattern => {
41
41
  const normalize = (value, pth) => {
42
42
  if (isRegExp(value)) return arg => value.test(arg)
43
43
 
44
- if (isConstructor(value)) { return is(value) }
45
-
46
44
  if (isFunction(value)) { return arg => value(path(pth, arg)) }
47
45
 
48
46
  return arg => equals(path(pth, arg), value)
@@ -85,7 +83,14 @@ const patroon = (...list) => {
85
83
  return (...args) => {
86
84
  const found = patterns.find(([matches]) => matches(...args))
87
85
 
88
- if (isNil(found)) { throw new NoMatchError(`Not able to match any pattern for value ${JSON.stringify(args)}`) }
86
+ if (isNil(found)) {
87
+ const error = new NoMatchError('Not able to match any pattern for arguments')
88
+ error.arguments = args
89
+
90
+ console.error(args)
91
+
92
+ throw error
93
+ }
89
94
 
90
95
  return found[1](...args)
91
96
  }
@@ -95,6 +100,10 @@ function reference (any) {
95
100
  return value => any === value
96
101
  }
97
102
 
103
+ function instanceOf (ctor) {
104
+ return value => value instanceof ctor
105
+ }
106
+
98
107
  module.exports = Object.assign(patroon, {
99
108
  NoMatchError,
100
109
  UnevenArgumentCountError,
@@ -107,5 +116,6 @@ module.exports = Object.assign(patroon, {
107
116
  reference,
108
117
  typed,
109
118
  t: typed,
119
+ instanceOf,
110
120
  _: T
111
121
  })
package/src/index.test.js CHANGED
@@ -2,6 +2,7 @@ const { test } = require('tape')
2
2
  const { check, gen } = require('tape-check')
3
3
  const {
4
4
  reference,
5
+ instanceOf,
5
6
  typed,
6
7
  t,
7
8
  ref,
@@ -39,9 +40,9 @@ test('Match using reference', t => {
39
40
 
40
41
  test('Matches on type and value', t => {
41
42
  patroon(
42
- every(Error, { x: 20 }), () => t.fail(),
43
- every(Error, { x: 10 }), () => t.end(),
44
- every(Error), () => t.fail()
43
+ every(instanceOf(Error), { x: 20 }), () => t.fail(),
44
+ every(instanceOf(Error), { x: 10 }), () => t.end(),
45
+ instanceOf(Error), () => t.fail()
45
46
  )(Object.assign(new Error(), { x: 10 }))
46
47
  })
47
48
 
package/tape-test CHANGED
@@ -4,6 +4,7 @@ cat <(echo '
4
4
  const {
5
5
  _,
6
6
  every,
7
+ instanceOf,
7
8
  some,
8
9
  multi,
9
10
  reference,