Skip to content

Commit

Permalink
refactor: use Transmuter for the function name
Browse files Browse the repository at this point in the history
  • Loading branch information
tonioriol committed Oct 31, 2024
1 parent 37c2071 commit 44df3d0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

### ⚠ BREAKING CHANGES

* Schema type now enforces strict type compatibility between source and target properties. Code with mismatched types in direct property mappings will need to be updated to use TransmuteFn for type conversion or fix the type mismatch.
* Schema type now enforces strict type compatibility between source and target properties. Code with mismatched types in direct property mappings will need to be updated to use Transmuter for type conversion or fix the type mismatch.

### Features

Expand All @@ -23,7 +23,7 @@

### Features

* enforce strict return types in transmutation functions ([bb575df](https://github.com/tonioriol/transmutant/commit/bb575dfd605934d76627867ed507567591c86317))
* enforce strict return types in transmuter functions ([bb575df](https://github.com/tonioriol/transmutant/commit/bb575dfd605934d76627867ed507567591c86317))

## [2.0.0](https://github.com/tonioriol/transmutant/compare/v1.0.1...v2.0.0) (2024-10-28)

Expand Down
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A powerful, type-safe TypeScript library for transmuting objects through flexibl
## Features

- 🔒 **Type-safe**: Full TypeScript support with strong type inference
- 🎯 **Flexible mapping**: Direct property mapping or custom transmutation functions
- 🎯 **Flexible mapping**: Direct property mapping or custom transmuter functions
-**High performance**: Minimal overhead and zero dependencies
- 🔄 **Extensible**: Support for custom transmutation logic and external data
- 📦 **Lightweight**: Zero dependencies, small bundle size
Expand Down Expand Up @@ -52,7 +52,7 @@ const schema: Schema<User, UserDTO>[] = [
}
];

// Transmut the object
// Transmute the object
const user: User = {
firstName: 'John',
lastName: 'Doe',
Expand All @@ -67,15 +67,15 @@ const userDTO = transmute(schema, user);

### Schema Definition

A schema is an array of transmutation rules that define how properties should be mapped from the source to the target type. Each rule specifies the target property key and either a source property key for direct mapping or a transmutation function that produces the correct type for that target property.
A schema is an array of transmutation rules that define how properties should be mapped from the source to the target type. Each rule specifies the target property key and either a source property key for direct mapping or a transmuter function that produces the correct type for that target property.

```typescript
type Schema<Source, Target, Extra = unknown> = {
[TargetKey in keyof Target]: {
/** Target property key */
to: TargetKey
/** Source property key for direct mapping or a custom transmutation function */
from: keyof Source | TransmuteFn<Source, Target, TargetKey, Extra>
/** Source property key for direct mapping or a custom transmuter function */
from: keyof Source | Transmuter<Source, Target, TargetKey, Extra>
}
}[keyof Target]
```
Expand All @@ -100,7 +100,7 @@ const schema: Schema<Source, Target>[] = [
];
```

#### 2. Custom Transmutation Functions
#### 2. Custom Transmuter Functions

Transmute properties using custom logic with type safety:

Expand Down Expand Up @@ -149,7 +149,7 @@ const schema: Schema<Source, Target, ExtraData>[] = [

### Handling Undefined Values

When a source property doesn't exist or a transmutation function returns undefined, the target property will remain undefined:
When a source property doesn't exist or a transmuter function returns undefined, the target property will remain undefined:

```typescript
interface Source {
Expand Down Expand Up @@ -185,7 +185,7 @@ This allows you to:

### `transmute<Source, Target, Extra = unknown>`

Main transmutation function.
Main transmuter function.

#### Parameters

Expand All @@ -209,21 +209,21 @@ type Schema<Source, Target, Extra = unknown> = {
[TargetKey in keyof Target]: {
/** Target property key */
to: TargetKey
/** Source property key for direct mapping or a custom transmutation function */
from: keyof Source | TransmuteFn<Source, Target, TargetKey, Extra>
/** Source property key for direct mapping or a custom transmuter function */
from: keyof Source | Transmuter<Source, Target, TargetKey, Extra>
}
}[keyof Target]

/**
* Function that performs property transmutation
*/
type TransmuteFn<Source, Target, TargetKey extends keyof Target, Extra = unknown> =
(args: TransmuteFnArgs<Source, Extra>) => Target[TargetKey]
type Transmuter<Source, Target, TargetKey extends keyof Target, Extra = unknown> =
(args: TransmuterArgs<Source, Extra>) => Target[TargetKey]

/**
* Arguments passed to transmutation function
* Arguments passed to transmuter function
*/
type TransmuteFnArgs<Source, Extra> = {
type TransmuterArgs<Source, Extra> = {
source: Source
extra?: Extra
}
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('transmute', () => {
expect(result).toEqual({ contactEmail: '[email protected]' })
})

it('should handle custom transmutation functions', () => {
it('should handle custom transmuter functions', () => {
const schema: Schema<SourceUser, TargetUser>[] = [
{
to: 'fullName',
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @template Source - The source type being transmuted from
* @template Extra - Type of additional data for transmutation
*/
export type TransmuteFnArgs<Source, Extra> = Extra extends undefined | never
export type TransmuterArgs<Source, Extra> = Extra extends undefined | never
? { source: Source }
: { source: Source; extra: Extra }

Expand All @@ -14,8 +14,8 @@ export type TransmuteFnArgs<Source, Extra> = Extra extends undefined | never
* @template TargetKey - The specific key of the target property being set
* @template Extra - Type of additional data for transmutation
*/
export type TransmuteFn<Source, Target, TargetKey extends keyof Target, Extra> =
(args: TransmuteFnArgs<Source, Extra>) => Target[TargetKey]
export type Transmuter<Source, Target, TargetKey extends keyof Target, Extra> =
(args: TransmuterArgs<Source, Extra>) => Target[TargetKey]

/**
* Get keys of Source that have values assignable to Target[TargetKey]
Expand All @@ -33,6 +33,6 @@ type AssignableKeys<Source, Target, TargetKey extends keyof Target> = {
export type Schema<Source, Target, Extra = undefined> = {
[TargetKey in keyof Target]: {
to: TargetKey
from: AssignableKeys<Source, Target, TargetKey> | TransmuteFn<Source, Target, TargetKey, Extra>
from: AssignableKeys<Source, Target, TargetKey> | Transmuter<Source, Target, TargetKey, Extra>
}
}[keyof Target]

0 comments on commit 44df3d0

Please sign in to comment.