@driimus/xf
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

xf

Per-property object transformation

Installation

pnpm add @driimus/xf

Usage

Compose property transformers using define

import { type PropertyTransformer, xf } from '@driimus/xf';

type User = {
  id: string;
  firstName: string;
  lastName: string;
  type?: 'admin' | 'user';
};

const transformer = xf.define(
  {
    fullName(user) { return `${user.firstName} ${user.lastName}`; },
  } satisfies PropertyTransformer<User>,
  {
    type(user) { return user.type ?? 'user'; },
  } satisfies PropertyTransformer<User>,
);

Compute transformed properties using apply

import { type PropertyTransformer, xf } from '@driimus/xf';

type User = {
  id: string;
  firstName: string;
  lastName: string;
  type?: 'admin' | 'user';
};

const data: User = { id: '1', firstName: 'Adam', lastName: 'Jones' };

const out = xf.apply(
  {
    fullName(user) { return `${user.firstName} ${user.lastName}`; },
  },
  data
); // { fullName: 'Adam Jones' }

Readme

Keywords

none

Package Sidebar

Install

npm i @driimus/xf

Weekly Downloads

1

Version

0.2.2

License

MIT

Unpacked Size

6.68 kB

Total Files

5

Last publish

Collaborators

  • driimus