transmutant
Advanced tools
Comparing version 2.1.0 to 3.0.0
@@ -7,3 +7,3 @@ import { Schema } from './types'; | ||
* @template Target - The target type being transmuted to | ||
* @template TExtra - Type of additional data passed to mutation functions | ||
* @template Extra - Type of additional data passed to mutation functions | ||
* @param schema - Array of mutation rules | ||
@@ -14,2 +14,2 @@ * @param source - Source object to transmute | ||
*/ | ||
export declare const transmute: <Source, Target, TExtra = unknown>(schema: Schema<Source, Target, TExtra>[], source: Source, extra?: TExtra) => Target; | ||
export declare const transmute: <Source, Target, Extra = unknown>(schema: Schema<Source, Target, Extra>[], source: Source, extra?: Extra) => Target; |
@@ -23,3 +23,3 @@ "use strict"; | ||
* @template Target - The target type being transmuted to | ||
* @template TExtra - Type of additional data passed to mutation functions | ||
* @template Extra - Type of additional data passed to mutation functions | ||
* @param schema - Array of mutation rules | ||
@@ -26,0 +26,0 @@ * @param source - Source object to transmute |
/** | ||
* Arguments passed to a mutation function | ||
* @template Source - The source type being transmuted from | ||
* @template TExtra - Type of additional data for transmutation | ||
* @template Extra - Type of additional data for transmutation | ||
*/ | ||
export type TransmuteFnArgs<Source, TExtra> = { | ||
export type TransmuteFnArgs<Source, Extra> = { | ||
/** The source object being transmuted */ | ||
source: Source; | ||
/** Optional extra data to assist with transmutation */ | ||
extra?: TExtra; | ||
extra?: Extra; | ||
}; | ||
@@ -17,12 +17,18 @@ /** | ||
* @template TargetKey - The specific key of the target property being set | ||
* @template TExtra - Type of additional data for transmutation | ||
* @template Extra - Type of additional data for transmutation | ||
*/ | ||
export type TransmuteFn<Source, Target, TargetKey extends keyof Target, TExtra = unknown> = (args: TransmuteFnArgs<Source, TExtra>) => Target[TargetKey]; | ||
export type TransmuteFn<Source, Target, TargetKey extends keyof Target, Extra> = (args: TransmuteFnArgs<Source, Extra>) => Target[TargetKey]; | ||
/** | ||
* Get keys of Source that have values assignable to Target[TargetKey] | ||
*/ | ||
type AssignableKeys<Source, Target, TargetKey extends keyof Target> = { | ||
[SourceKey in keyof Source]: Source[SourceKey] extends Target[TargetKey] ? SourceKey : never; | ||
}[keyof Source]; | ||
/** | ||
* Defines how a property should be transmuted from source to target type | ||
* @template Source - The source type being transmuted from | ||
* @template Target - The target type being transmuted to | ||
* @template TExtra - Type of additional data for transmutation | ||
* @template Extra - Type of additional data for transmutation | ||
*/ | ||
export type Schema<Source, Target, TExtra = unknown> = { | ||
export type Schema<Source, Target, Extra = unknown> = { | ||
[TargetKey in keyof Target]: { | ||
@@ -32,4 +38,5 @@ /** Target property key */ | ||
/** Source property key for direct mapping or a custom transmutation function */ | ||
from: keyof Source | TransmuteFn<Source, Target, TargetKey, TExtra>; | ||
from: AssignableKeys<Source, Target, TargetKey> | TransmuteFn<Source, Target, TargetKey, Extra>; | ||
}; | ||
}[keyof Target]; | ||
export {}; |
{ | ||
"name": "transmutant", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
# 🧬 Transmutant 🧬 | ||
A powerful, type-safe TypeScript library for transforming objects through flexible schema definitions. | ||
A powerful, type-safe TypeScript library for transmuting objects through flexible schema definitions. | ||
@@ -13,5 +13,5 @@ [![npm version](https://badge.fury.io/js/transmutant.svg)](https://www.npmjs.com/package/transmutant) | ||
- 🔒 **Type-safe**: Full TypeScript support with strong type inference | ||
- 🎯 **Flexible mapping**: Direct property mapping or custom transformation functions | ||
- 🎯 **Flexible mapping**: Direct property mapping or custom transmutation functions | ||
- ⚡ **High performance**: Minimal overhead and zero dependencies | ||
- 🔄 **Extensible**: Support for custom transformation logic and external data | ||
- 🔄 **Extensible**: Support for custom transmutation logic and external data | ||
- 📦 **Lightweight**: Zero dependencies, small bundle size | ||
@@ -44,3 +44,3 @@ - 🛠️ **Predictable**: Transparent handling of undefined values | ||
// Define transformation schema | ||
// Define transmutation schema | ||
const schema: Schema<User, UserDTO>[] = [ | ||
@@ -57,3 +57,3 @@ { | ||
// Transform the object | ||
// Transmut the object | ||
const user: User = { | ||
@@ -73,11 +73,11 @@ firstName: 'John', | ||
A schema is an array of transformation 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 transformation 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 transmutation function that produces the correct type for that target property. | ||
```typescript | ||
type Schema<Source, Target, TExtra = unknown> = { | ||
type Schema<Source, Target, Extra = unknown> = { | ||
[TargetKey in keyof Target]: { | ||
/** Target property key */ | ||
to: TargetKey | ||
/** Source property key for direct mapping or a custom transformation function */ | ||
from: keyof Source | TransmuteFn<Source, Target, TargetKey, TExtra> | ||
/** Source property key for direct mapping or a custom transmutation function */ | ||
from: keyof Source | TransmuteFn<Source, Target, TargetKey, Extra> | ||
} | ||
@@ -87,3 +87,3 @@ }[keyof Target] | ||
### Transformation Types | ||
### Transmutation Types | ||
@@ -108,5 +108,5 @@ #### 1. Direct Property Mapping | ||
#### 2. Custom Transformation Functions | ||
#### 2. Custom Transmutation Functions | ||
Transform properties using custom logic with type safety: | ||
Transmute properties using custom logic with type safety: | ||
@@ -130,5 +130,5 @@ ```typescript | ||
#### 3. External Data Transformations | ||
#### 3. External Data Transmutations | ||
Include additional context in transformations: | ||
Include additional context in transmutations: | ||
@@ -159,3 +159,3 @@ ```typescript | ||
When a source property doesn't exist or a transformation function returns undefined, the target property will remain undefined: | ||
When a source property doesn't exist or a transmutation function returns undefined, the target property will remain undefined: | ||
@@ -179,3 +179,3 @@ ```typescript | ||
to: 'computedField', | ||
from: ({ source }) => undefined // Transformation returns undefined | ||
from: ({ source }) => undefined // Transmutation returns undefined | ||
} | ||
@@ -191,17 +191,17 @@ ]; | ||
- Handle optional properties naturally | ||
- Process partial transformations as needed | ||
- Process partial transmutations as needed | ||
## API Reference | ||
### `transmute<Source, Target, TExtra = unknown>` | ||
### `transmute<Source, Target, Extra = unknown>` | ||
Main transformation function. | ||
Main transmutation function. | ||
#### Parameters | ||
| Parameter | Type | Description | | ||
|-----------|------------------------------------|-------------------------------| | ||
| schema | `Schema<Source, Target, TExtra>[]` | Array of transformation rules | | ||
| source | `Source` | Source object to transform | | ||
| extra? | `TExtra` | Optional additional data | | ||
| Parameter | Type | Description | | ||
|-----------|-----------------------------------|------------------------------| | ||
| schema | `Schema<Source, Target, Extra>[]` | Array of transmutation rules | | ||
| source | `Source` | Source object to transmut | | ||
| extra? | `Extra` | Optional additional data | | ||
@@ -216,10 +216,10 @@ #### Returns | ||
/** | ||
* Schema entry defining how a property should be transformed | ||
* Schema entry defining how a property should be transmuted | ||
*/ | ||
type Schema<Source, Target, TExtra = unknown> = { | ||
type Schema<Source, Target, Extra = unknown> = { | ||
[TargetKey in keyof Target]: { | ||
/** Target property key */ | ||
to: TargetKey | ||
/** Source property key for direct mapping or a custom transformation function */ | ||
from: keyof Source | TransmuteFn<Source, Target, TargetKey, TExtra> | ||
/** Source property key for direct mapping or a custom transmutation function */ | ||
from: keyof Source | TransmuteFn<Source, Target, TargetKey, Extra> | ||
} | ||
@@ -229,13 +229,13 @@ }[keyof Target] | ||
/** | ||
* Function that performs property transformation | ||
* Function that performs property transmutation | ||
*/ | ||
type TransmuteFn<Source, Target, TargetKey extends keyof Target, TExtra = unknown> = | ||
(args: TransmuteFnArgs<Source, TExtra>) => Target[TargetKey] | ||
type TransmuteFn<Source, Target, TargetKey extends keyof Target, Extra = unknown> = | ||
(args: TransmuteFnArgs<Source, Extra>) => Target[TargetKey] | ||
/** | ||
* Arguments passed to transformation function | ||
* Arguments passed to transmutation function | ||
*/ | ||
type TransmuteFnArgs<Source, TExtra> = { | ||
type TransmuteFnArgs<Source, Extra> = { | ||
source: Source | ||
extra?: TExtra | ||
extra?: Extra | ||
} | ||
@@ -242,0 +242,0 @@ ``` |
12646
88