1+ import type { PermutationGenerator } from '#src/permutation/definitions' ;
12import type {
2- PermutationGenerator ,
3- UnwrapPermutation ,
4- UnwrapPermutationGenerator ,
5- } from '#src/permutation/definitions ' ;
3+ AppendModifier ,
4+ Optional ,
5+ OptionalPatch ,
6+ } from '#src/permutation/modifiers/optional.types ' ;
67import { merge } from '#src/permutation/utils' ;
7- import type { CastAsPermutationGenerator } from '#src/utils/casting' ;
88
9- type UnwrapValue < T > = UnwrapPermutation < UnwrapPermutationGenerator < CastAsPermutationGenerator < T > > > ;
10-
11- export type OptionalGenerator < out T extends PermutationGenerator = PermutationGenerator > =
12- ( ) => Iterable < UnwrapValue < T > > ;
13-
14- export type OptionalPatch < T extends PermutationGenerator = PermutationGenerator > = {
15- readonly size : T [ 'size' ] ;
16- readonly modifiers : 'optional' extends T [ 'modifiers' ] [ number ]
17- ? T [ 'modifiers' ]
18- : [ 'optional' , ...T [ 'modifiers' ] ] ;
19- readonly originalInputArg : T ;
20- readonly type : T [ 'type' ] ;
21- readonly structure : T [ 'structure' ] ;
22- readonly permutationPaths : readonly string [ ] ;
23- readonly primitivePermutationPaths : readonly string [ ] ;
24- extract : ( paths : readonly string [ ] ) => PermutationGenerator ;
25- exclude : ( paths : readonly string [ ] ) => PermutationGenerator ;
26- generatorAt : ( path : string ) => PermutationGenerator ;
27- override : ( v : PermutationGenerator ) => PermutationGenerator ;
28- } ;
29-
30- export function optional < const T extends PermutationGenerator > (
31- input : T ,
32- ) : OptionalGenerator < T > & OptionalPatch < T > {
33- if ( isOptional ( input ) ) return input as unknown as OptionalGenerator < T > & OptionalPatch < T > ;
34- const modifiers = [ 'optional' , ...input . modifiers ] ;
9+ export function optional < const T extends PermutationGenerator > ( input : T ) : Optional < T > {
10+ if ( isOptional ( input ) ) return input as unknown as Optional < T > ;
11+ const modifiers = [ 'optional' , ...input . modifiers ] as AppendModifier < T [ 'modifiers' ] > ;
3512 return Object . assign (
3613 function * ( ) {
3714 yield * input ( ) ;
38- } as OptionalGenerator < T > ,
15+ } ,
3916 {
4017 get size ( ) {
4118 return input . size ;
@@ -44,7 +21,7 @@ export function optional<const T extends PermutationGenerator>(
4421 return modifiers ;
4522 } ,
4623 get originalInputArg ( ) {
47- return input . originalInputArg as PermutationGenerator ;
24+ return input . originalInputArg as T ;
4825 } ,
4926 get type ( ) {
5027 return input . type ;
@@ -70,10 +47,10 @@ export function optional<const T extends PermutationGenerator>(
7047 override ( v ) {
7148 return merge ( this , v ) ;
7249 } ,
73- } satisfies OptionalPatch & ThisType < OptionalGenerator < T > & OptionalPatch < T > > ,
74- ) as OptionalGenerator < T > & OptionalPatch < T > ;
50+ } satisfies OptionalPatch < T > & ThisType < Optional < T > > ,
51+ ) as Optional < T > ;
7552}
7653
77- export function isOptional ( v : PermutationGenerator ) : v is OptionalGenerator & OptionalPatch {
54+ export function isOptional ( v : PermutationGenerator ) : v is Optional {
7855 return v . modifiers . includes ( 'optional' ) ;
7956}
0 commit comments