From 2f7c0496dc16ab4d28f569c47261822448f17856 Mon Sep 17 00:00:00 2001 From: Evan Sutherland Date: Thu, 7 Mar 2024 19:32:55 -0600 Subject: [PATCH] trying register interface syntax --- .vscode/settings.json | 2 +- README.md | 2 +- src/createMapper.ts | 10 ++++++---- src/createmapper.spec.ts | 4 +++- src/types/mapper.ts | 19 +++++++++++++++++-- src/types/profile.ts | 11 ++++++++++- 6 files changed, 38 insertions(+), 10 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 8939317..5b93904 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,7 +2,7 @@ "editor.formatOnSave": false, "javascript.format.enable": false, "editor.codeActionsOnSave": { - "source.fixAll.eslint": true + "source.fixAll.eslint": "explicit" }, "cSpell.words": [ "camelcase", diff --git a/README.md b/README.md index 7470892..197df27 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/createMapper.ts b/src/createMapper.ts index 12bb033..4cbc579 100644 --- a/src/createMapper.ts +++ b/src/createMapper.ts @@ -1,5 +1,5 @@ import { ProfileNotFoundError } from '@/profileNotFoundError' -import { Mapper, Profile, ProfileKey } from '@/types' +import { GenericMapper, Mapper, Profile, ProfileKey } from '@/types' export function createMapper(profiles: T[]): Mapper { @@ -16,14 +16,16 @@ export function createMapper(profiles: T[]): Mapper { return profile } - const mapper: Mapper = { + 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)) }, diff --git a/src/createmapper.spec.ts b/src/createmapper.spec.ts index bcf8c78..c0b56cf 100644 --- a/src/createmapper.spec.ts +++ b/src/createmapper.spec.ts @@ -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]) diff --git a/src/types/mapper.ts b/src/types/mapper.ts index 851faa1..a3a6864 100644 --- a/src/types/mapper.ts +++ b/src/types/mapper.ts @@ -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 = { map: < @@ -13,4 +13,19 @@ export type Mapper = { TDestinationKey extends ExtractDestinationKeys, TDestination extends ExtractDestinations > (sourceKey: TSourceKey, sourceArray: TSource[], destinationKey: TDestinationKey) => TDestination[], -} \ No newline at end of file +} + +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 \ No newline at end of file diff --git a/src/types/profile.ts b/src/types/profile.ts index d372a62..ae505d0 100644 --- a/src/types/profile.ts +++ b/src/types/profile.ts @@ -1,8 +1,17 @@ +import { GenericMapper, RegisteredMapper } from '@/types/mapper' + // eslint-disable-next-line @typescript-eslint/no-explicit-any export interface Profile { 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['sourceKey']}-${T['destinationKey']}`