patroon 1.3.0 → 1.4.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/CHANGELOG.md +6 -0
- package/CONTRIBUTING.md +2 -2
- package/README.md +35 -4
- package/README.mz +27 -0
- package/package.json +1 -1
- package/src/index.js +8 -0
- package/src/index.test.js +21 -0
- package/tape-test +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -4,8 +4,14 @@ 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.4.0](https://github.com/bas080/patroon/compare/v1.3.0...v1.4.0)
|
|
8
|
+
|
|
9
|
+
- Add the matches pattern helper [`249eba6`](https://github.com/bas080/patroon/commit/249eba61481ce1a8899a3474c5055e85d441b783)
|
|
10
|
+
|
|
7
11
|
#### [v1.3.0](https://github.com/bas080/patroon/compare/v1.2.6...v1.3.0)
|
|
8
12
|
|
|
13
|
+
> 5 October 2022
|
|
14
|
+
|
|
9
15
|
- Remove testcheck from dev dependency list [`6d0bbfb`](https://github.com/bas080/patroon/commit/6d0bbfbc41a539670f9ce830c6b3f679fafaff7e)
|
|
10
16
|
|
|
11
17
|
#### [v1.2.6](https://github.com/bas080/patroon/compare/v1.2.5...v1.2.6)
|
package/CONTRIBUTING.md
CHANGED
|
@@ -42,11 +42,11 @@ npm link patroon
|
|
|
42
42
|
```
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
-
up to date, audited 3 packages in
|
|
45
|
+
up to date, audited 3 packages in 910ms
|
|
46
46
|
|
|
47
47
|
found 0 vulnerabilities
|
|
48
48
|
|
|
49
|
-
added 1 package, and audited 79 packages in
|
|
49
|
+
added 1 package, and audited 79 packages in 906ms
|
|
50
50
|
|
|
51
51
|
52 packages are looking for funding
|
|
52
52
|
run `npm fund` for details
|
package/README.md
CHANGED
|
@@ -22,6 +22,7 @@ Pattern matching in JavaScript without additional syntax.
|
|
|
22
22
|
* [Some](#some)
|
|
23
23
|
* [Multi](#multi)
|
|
24
24
|
* [Predicate](#predicate)
|
|
25
|
+
* [Matches](#matches)
|
|
25
26
|
* [Custom Helper](#custom-helper)
|
|
26
27
|
* [Errors](#errors)
|
|
27
28
|
+ [NoMatchError](#nomatcherror)
|
|
@@ -46,7 +47,9 @@ npm install patroon
|
|
|
46
47
|
```js node -
|
|
47
48
|
const {
|
|
48
49
|
|
|
50
|
+
// Match Helpers
|
|
49
51
|
patroon,
|
|
52
|
+
matches,
|
|
50
53
|
|
|
51
54
|
// Pattern Helpers
|
|
52
55
|
every,
|
|
@@ -495,6 +498,34 @@ patroon(
|
|
|
495
498
|
is greater than 42
|
|
496
499
|
```
|
|
497
500
|
|
|
501
|
+
### Matches
|
|
502
|
+
|
|
503
|
+
A pattern matching helper that can help with using patroon patterns in if
|
|
504
|
+
statements and such.
|
|
505
|
+
|
|
506
|
+
```js ./tape-test
|
|
507
|
+
const isUser = matches({user: _})
|
|
508
|
+
const isAdmin = matches({user: {admin: true}})
|
|
509
|
+
|
|
510
|
+
const user = {
|
|
511
|
+
user: {
|
|
512
|
+
id: 2
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const admin = {
|
|
517
|
+
user: {
|
|
518
|
+
id: 1,
|
|
519
|
+
admin: true
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
JSON.stringify([isUser(admin), isUser(user), isAdmin(admin), isAdmin(user)])
|
|
524
|
+
```
|
|
525
|
+
```
|
|
526
|
+
[true,true,true,false]
|
|
527
|
+
```
|
|
528
|
+
|
|
498
529
|
### Custom Helper
|
|
499
530
|
|
|
500
531
|
It is very easy to write your own helpers. All the builtin helpers are really
|
|
@@ -604,7 +635,7 @@ npx nyc npm t | npx tap-nyc
|
|
|
604
635
|
npx nyc check-coverage
|
|
605
636
|
```
|
|
606
637
|
```
|
|
607
|
-
> patroon@1.
|
|
638
|
+
> patroon@1.4.0 test
|
|
608
639
|
> tape ./src/index.test.js
|
|
609
640
|
-------------|---------|----------|---------|---------|-------------------
|
|
610
641
|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
|
|
@@ -615,10 +646,10 @@ npx nyc check-coverage
|
|
|
615
646
|
walkable.js | 100 | 100 | 100 | 100 |
|
|
616
647
|
-------------|---------|----------|---------|---------|-------------------
|
|
617
648
|
|
|
618
|
-
total:
|
|
619
|
-
passing:
|
|
649
|
+
total: 31
|
|
650
|
+
passing: 31
|
|
620
651
|
|
|
621
|
-
duration:
|
|
652
|
+
duration: 10.7s
|
|
622
653
|
|
|
623
654
|
```
|
|
624
655
|
|
package/README.mz
CHANGED
|
@@ -24,7 +24,9 @@ npm install patroon
|
|
|
24
24
|
```js node -
|
|
25
25
|
const {
|
|
26
26
|
|
|
27
|
+
// Match Helpers
|
|
27
28
|
patroon,
|
|
29
|
+
matches,
|
|
28
30
|
|
|
29
31
|
// Pattern Helpers
|
|
30
32
|
every,
|
|
@@ -392,6 +394,31 @@ patroon(
|
|
|
392
394
|
)([{a: 43}])
|
|
393
395
|
```
|
|
394
396
|
|
|
397
|
+
### Matches
|
|
398
|
+
|
|
399
|
+
A pattern matching helper that can help with using patroon patterns in if
|
|
400
|
+
statements and such.
|
|
401
|
+
|
|
402
|
+
```js ./tape-test
|
|
403
|
+
const isUser = matches({user: _})
|
|
404
|
+
const isAdmin = matches({user: {admin: true}})
|
|
405
|
+
|
|
406
|
+
const user = {
|
|
407
|
+
user: {
|
|
408
|
+
id: 2
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const admin = {
|
|
413
|
+
user: {
|
|
414
|
+
id: 1,
|
|
415
|
+
admin: true
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
JSON.stringify([isUser(admin), isUser(user), isAdmin(admin), isAdmin(user)])
|
|
420
|
+
```
|
|
421
|
+
|
|
395
422
|
### Custom Helper
|
|
396
423
|
|
|
397
424
|
It is very easy to write your own helpers. All the builtin helpers are really
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -108,6 +108,13 @@ function instanceOf (ctor) {
|
|
|
108
108
|
return value => value instanceof ctor
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
function matches (pattern) {
|
|
112
|
+
return patroon(
|
|
113
|
+
pattern, true,
|
|
114
|
+
T, false
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
|
|
111
118
|
module.exports = Object.assign(patroon, {
|
|
112
119
|
NoMatchError,
|
|
113
120
|
UnevenArgumentCountError,
|
|
@@ -121,5 +128,6 @@ module.exports = Object.assign(patroon, {
|
|
|
121
128
|
typed,
|
|
122
129
|
t: typed,
|
|
123
130
|
instanceOf,
|
|
131
|
+
matches,
|
|
124
132
|
_: T
|
|
125
133
|
})
|
package/src/index.test.js
CHANGED
|
@@ -5,6 +5,7 @@ const { version } = require('../package')
|
|
|
5
5
|
const {
|
|
6
6
|
reference,
|
|
7
7
|
instanceOf,
|
|
8
|
+
matches,
|
|
8
9
|
typed,
|
|
9
10
|
t,
|
|
10
11
|
ref,
|
|
@@ -227,6 +228,26 @@ test('Does not match on reference', t => {
|
|
|
227
228
|
t.end()
|
|
228
229
|
})
|
|
229
230
|
|
|
231
|
+
test('Matches function', check(gen.any, gen.any, (t, a, b) => {
|
|
232
|
+
t.ok(matches(a)(a))
|
|
233
|
+
t.equals(typeof matches(a)(b), 'boolean')
|
|
234
|
+
t.end()
|
|
235
|
+
}))
|
|
236
|
+
|
|
237
|
+
test('Check that matches throws non patroon errors', t => {
|
|
238
|
+
class SomeError extends Error { }
|
|
239
|
+
|
|
240
|
+
const predicate = () => {
|
|
241
|
+
throw new SomeError()
|
|
242
|
+
}
|
|
243
|
+
const pattern = {
|
|
244
|
+
predicate
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
t.throws(() => matches(pattern)({ predicate: 2 }), SomeError)
|
|
248
|
+
t.end()
|
|
249
|
+
})
|
|
250
|
+
|
|
230
251
|
if (version.startsWith('2')) {
|
|
231
252
|
test('Deprecated functions in version 2', t => {
|
|
232
253
|
t.plan(3)
|