Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/types/mapper.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { ExtractSourceKeys, ExtractSources, ExtractDestinationKeys, ExtractDestinations, Profile } from '@/types/profile'
import { ExtractSourceKeys, Profile } from '@/types/profile'

/**
* Registered profiles grouped once by source key, then destination key, so each `map` call is two
* property lookups rather than a fresh pass over every registered profile.
*/
type ProfileIndex<TProfile> = {
[TSourceKey in ExtractSourceKeys<TProfile>]: {
[TMatch in Extract<TProfile, { sourceKey: TSourceKey }> as TMatch extends Profile ? TMatch['destinationKey'] : never]: TMatch
}
}

type ProfileSource<TProfile> = TProfile extends Profile<string, infer TSource, string, unknown> ? TSource : never

type ProfileDestination<TProfile> = TProfile extends Profile<string, never, string, infer TDestination> ? TDestination : never

export type Mapper<TProfiles extends readonly Profile[], TProfile = TProfiles[number]> = {
register: (profiles: Profile[] | readonly Profile[] | Profile) => void,
clear: () => void,
has: (sourceKey: string, destinationKey: string) => boolean,
map: <
TSourceKey extends ExtractSourceKeys<TProfile>,
TDestinationKey extends ExtractDestinationKeys<TProfile, TSourceKey>
> (sourceKey: TSourceKey, source: ExtractSources<TProfile, TSourceKey>, destinationKey: TDestinationKey) => ExtractDestinations<TProfile, TSourceKey, TDestinationKey>,
TSourceKey extends keyof ProfileIndex<TProfile> & string,
TDestinationKey extends keyof ProfileIndex<TProfile>[TSourceKey] & string
> (sourceKey: TSourceKey, source: ProfileSource<ProfileIndex<TProfile>[TSourceKey][TDestinationKey]>, destinationKey: TDestinationKey) => ProfileDestination<ProfileIndex<TProfile>[TSourceKey][TDestinationKey]>,
mapMany: <
TSourceKey extends ExtractSourceKeys<TProfile>,
TDestinationKey extends ExtractDestinationKeys<TProfile, TSourceKey>
> (sourceKey: TSourceKey, sourceArray: ExtractSources<TProfile, TSourceKey>[], destinationKey: TDestinationKey) => ExtractDestinations<TProfile, TSourceKey, TDestinationKey>[],
TSourceKey extends keyof ProfileIndex<TProfile> & string,
TDestinationKey extends keyof ProfileIndex<TProfile>[TSourceKey] & string
> (sourceKey: TSourceKey, sourceArray: ProfileSource<ProfileIndex<TProfile>[TSourceKey][TDestinationKey]>[], destinationKey: TDestinationKey) => ProfileDestination<ProfileIndex<TProfile>[TSourceKey][TDestinationKey]>[],
}