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 🧬

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
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

Basic Property Mapping

import { transmute } from 'transmutant';

interface User {
  firstName: string;
  lastName: string;
  age: number;
}

interface UserDTO {
  fullName: string;
  yearOfBirth: number;
}

const schema = [
  {
    to: 'fullName',
    fn: ({ source }) => `${source.firstName} ${source.lastName}`
  },
  {
    to: 'yearOfBirth',
    fn: ({ source }) => new Date().getFullYear() - source.age
  }
];

const user: User = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30
};

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

Direct Property Mapping

interface Source {
  id: number;
  name: string;
}

interface Target {
  userId: number;
  userName: string;
}

const schema = [
  { from: 'id', to: 'userId' },
  { from: 'name', to: 'userName' }
];

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

Using Extra Data

interface Product {
  price: number;
}

interface PricedProduct {
  finalPrice: number;
}

const schema = [
  {
    to: 'finalPrice',
    fn: ({ source, extra }) => source.price * (1 + extra.taxRate)
  }
];

const product: Product = { price: 100 };
const pricedProduct = transmute<Product, PricedProduct>(
  schema,
  product,
  { taxRate: 0.2 }
);
// Result: { finalPrice: 120 }

API Reference

transmute<Source, Target>(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 Options
  1. Direct mapping:
{
  to: keyof Target;
  from: keyof Source;
}
  1. Custom transmutation:
{
  to: keyof Target;
  fn: (args: { source: Source; extra?: Extra }) => unknown;
}
  1. Combined mapping and transmutation:
{
  to: keyof Target;
  from: keyof Source;
  fn: (args: { source: Source; from?: keyof Source; extra?: Extra }) => unknown;
}

License

MIT

Contributing

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

Keywords

FAQs

Package last updated on 27 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