@@ -193,16 +193,35 @@ export function isObjectOf<
193193 predObj : T ,
194194 options : { strict ?: boolean } = { } ,
195195) : Predicate < ObjectOf < T > > {
196- const preds = Object . entries ( predObj ) ;
197- const allKeys = new Set ( preds . map ( ( [ key ] ) => key ) ) ;
198- const requiredKeys = preds
199- . filter ( ( [ _ , pred ] ) => ! ( pred as OptionalPredicate < unknown > ) . optional )
200- . map ( ( [ key ] ) => key ) ;
201- const hasKeys = options . strict
202- ? ( props : string [ ] ) => props . every ( ( p ) => allKeys . has ( p ) )
203- : ( props : string [ ] ) => requiredKeys . every ( ( k ) => props . includes ( k ) ) ;
204- return ( x : unknown ) : x is ObjectOf < T > =>
205- isRecord ( x ) && hasKeys ( Object . keys ( x ) ) && preds . every ( ( [ k , p ] ) => p ( x [ k ] ) ) ;
196+ return options . strict ? isObjectOfStrict ( predObj ) : isObjectOfLoose ( predObj ) ;
197+ }
198+
199+ function isObjectOfLoose <
200+ T extends RecordOf < Predicate < unknown > > ,
201+ > (
202+ predObj : T ,
203+ ) : Predicate < ObjectOf < T > > {
204+ return ( x : unknown ) : x is ObjectOf < T > => {
205+ if ( ! isRecord ( x ) ) return false ;
206+ for ( const k in predObj ) {
207+ if ( ! predObj [ k ] ( x [ k ] ) ) return false ;
208+ }
209+ return true ;
210+ } ;
211+ }
212+
213+ function isObjectOfStrict <
214+ T extends RecordOf < Predicate < unknown > > ,
215+ > (
216+ predObj : T ,
217+ ) : Predicate < ObjectOf < T > > {
218+ const keys = new Set ( Object . keys ( predObj ) ) ;
219+ const pred = isObjectOfLoose ( predObj ) ;
220+ return ( x : unknown ) : x is ObjectOf < T > => {
221+ if ( ! pred ( x ) ) return false ;
222+ const ks = Object . keys ( x ) ;
223+ return ks . length <= keys . size && ks . every ( ( k ) => keys . has ( k ) ) ;
224+ } ;
206225}
207226
208227/**
0 commit comments