patroon 1.5.0 → 1.5.2
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/CHANGELOG.md +11 -0
- package/CONTRIBUTING.md +9 -9
- package/README.md +43 -43
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -4,8 +4,19 @@ All notable changes to this project will be documented in this file. Dates are d
|
|
|
4
4
|
|
|
5
5
|
Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
|
|
6
6
|
|
|
7
|
+
#### [v1.5.2](https://github.com/bas080/patroon/compare/v1.5.1...v1.5.2)
|
|
8
|
+
|
|
9
|
+
- Update tape dev dependency [`841b1fb`](https://github.com/bas080/patroon/commit/841b1fb54c8d0197389a9649471dc9114a103b4f)
|
|
10
|
+
- Update tape package to 5.7.0 [`afd1c5c`](https://github.com/bas080/patroon/commit/afd1c5cb3762d0cd6f624c4b76de6bdc1813dfcb)
|
|
11
|
+
|
|
12
|
+
#### [v1.5.1](https://github.com/bas080/patroon/compare/v1.5.0...v1.5.1)
|
|
13
|
+
|
|
14
|
+
> 9 June 2023
|
|
15
|
+
|
|
7
16
|
#### [v1.5.0](https://github.com/bas080/patroon/compare/v1.4.2...v1.5.0)
|
|
8
17
|
|
|
18
|
+
> 2 June 2023
|
|
19
|
+
|
|
9
20
|
- Set license from GPL to MIT [`ac32aee`](https://github.com/bas080/patroon/commit/ac32aeea3ef6ad0acd88370274a4886a8f84325b)
|
|
10
21
|
|
|
11
22
|
#### [v1.4.2](https://github.com/bas080/patroon/compare/v1.4.1...v1.4.2)
|
package/CONTRIBUTING.md
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
|
|
16
16
|
Standard is good enough.
|
|
17
17
|
|
|
18
|
-
```bash
|
|
18
|
+
```bash
|
|
19
19
|
npx standard
|
|
20
20
|
```
|
|
21
21
|
|
|
@@ -23,32 +23,32 @@ npx standard
|
|
|
23
23
|
|
|
24
24
|
We generate the contributors list using node and store it in [memplate][7].
|
|
25
25
|
|
|
26
|
-
```js
|
|
26
|
+
```js
|
|
27
27
|
require('./package.json').contributors.reduce((acc, {name, url, email}) =>
|
|
28
28
|
`${acc}- **${name}** *${url}*\n`, '')
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
We also generate the table of contents and store it in memplate.
|
|
32
32
|
|
|
33
|
-
```bash
|
|
33
|
+
```bash
|
|
34
34
|
npx markdown-toc --no-firsth1 --maxdepth 4 README.mz
|
|
35
35
|
```
|
|
36
36
|
|
|
37
37
|
For the tests in the README.mz to work we also need to install patroon.
|
|
38
38
|
|
|
39
|
-
```bash
|
|
39
|
+
```bash
|
|
40
40
|
npm link
|
|
41
41
|
npm link patroon
|
|
42
42
|
```
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
added 1 package, and audited 3 packages in 660ms
|
|
46
46
|
|
|
47
47
|
found 0 vulnerabilities
|
|
48
48
|
|
|
49
|
-
added 1 package, and audited
|
|
49
|
+
added 1 package, and audited 95 packages in 811ms
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
65 packages are looking for funding
|
|
52
52
|
run `npm fund` for details
|
|
53
53
|
|
|
54
54
|
found 0 vulnerabilities
|
|
@@ -57,7 +57,7 @@ found 0 vulnerabilities
|
|
|
57
57
|
We then use memplate to template the README. Then we run [markatzea][6] to run
|
|
58
58
|
the examples and show the output of those.
|
|
59
59
|
|
|
60
|
-
```bash
|
|
60
|
+
```bash
|
|
61
61
|
memplate < README.mz | markatzea > README.md
|
|
62
62
|
echo 'Documentation generated successfully.' 1>&2
|
|
63
63
|
```
|
|
@@ -67,7 +67,7 @@ echo 'Documentation generated successfully.' 1>&2
|
|
|
67
67
|
The [changelog][changelog] is generated using the useful [auto-changelog][auto-changelog]
|
|
68
68
|
project.
|
|
69
69
|
|
|
70
|
-
```bash
|
|
70
|
+
```bash
|
|
71
71
|
npx auto-changelog -p
|
|
72
72
|
```
|
|
73
73
|
|
package/README.md
CHANGED
|
@@ -44,7 +44,7 @@ npm install patroon
|
|
|
44
44
|
|
|
45
45
|
## Usage
|
|
46
46
|
|
|
47
|
-
```js
|
|
47
|
+
```js
|
|
48
48
|
const {
|
|
49
49
|
|
|
50
50
|
// Match Helpers
|
|
@@ -76,7 +76,7 @@ The simplest thing one can do is to match on a [primitive][1].
|
|
|
76
76
|
|
|
77
77
|
Numbers:
|
|
78
78
|
|
|
79
|
-
```js
|
|
79
|
+
```js
|
|
80
80
|
patroon(
|
|
81
81
|
2, 3,
|
|
82
82
|
1, 2
|
|
@@ -88,7 +88,7 @@ patroon(
|
|
|
88
88
|
|
|
89
89
|
Strings:
|
|
90
90
|
|
|
91
|
-
```js
|
|
91
|
+
```js
|
|
92
92
|
patroon(
|
|
93
93
|
'a', 'b',
|
|
94
94
|
'c', 'd'
|
|
@@ -100,7 +100,7 @@ d
|
|
|
100
100
|
|
|
101
101
|
Booleans:
|
|
102
102
|
|
|
103
|
-
```js
|
|
103
|
+
```js
|
|
104
104
|
patroon(
|
|
105
105
|
true, false,
|
|
106
106
|
false, true
|
|
@@ -112,7 +112,7 @@ false
|
|
|
112
112
|
|
|
113
113
|
Symbols:
|
|
114
114
|
|
|
115
|
-
```js
|
|
115
|
+
```js
|
|
116
116
|
const a = Symbol('a')
|
|
117
117
|
const b = Symbol('b')
|
|
118
118
|
const c = Symbol('c')
|
|
@@ -128,7 +128,7 @@ Symbol(c)
|
|
|
128
128
|
|
|
129
129
|
Nil values:
|
|
130
130
|
|
|
131
|
-
```js
|
|
131
|
+
```js
|
|
132
132
|
patroon(
|
|
133
133
|
null, undefined,
|
|
134
134
|
undefined, null,
|
|
@@ -143,7 +143,7 @@ null
|
|
|
143
143
|
Will check if a Regex matches the passed string using the string's `.test`
|
|
144
144
|
method.
|
|
145
145
|
|
|
146
|
-
```js
|
|
146
|
+
```js
|
|
147
147
|
patroon(
|
|
148
148
|
/^bunion/, 'string starts with bunion',
|
|
149
149
|
/^banana/, 'string starts with banana'
|
|
@@ -157,7 +157,7 @@ string starts with banana
|
|
|
157
157
|
|
|
158
158
|
The `_` is a placeholder/wildcard value that is useful to implement a default case.
|
|
159
159
|
|
|
160
|
-
```js
|
|
160
|
+
```js
|
|
161
161
|
patroon(
|
|
162
162
|
1, 'value is 1',
|
|
163
163
|
'a', 'value is a',
|
|
@@ -174,7 +174,7 @@ We can combine the `_` with other patroon features.
|
|
|
174
174
|
|
|
175
175
|
Patroon can help you match **objects** that follow a certain spec.
|
|
176
176
|
|
|
177
|
-
```js
|
|
177
|
+
```js
|
|
178
178
|
patroon(
|
|
179
179
|
{b: _}, 'has a "b" property',
|
|
180
180
|
{a: _}, 'has an "a" property'
|
|
@@ -186,7 +186,7 @@ has a "b" property
|
|
|
186
186
|
|
|
187
187
|
Next we also match on the key's value.
|
|
188
188
|
|
|
189
|
-
```js
|
|
189
|
+
```js
|
|
190
190
|
patroon(
|
|
191
191
|
{a: 1}, 'a is 1',
|
|
192
192
|
{a: 2}, 'a is 2',
|
|
@@ -199,7 +199,7 @@ a is 2
|
|
|
199
199
|
|
|
200
200
|
What about nested objects?
|
|
201
201
|
|
|
202
|
-
```js
|
|
202
|
+
```js
|
|
203
203
|
patroon(
|
|
204
204
|
{a: {a: 1}}, 'a.a is 1',
|
|
205
205
|
{a: {a: 2}}, 'a.a is 2',
|
|
@@ -215,7 +215,7 @@ a.a is 2
|
|
|
215
215
|
Sometimes it's nice to know if the value is of a certain type. We'll use the
|
|
216
216
|
builtin node error constructors in this example.
|
|
217
217
|
|
|
218
|
-
```js
|
|
218
|
+
```js
|
|
219
219
|
patroon(
|
|
220
220
|
instanceOf(TypeError), 'is a type error',
|
|
221
221
|
instanceOf(Error), 'is an error'
|
|
@@ -227,7 +227,7 @@ is an error
|
|
|
227
227
|
|
|
228
228
|
Patroon uses `instanceof` to match on types.
|
|
229
229
|
|
|
230
|
-
```js
|
|
230
|
+
```js
|
|
231
231
|
new TypeError() instanceof Error
|
|
232
232
|
```
|
|
233
233
|
```
|
|
@@ -236,7 +236,7 @@ true
|
|
|
236
236
|
|
|
237
237
|
Because of this you can match a TypeError with an Error.
|
|
238
238
|
|
|
239
|
-
```js
|
|
239
|
+
```js
|
|
240
240
|
patroon(
|
|
241
241
|
instanceOf(Error), 'matches on error',
|
|
242
242
|
instanceOf(TypeError), 'matches on type error'
|
|
@@ -249,7 +249,7 @@ matches on error
|
|
|
249
249
|
An object of a certain type might also have values we would want to match on.
|
|
250
250
|
Here you should use the every helper.
|
|
251
251
|
|
|
252
|
-
```js
|
|
252
|
+
```js
|
|
253
253
|
patroon(
|
|
254
254
|
every(instanceOf(TypeError), { value: 20 }), 'type error where value is 20',
|
|
255
255
|
every(instanceOf(Error), { value: 30 }), 'error where value is 30',
|
|
@@ -262,7 +262,7 @@ type error where value is 20
|
|
|
262
262
|
|
|
263
263
|
Matching on an object type can be written in several ways.
|
|
264
264
|
|
|
265
|
-
```js
|
|
265
|
+
```js
|
|
266
266
|
patroon({}, 'is object')({})
|
|
267
267
|
patroon(Object, 'is object')({})
|
|
268
268
|
```
|
|
@@ -271,21 +271,21 @@ These are all equivalent.
|
|
|
271
271
|
|
|
272
272
|
Arrays can also be matched in a similar way.
|
|
273
273
|
|
|
274
|
-
```js
|
|
274
|
+
```js
|
|
275
275
|
patroon([], 'is array')([])
|
|
276
276
|
patroon(Array, 'is array')([])
|
|
277
277
|
```
|
|
278
278
|
|
|
279
279
|
A less intuitive case:
|
|
280
280
|
|
|
281
|
-
```js
|
|
281
|
+
```js
|
|
282
282
|
patroon({}, 'is object')([])
|
|
283
283
|
patroon([], 'is array')({})
|
|
284
284
|
```
|
|
285
285
|
|
|
286
286
|
Patroon allows this because Arrays can have properties defined.
|
|
287
287
|
|
|
288
|
-
```js
|
|
288
|
+
```js
|
|
289
289
|
const array = []
|
|
290
290
|
array.prop = 42
|
|
291
291
|
|
|
@@ -294,7 +294,7 @@ patroon({prop: _}, 'has prop')(array)
|
|
|
294
294
|
|
|
295
295
|
The other way around is also allowed even if it seems weird.
|
|
296
296
|
|
|
297
|
-
```js
|
|
297
|
+
```js
|
|
298
298
|
const object = {0: 42}
|
|
299
299
|
patroon([42], 'has 0th')(object)
|
|
300
300
|
```
|
|
@@ -302,7 +302,7 @@ patroon([42], 'has 0th')(object)
|
|
|
302
302
|
If you do not desire this loose behavior you can use a predicate to make sure
|
|
303
303
|
something is an array or object.
|
|
304
304
|
|
|
305
|
-
```js
|
|
305
|
+
```js
|
|
306
306
|
patroon(Array.isArray, 'is array')([])
|
|
307
307
|
```
|
|
308
308
|
|
|
@@ -311,7 +311,7 @@ patroon(Array.isArray, 'is array')([])
|
|
|
311
311
|
If you wish to match on the reference of a constructor you can use the `ref`
|
|
312
312
|
helper.
|
|
313
313
|
|
|
314
|
-
```js
|
|
314
|
+
```js
|
|
315
315
|
patroon(
|
|
316
316
|
instanceOf(Error), 'is an instance of Error',
|
|
317
317
|
reference(Error), 'is the Error constructor'
|
|
@@ -323,7 +323,7 @@ is the Error constructor
|
|
|
323
323
|
|
|
324
324
|
### Array
|
|
325
325
|
|
|
326
|
-
```js
|
|
326
|
+
```js
|
|
327
327
|
patroon(
|
|
328
328
|
[], 'is an array',
|
|
329
329
|
)([1, 2, 3])
|
|
@@ -332,7 +332,7 @@ patroon(
|
|
|
332
332
|
is an array
|
|
333
333
|
```
|
|
334
334
|
|
|
335
|
-
```js
|
|
335
|
+
```js
|
|
336
336
|
patroon(
|
|
337
337
|
[1], 'is an array that starts with 1',
|
|
338
338
|
[1,2], 'is an array that starts with 1 and 2',
|
|
@@ -353,7 +353,7 @@ verbose when wanting to match on a bigger index.
|
|
|
353
353
|
|
|
354
354
|
These two patterns are equivalent:
|
|
355
355
|
|
|
356
|
-
```js
|
|
356
|
+
```js
|
|
357
357
|
patroon(
|
|
358
358
|
{6: 7}, 'Index 6 has value 7',
|
|
359
359
|
[_, _, _, _, _, _, 7], 'Index 6 has value 7'
|
|
@@ -365,7 +365,7 @@ Index 6 has value 7
|
|
|
365
365
|
|
|
366
366
|
A function that returns the lenght of an array:
|
|
367
367
|
|
|
368
|
-
```js
|
|
368
|
+
```js
|
|
369
369
|
const count = patroon(
|
|
370
370
|
[_], ([, ...xs]) => 1 + count(xs),
|
|
371
371
|
[], 0
|
|
@@ -379,7 +379,7 @@ count([0,1,2,3])
|
|
|
379
379
|
|
|
380
380
|
A function that looks for a certain pattern in an array:
|
|
381
381
|
|
|
382
|
-
```js
|
|
382
|
+
```js
|
|
383
383
|
const containsPattern = patroon(
|
|
384
384
|
[0, 0], true,
|
|
385
385
|
[_, _], ([, ...rest]) => containsPattern(rest),
|
|
@@ -394,7 +394,7 @@ true
|
|
|
394
394
|
|
|
395
395
|
A toPairs function:
|
|
396
396
|
|
|
397
|
-
```js
|
|
397
|
+
```js
|
|
398
398
|
const toPairs = patroon(
|
|
399
399
|
[_, _], ([a, b, ...c], p = []) => toPairs(c, [...p, [a, b]]),
|
|
400
400
|
_, (_, p = []) => p
|
|
@@ -414,7 +414,7 @@ toPairs([1, 2, 3, 4])
|
|
|
414
414
|
|
|
415
415
|
A helper that makes it easy to check if a value passes all patterns.
|
|
416
416
|
|
|
417
|
-
```js
|
|
417
|
+
```js
|
|
418
418
|
const gte200 = x => x >= 200
|
|
419
419
|
const lt300 = x => x < 300
|
|
420
420
|
|
|
@@ -430,7 +430,7 @@ Is a 200 status code
|
|
|
430
430
|
|
|
431
431
|
A helper to check if any of the pattern matches value.
|
|
432
432
|
|
|
433
|
-
```js
|
|
433
|
+
```js
|
|
434
434
|
const isMovedResponse = patroon(
|
|
435
435
|
{statusCode: some(301, 302, 307, 308)}, true,
|
|
436
436
|
_, false
|
|
@@ -447,7 +447,7 @@ true
|
|
|
447
447
|
Patroon offers the `multi` function in order to match on the value of another
|
|
448
448
|
argument than the first one. This is named [multiple dispatch][2].
|
|
449
449
|
|
|
450
|
-
```js
|
|
450
|
+
```js
|
|
451
451
|
patroon(
|
|
452
452
|
multi(1, 2, 3), 'arguments are 1, 2 and 3'
|
|
453
453
|
)(1, 2, 3)
|
|
@@ -463,7 +463,7 @@ By default a function is assumed to be a predicate.
|
|
|
463
463
|
See the [references](#references) section if you wish to match on the reference
|
|
464
464
|
of the function.
|
|
465
465
|
|
|
466
|
-
```js
|
|
466
|
+
```js
|
|
467
467
|
const isTrue = v => v === true
|
|
468
468
|
|
|
469
469
|
patroon(
|
|
@@ -476,7 +476,7 @@ is true
|
|
|
476
476
|
|
|
477
477
|
Could one combine predicates with arrays and objects? Sure one can!
|
|
478
478
|
|
|
479
|
-
```js
|
|
479
|
+
```js
|
|
480
480
|
const gt20 = v => v > 20
|
|
481
481
|
|
|
482
482
|
patroon(
|
|
@@ -487,7 +487,7 @@ patroon(
|
|
|
487
487
|
is greater than 20
|
|
488
488
|
```
|
|
489
489
|
|
|
490
|
-
```js
|
|
490
|
+
```js
|
|
491
491
|
const gt42 = v => v > 42
|
|
492
492
|
|
|
493
493
|
patroon(
|
|
@@ -503,7 +503,7 @@ is greater than 42
|
|
|
503
503
|
A pattern matching helper that can help with using patroon patterns in if
|
|
504
504
|
statements and such.
|
|
505
505
|
|
|
506
|
-
```js
|
|
506
|
+
```js
|
|
507
507
|
const isUser = matches({user: _})
|
|
508
508
|
const isAdmin = matches({user: {admin: true}})
|
|
509
509
|
|
|
@@ -532,7 +532,7 @@ It is very easy to write your own helpers. All the builtin helpers are really
|
|
|
532
532
|
just predicates. Let's look at the source of one of these helpers, the simplest
|
|
533
533
|
one being the `_` helper.
|
|
534
534
|
|
|
535
|
-
```js
|
|
535
|
+
```js
|
|
536
536
|
_.toString()
|
|
537
537
|
```
|
|
538
538
|
```
|
|
@@ -542,7 +542,7 @@ _.toString()
|
|
|
542
542
|
Other more complex helpers like the `every` or `some` helper are also
|
|
543
543
|
predicates.
|
|
544
544
|
|
|
545
|
-
```js
|
|
545
|
+
```js
|
|
546
546
|
every.toString()
|
|
547
547
|
```
|
|
548
548
|
```
|
|
@@ -564,7 +564,7 @@ created. It's important to know when they occur.
|
|
|
564
564
|
|
|
565
565
|
The no match error occurs when none of the patterns match the value.
|
|
566
566
|
|
|
567
|
-
```js
|
|
567
|
+
```js
|
|
568
568
|
const oneIsTwo = patroon(1, 2)
|
|
569
569
|
|
|
570
570
|
oneIsTwo(3)
|
|
@@ -582,7 +582,7 @@ NoMatchError: Not able to match any pattern for arguments
|
|
|
582
582
|
|
|
583
583
|
Another error that occurs is when the patroon function is not used correctly.
|
|
584
584
|
|
|
585
|
-
```js
|
|
585
|
+
```js
|
|
586
586
|
patroon(1)
|
|
587
587
|
```
|
|
588
588
|
```
|
|
@@ -598,7 +598,7 @@ UnevenArgumentCountError: Patroon should have an even amount of arguments.
|
|
|
598
598
|
|
|
599
599
|
All errors patroon produces can be matched against the PatroonError using `instanceof`.
|
|
600
600
|
|
|
601
|
-
```js
|
|
601
|
+
```js
|
|
602
602
|
const isPatroonError = patroon(instanceOf(PatroonError), 'patroon is causing an error')
|
|
603
603
|
|
|
604
604
|
isPatroonError(new NoMatchError())
|
|
@@ -624,7 +624,7 @@ some property based tests.
|
|
|
624
624
|
We also care about code coverage so we'll use [nyc][8] to generate a coverage
|
|
625
625
|
report.
|
|
626
626
|
|
|
627
|
-
```bash
|
|
627
|
+
```bash
|
|
628
628
|
# Clean install dependencies.
|
|
629
629
|
npm ci &> /dev/null
|
|
630
630
|
|
|
@@ -635,7 +635,7 @@ npx nyc npm t | npx tap-nyc
|
|
|
635
635
|
npx nyc check-coverage
|
|
636
636
|
```
|
|
637
637
|
```
|
|
638
|
-
> patroon@1.5.
|
|
638
|
+
> patroon@1.5.2 test
|
|
639
639
|
> tape ./src/index.test.js
|
|
640
640
|
-------------|---------|----------|---------|---------|-------------------
|
|
641
641
|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
|
|
@@ -649,7 +649,7 @@ npx nyc check-coverage
|
|
|
649
649
|
total: 31
|
|
650
650
|
passing: 31
|
|
651
651
|
|
|
652
|
-
duration:
|
|
652
|
+
duration: 9.2s
|
|
653
653
|
|
|
654
654
|
```
|
|
655
655
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "patroon",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.2",
|
|
4
4
|
"description": "Pattern matching library",
|
|
5
5
|
"repository": "github:bas080/patroon",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"author": "Bassim Huis",
|
|
29
29
|
"license": "MIT",
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"tape": "^5.
|
|
31
|
+
"tape": "^5.6.3",
|
|
32
32
|
"tape-check": "^1.0.0-rc.0"
|
|
33
33
|
},
|
|
34
34
|
"contributors": [
|