Skip to content
Draft
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
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"editor.formatOnSave": false,
"javascript.format.enable": false,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"cSpell.words": [
"camelcase",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ This library provides a useful method for automatically loading profiles. If you
```

```ts
import { createMapper, loadProfiles } from '@stackoverfloweth/mapper'
import { createMapper, loadProfiles } from '@kitbag/mapper'
import * as maybeProfiles from '@/maps'

const profiles = loadProfiles(maybeProfiles)
Expand Down
10 changes: 6 additions & 4 deletions src/createMapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ProfileNotFoundError } from '@/profileNotFoundError'
import { Mapper, Profile, ProfileKey } from '@/types'
import { GenericMapper, Mapper, Profile, ProfileKey } from '@/types'

export function createMapper<T extends Profile>(profiles: T[]): Mapper<T> {

Expand All @@ -16,14 +16,16 @@ export function createMapper<T extends Profile>(profiles: T[]): Mapper<T> {
return profile
}

const mapper: Mapper<T> = {
const mapper: GenericMapper = {
map: (sourceKey, source, destinationKey) => {
const profile = getProfile(sourceKey, destinationKey)
const { map } = getProfile(sourceKey, destinationKey)
const profile = { map: map.bind(mapper) }

return profile.map(source)
},
mapMany: (sourceKey, sourceArray, destinationKey) => {
const profile = getProfile(sourceKey, destinationKey)
const { map } = getProfile(sourceKey, destinationKey)
const profile = { map: map.bind(mapper) }

return sourceArray.map(source => profile.map(source))
},
Expand Down
4 changes: 3 additions & 1 deletion src/createmapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { Profile } from '@/types'
const stringToBoolean = {
sourceKey: 'string',
destinationKey: 'boolean',
map: (source: string): boolean => Boolean(source),
map: function(source) {
this.map('anything', source, 'else')
},
} as const satisfies Profile

const mapper = createMapper([stringToBoolean])
Expand Down
19 changes: 17 additions & 2 deletions src/types/mapper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExtractSourceKeys, ExtractSources, ExtractDestinationKeys, ExtractDestinations, Profile } from '@/types/profile'
import { ExtractSourceKeys, ExtractSources, ExtractDestinationKeys, ExtractDestinations, Profile, GenericProfile } from '@/types/profile'

export type Mapper<T extends Profile> = {
map: <
Expand All @@ -13,4 +13,19 @@ export type Mapper<T extends Profile> = {
TDestinationKey extends ExtractDestinationKeys<T, TSourceKey>,
TDestination extends ExtractDestinations<T, TSourceKey, TDestinationKey>
> (sourceKey: TSourceKey, sourceArray: TSource[], destinationKey: TDestinationKey) => TDestination[],
}
}

export type GenericMapper = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
map: (sourceKey: string, source: any, destinationKey: string) => any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
mapMany: (sourceKey: string, sourceArray: any[], destinationKey: string) => any[],
}

export interface Register {
// mapper: Mapper
}

export type RegisteredMapper = Register extends { mapper: infer TMapper }
? TMapper
: GenericMapper
11 changes: 10 additions & 1 deletion src/types/profile.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { GenericMapper, RegisteredMapper } from '@/types/mapper'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface Profile<TSourceKey extends string = string, TSource = any, TDestinationKey extends string = string, TDestination = any> {
sourceKey: TSourceKey,
destinationKey: TDestinationKey,
map: (source: TSource) => TDestination,
map: (this: RegisteredMapper, source: TSource) => TDestination,
}

export interface GenericProfile {
sourceKey: string,
destinationKey: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
map: (this: GenericMapper, source: any) => any,
}

export type ProfileKey<T extends Profile> = `${T['sourceKey']}-${T['destinationKey']}`
Expand Down