Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

transmutant

Package Overview
Dependencies
Maintainers
0
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

transmutant

Powerful type transmutations for TypeScript 🧬

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6
increased by200%
Maintainers
0
Weekly downloads
 
Created
Source

Transmutant

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

Installation

npm install transmutant

Features

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

Usage

Direct Property Mapping

interface Source {
  email: string;
}

interface Target {
  contactEmail: string;
}

const schema: Schema<Source, Target>[] = [
  { from: 'email', to: 'contactEmail' }
];

const source: Source = { email: 'john@example.com' };
const target = transmute(schema, source);

// Result: { contactEmail: 'john@example.com' }

Using Custom Transmutation Functions

interface Source {
  firstName: string;
  lastName: string;
}

interface Target {
  fullName: string;
}

const schema: Schema<Source, Target>[] = [
  {
    to: 'fullName',
    fn: ({ source }) => `${source.firstName} ${source.lastName}`
  }
];

const source: Source = { firstName: 'John', lastName: 'Doe' };
const target = transmute(schema, source);

// Result: { fullName: 'John Doe' }

Using Extra Data

interface Source {
  city: string;
  country: string;
}

interface Target {
  location: string;
}

interface Extra {
  separator: string;
}

const schema: Schema<Source, Target, Extra>[] = [
  {
    to: 'location',
    fn: ({ source, extra }) =>
      `${source.city}, ${source.country}${extra?.separator}`
  }
];

const source: Source = {
  city: 'New York',
  country: 'USA'
};

const target = transmute(schema, source, { separator: ' | ' });

// Result: { location: 'New York, USA | ' }

API Reference

transmute(schema, source, extra?)

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

Parameters
  • 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 Types

Each schema entry must specify the target property and use either direct mapping OR a custom function:

type Schema<Source, Target, TExtra> = {
  /** Target property key */
  to: keyof Target;
} & (
  | {
      /** Source property key for direct mapping */
      from: keyof Source;
      fn?: never;
    }
  | {
      /** Custom transmutation function */
      fn: TransmuteFn<Source, TExtra>;
      from?: never;
    }
);

The TransmuteFn type is defined as:

type TransmuteFn<Source, TExtra> = (args: {
  source: Source;
  extra?: TExtra;
}) => unknown;
Behavior Notes
  • Direct mapping uses the from property to copy values directly from source to target
  • Custom functions receive the entire source object and optional extra data
  • If a direct mapping property is undefined or null, it will be set to null in the target object
  • Empty schemas result in an empty object
  • Each schema entry must use either from OR fn, but not both
  • The schema is processed sequentially, with each rule contributing to the final object

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Keywords

FAQs

Package last updated on 28 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc