11import type {
2+ Permutation ,
23 PermutationGenerator ,
34 UnwrapPermutation ,
45 UnwrapPermutationGenerator ,
56} from '#src/permutation/definitions' ;
7+ import { concat } from '#src/permutation/primitive/concat' ;
68import { explicitPermutations } from '#src/permutation/pure/explicit-permutations' ;
9+ import { REMOVE } from '#src/permutation/symbols' ;
710import type { PlainType } from '#src/utils/common' ;
811import { isExpandableArray } from '#src/utils/expandable-check' ;
912
@@ -19,11 +22,23 @@ export type RecordGenerator<T extends ValidRecordInput> = PlainType<{
1922
2023export function record < const T extends ValidRecordInput > ( input : T ) {
2124 return function ( ) {
22- const r = Object . entries ( input ) . map ( ( [ k , v ] ) => [ k , v ( ) ] as const ) ;
23- const size = r . reduce ( ( acc , curr ) => acc * curr [ 1 ] . size , 1 ) ;
25+ const r = Object . entries ( input ) . map ( ( [ k , v ] ) => {
26+ const b = v ( ) ;
27+ const ret = b . modifiers . includes ( 'optional' )
28+ ? ( [ k , concat ( v , REMOVE ) ( ) ] as const )
29+ : ( [ k , b ] as const ) ;
30+ return ret as [ string , Permutation ] ;
31+ } ) ;
32+ const size = r . reduce ( ( acc , curr ) => {
33+ const [ , p ] = curr ;
34+ const isOptional = p . modifiers . includes ( 'optional' ) ;
35+ const currentSize = p . size + ( isOptional ? 1 : 0 ) ;
36+ return acc * currentSize ;
37+ } , 1 ) ;
2438 return {
2539 size,
2640 type : 'record' ,
41+ modifiers : [ ] ,
2742 * [ Symbol . iterator ] ( ) {
2843 if ( isExpandableArray ( input ) ) {
2944 yield * explicitPermutations ( r . map ( ( v ) => v [ 1 ] ) ) ;
@@ -34,9 +49,8 @@ export function record<const T extends ValidRecordInput>(input: T) {
3449 } ,
3550 } ;
3651 } as PermutationGenerator <
37- {
38- readonly size : number ;
39- readonly type : 'record' ;
40- } & Iterable < RecordGenerator < T > >
52+ { readonly size : number ; readonly type : 'record' ; readonly modifiers : [ ] } & Iterable <
53+ RecordGenerator < T >
54+ >
4155 > ;
4256}
0 commit comments