@driimus/xf
v0.2.1
Published
Per-property object transformation
Downloads
36
Readme
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' }