Skip to content

Commit

Permalink
feat: rename
Browse files Browse the repository at this point in the history
  • Loading branch information
tonioriol committed Oct 27, 2024
1 parent b52a177 commit ac7ea8d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 55 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Mutant
# Transmutant

A lightweight TypeScript library for flexible object transformation with type safety.
A lightweight TypeScript library for flexible object transmutation with type safety.

## Installation

```bash
npm install mutant
npm install transmutant
```

## Features

- 🔒 Type-safe transformations
- 🔒 Type-safe transmutations
- 🎯 Direct property mapping
- ⚡ Custom transformation functions
- ⚡ Custom transmutation functions
- 🔄 Flexible schema definition
- 📦 Zero dependencies

Expand All @@ -21,7 +21,7 @@ npm install mutant
### Basic Property Mapping

```typescript
import { mutate } from 'mutant';
import { transmute } from 'transmutant';

interface User {
firstName: string;
Expand Down Expand Up @@ -51,7 +51,7 @@ const user: User = {
age: 30
};

const userDTO = mutate<User, UserDTO>(schema, user);
const userDTO = transmute<User, UserDTO>(schema, user);
// Result: { fullName: 'John Doe', yearOfBirth: 1994 }
```

Expand All @@ -74,7 +74,7 @@ const schema = [
];

const source: Source = { id: 1, name: 'John' };
const target = mutate<Source, Target>(schema, source);
const target = transmute<Source, Target>(schema, source);
// Result: { userId: 1, userName: 'John' }
```

Expand All @@ -97,7 +97,7 @@ const schema = [
];

const product: Product = { price: 100 };
const pricedProduct = mutate<Product, PricedProduct>(
const pricedProduct = transmute<Product, PricedProduct>(
schema,
product,
{ taxRate: 0.2 }
Expand All @@ -107,15 +107,15 @@ const pricedProduct = mutate<Product, PricedProduct>(

## API Reference

### `mutate<Source, Target>(schema, source, extra?)`
### `transmute<Source, Target>(schema, source, extra?)`

Transforms a source source into a target type based on the provided schema.
Transmutes a source object into a target type based on the provided schema.

#### Parameters

- `schema`: Array of transformation rules defining how properties should be mapped or transformed
- `source`: Source source to transform
- `extra`: (Optional) Additional data to pass to transformation functions
- `schema`: Array of transmutation rules defining how properties should be transmuted
- `source`: Source object to transmute
- `extra`: (Optional) Additional data to pass to transmutation functions

#### Schema Options

Expand All @@ -127,15 +127,15 @@ Transforms a source source into a target type based on the provided schema.
}
```

2. Custom transformation:
2. Custom transmutation:
```typescript
{
to: keyof Target;
fn: (args: { source: Source; extra?: Extra }) => unknown;
}
```

3. Combined mapping and transformation:
3. Combined mapping and transmutation:
```typescript
{
to: keyof Target;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "mutant",
"name": "transmutant",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -16,6 +16,7 @@
"keywords": [
"transform",
"mutation",
"transmutation",
"typescript",
"schema",
"mapping",
Expand All @@ -24,7 +25,7 @@
],
"author": "Toni Oriol <[email protected]> (http://tonioriol.com/)",
"license": "MIT",
"description": "Transform your data with the power of mutation 🧬",
"description": "Powerful type transmutations for TypeScript 🧬",
"devDependencies": {
"@types/jest": "^29.5.14",
"jest": "^29.7.0",
Expand Down
30 changes: 15 additions & 15 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mutate, Schema } from '../'
import { transmute, Schema } from '../'

interface SourceUser {
firstName: string
Expand All @@ -20,7 +20,7 @@ interface TargetUser {
isAdult: boolean
}

describe('mutate', () => {
describe('transmute', () => {
const sourceUser: SourceUser = {
firstName: 'John',
lastName: 'Doe',
Expand All @@ -38,35 +38,35 @@ describe('mutate', () => {
{ from: 'email', to: 'contactEmail' }
]

const result = mutate(schema, sourceUser)
const result = transmute(schema, sourceUser)
expect(result).toEqual({ contactEmail: '[email protected]' })
})

it('should handle custom transformation functions', () => {
it('should handle custom transmutation functions', () => {
const schema: Schema<SourceUser, Pick<TargetUser, 'fullName'>>[] = [
{
to: 'fullName',
fn: ({ source }) => `${source.firstName} ${source.lastName}`
}
]

const result = mutate(schema, sourceUser)
const result = transmute(schema, sourceUser)
expect(result).toEqual({ fullName: 'John Doe' })
})

it('should handle transformation with both "from" and "fn"', () => {
it('should handle transmutation with both "from" and "fn"', () => {
const schema: Schema<SourceUser, Pick<TargetUser, 'userAge'>>[] = [
{
to: 'userAge',
fn: ({ source }) => source['age'] + 1
}
]

const result = mutate(schema, sourceUser)
const result = transmute(schema, sourceUser)
expect(result).toEqual({ userAge: 26 })
})

it('should handle extra data in transformations', () => {
it('should handle extra data in transmutations', () => {
const schema: Schema<SourceUser, Pick<TargetUser, 'location'>>[] = [
{
to: 'location',
Expand All @@ -75,11 +75,11 @@ describe('mutate', () => {
}
]

const result = mutate(schema, sourceUser, { separator: ' | ' })
const result = transmute(schema, sourceUser, { separator: ' | ' })
expect(result).toEqual({ location: 'New York, USA | ' })
})

it('should handle multiple transformations', () => {
it('should handle multiple transmutations', () => {
const schema: Schema<SourceUser, TargetUser>[] = [
{
to: 'fullName',
Expand All @@ -104,7 +104,7 @@ describe('mutate', () => {
}
]

const result = mutate(schema, sourceUser)
const result = transmute(schema, sourceUser)
expect(result).toEqual({
fullName: 'John Doe',
userAge: 25,
Expand All @@ -114,21 +114,21 @@ describe('mutate', () => {
})
})

it('should set null for undefined transformations', () => {
it('should set null for undefined transmutations', () => {
const schema: Schema<SourceUser, { optionalField: string }>[] = [
{
to: 'optionalField',
from: 'nonexistentField' as keyof SourceUser
}
]

const result = mutate(schema, sourceUser)
const result = transmute(schema, sourceUser)
expect(result).toEqual({ optionalField: null })
})

it('should handle empty schema', () => {
const schema: Schema<SourceUser, {}>[] = []
const result = mutate(schema, sourceUser)
const result = transmute(schema, sourceUser)
expect(result).toEqual({})
})

Expand All @@ -142,7 +142,7 @@ describe('mutate', () => {
{ from: 'email', to: 'contactEmail' }
]

const result = mutate(schema, sourceWithNull)
const result = transmute(schema, sourceWithNull)
expect(result).toEqual({ contactEmail: null })
})
})
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { Extra, Schema } from './types'
export * from './types'

/**
* Mutates an object from the Source type into the Target type based on the provided schema
* @template Source - The source type being mutated from
* @template Target - The target type being mutated to
* Transmutes an object from the Source type into the Target type based on the provided schema
* @template Source - The source type being transmuted from
* @template Target - The target type being transmuted to
* @template TExtra - Type of additional data passed to mutation functions
* @param schema - Array of mutation rules
* @param source - Source object to mutate
* @param source - Source object to transmute
* @param extra - Optional extra data to pass to mutation functions
* @returns Mutated object matching Target type
* @returns Transmuted object matching Target type
*/
export const mutate = <Source, Target, TExtra extends Extra = Extra>(
export const transmute = <Source, Target, TExtra extends Extra = Extra>(
schema: Schema<Source, Target>[],
source: Source,
extra?: TExtra
Expand Down
28 changes: 14 additions & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,40 @@ export type Extra = Record<string, unknown>

/**
* Arguments passed to a mutation function
* @template From - The source type being mutated from
* @template From - The source type being transmuted from
*/
export interface MutateFnArgs<Source> {
/** The source object being transformed */
export interface TransmuteFnArgs<Source> {
/** The source object being transmuted */
source: Source
/** Optional source property key */
from?: keyof Source
/** Optional extra data to assist with transformation */
/** Optional extra data to assist with transmutation */
extra?: Extra
}

/**
* Function that performs a custom transformation on a source source
* @template From - The source type being mutated from
* Function that performs a custom transmutation on a source object
* @template From - The source type being transmuted from
*/
export type MutateFn<Source> = (args: MutateFnArgs<Source>) => unknown
export type TransmuteFn<Source> = (args: TransmuteFnArgs<Source>) => unknown

/**
* Defines how a property should be transformed from source to target type
* @template From - The source type being mutated from
* @template To - The target type being mutated to
* Defines how a property should be transmuted from source to target type
* @template From - The source type being transmuted from
* @template To - The target type being transmuted to
*/
export type Schema<Source, Target> = | {
/** Target property key */
to: keyof Target
/** Source property key for direct mapping */
from: keyof Source
/** Custom transformation function */
fn?: MutateFn<Source>
/** Custom transmutation function */
fn?: TransmuteFn<Source>
} | {
/** Target property key */
to: keyof Target
/** Source property key for direct mapping */
from?: keyof Source
/** Custom transformation function */
fn: MutateFn<Source>
/** Custom transmutation function */
fn: TransmuteFn<Source>
}

0 comments on commit ac7ea8d

Please sign in to comment.