Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ npm install @kitbag/mapper
## Basic Setup

```ts
import mapper from '@kitbag/mapper'
import mapper, { createProfile } from '@kitbag/mapper'

const profiles = [
createProfile('number', 'string', (source: number): boolean => source.toString())
createProfile('number', 'string', (source: number): string => source.toString()),
createProfile('number', 'Date', (source: number): Date =>new Date(source))
]

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@kitbag/mapper",
"private": false,
"version": "0.0.6",
"version": "0.1.0",
"description": "A simple and versatile mapping utility for Typescript.",
"bugs": {
"url": "https://github.com/kitbagjs/mapper/issues"
Expand All @@ -12,6 +12,7 @@
"types": "./dist/src/index.d.ts",
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/mapper.mjs",
"require": "./dist/mapper.umd.cjs"
}
Expand Down
2 changes: 2 additions & 0 deletions src/types/mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ type ProfileIndex<TProfile> = {
}
}

/** Extracts the source type from a profile. `unknown` for the destination parameter matches any profile regardless of its destination type. */
type ProfileSource<TProfile> = TProfile extends Profile<string, infer TSource, string, unknown> ? TSource : never

/** Extracts the destination type from a profile. `never` for the source parameter exploits contravariance — any concrete source type satisfies `never extends TSource` in the function position, so the match is unconditional. */
type ProfileDestination<TProfile> = TProfile extends Profile<string, never, string, infer TDestination> ? TDestination : never

export type Mapper<TProfiles extends readonly Profile[], TProfile = TProfiles[number]> = {
Expand Down
8 changes: 0 additions & 8 deletions src/types/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,5 @@ export type ProfileKey<T extends Profile> = `${T['sourceKey']}-${T['destinationK
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ExtractSourceKeys<TProfile> = TProfile extends Profile<infer TSourceKey, any, any> ? TSourceKey : never

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ExtractSources<TProfile, TKey extends ExtractSourceKeys<TProfile>> = TProfile extends Profile<TKey, infer TSource, any> ? TSource : never

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ExtractDestinationKeys<TProfile, TSourceKey extends ExtractSourceKeys<TProfile>> = TProfile extends Profile<TSourceKey, any, infer TDestinationKey> ? TDestinationKey : never

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ExtractDestinations<TProfile, TSourceKey extends ExtractSourceKeys<TProfile>, TDestinationKey extends ExtractDestinationKeys<TProfile, TSourceKey>> = TProfile extends Profile<TSourceKey, any, TDestinationKey, infer TDestination> ? TDestination : never

export type Profiles = readonly Profile[]
Loading