Skip to content

Commit b2c2bd0

Browse files
committed
$mol_error_match extracted
1 parent 8b8c791 commit b2c2bd0

File tree

3 files changed

+54
-72
lines changed

3 files changed

+54
-72
lines changed

error/match/match.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace $ {
2+
const recursion_protect = new WeakMap<Error, true>()
3+
4+
export function $mol_error_match< Instance >(
5+
host: unknown,
6+
is: (e: unknown) => boolean
7+
): Instance | null {
8+
9+
if( is(host) ) return host as Instance
10+
if ( ! ( host instanceof Error ) ) return null
11+
if (recursion_protect.get(host)) return null
12+
13+
recursion_protect.set(host, true)
14+
15+
let sub
16+
17+
if ( host instanceof AggregateError ) {
18+
for( const e of host.errors ) {
19+
sub = $mol_error_match< Instance >(e, is)
20+
if (sub) break
21+
}
22+
}
23+
24+
if ( ! sub && ! ( host instanceof $mol_error_mix ) && Array.isArray(host.cause) ) {
25+
for( const e of host.cause ) {
26+
sub = $mol_error_match< Instance >(e, is)
27+
if (sub) break
28+
}
29+
}
30+
31+
if (! sub) sub = $mol_error_match< Instance >(host.cause, is)
32+
33+
recursion_protect.delete(host)
34+
35+
return sub
36+
}
37+
38+
export function $mol_error_match_all< Instance >(
39+
host: unknown,
40+
is: (e: unknown) => boolean
41+
): readonly Instance[] {
42+
43+
const finded: Instance[] = []
44+
45+
$mol_error_match<Instance>(host, e => {
46+
if (is( e )) finded.push(e as Instance)
47+
return false
48+
})
49+
50+
return finded
51+
}
52+
53+
}

error/mix/mix.test.ts

-5
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,5 @@ namespace $ {
8989
$mol_assert_equal( mix.pick( MyError2 )?.message, 'my-error-2' )
9090

9191
},
92-
93-
'pick all by class'() {
94-
const { mix } = error_mock()
95-
$mol_assert_equal( mix.pick_all( SyntaxError ).map(err => err.message), [ 'xxx1', 'xxx2', 'xxx4' ] )
96-
},
9792
})
9893
}

error/mix/mix.ts

+1-67
Original file line numberDiff line numberDiff line change
@@ -21,77 +21,11 @@ namespace $ {
2121
return this.errors.map( e => e.message )
2222
}
2323

24-
static recursion_protect = new WeakMap<Error, true>()
25-
26-
static find< Instance >(
27-
host: unknown,
28-
is: (e: unknown) => boolean
29-
): Instance | null {
30-
31-
if( is(host) ) return host as Instance
32-
if ( ! ( host instanceof Error ) ) return null
33-
if (this.recursion_protect.get(host)) return null
34-
35-
this.recursion_protect.set(host, true)
36-
37-
let sub
38-
39-
if ( ! ( host instanceof $mol_error_mix ) && Array.isArray(host.cause) ) {
40-
for( const e of host.cause ) {
41-
sub = this.find< Instance >(e, is)
42-
if (sub) break
43-
}
44-
}
45-
46-
if ( ! sub && host instanceof AggregateError ) {
47-
for( const e of host.errors ) {
48-
sub = this.find< Instance >(e, is)
49-
if (sub) break
50-
}
51-
}
52-
53-
if (! sub) sub = this.find< Instance >(host.cause, is)
54-
55-
this.recursion_protect.delete(host)
56-
57-
return sub
58-
}
59-
60-
static filter< Instance >(
61-
host: unknown,
62-
is: (e: unknown) => boolean
63-
): readonly Instance[] {
64-
65-
const finded: Instance[] = []
66-
67-
this.find<Instance>(host, e => {
68-
if (is( e )) finded.push(e as Instance)
69-
return false
70-
})
71-
72-
return finded
73-
}
74-
75-
filter< Instance >( is: (e: unknown) => boolean ) {
76-
return $mol_error_mix.filter<Instance>(this, is)
77-
}
78-
79-
find< Instance >( is: (e: unknown) => boolean ) {
80-
return $mol_error_mix.find<Instance>(this, is)
81-
}
82-
8324
pick<
8425
Instance extends Error,
8526
Class extends new (...args: any[]) => Instance
8627
>( Class: Class ) {
87-
return this.find<Instance>(e => e instanceof Class)
88-
}
89-
90-
pick_all<
91-
Instance extends Error,
92-
Class extends new (...args: any[]) => Instance
93-
>( Class: Class ) {
94-
return this.filter<Instance>(e => e instanceof Class)
28+
return $mol_error_match<Instance>(this, e => e instanceof Class)
9529
}
9630

9731
}

0 commit comments

Comments
 (0)