Skip to main content

createTransformer()

function createTransformer<TValue>(transformer, defaultOptions?): <TForm>(field, options?) => void;

Defined in: core/behavior/behaviors/transform-value.ts:154

Хелпер для создания переиспользуемых трансформаций

Type Parameters

TValue

TValue extends FormValue = FormValue

Parameters

transformer

(value) => TValue

Идемпотентная функция преобразования значения

defaultOptions?

TransformValueOptions

Опции, применяемые ко всем вызовам созданного трансформера

Returns

<TForm>(field, options?): void;

Type Parameters

TForm

TForm extends FormFields

Parameters

field

FieldPathNode<TForm, TValue>

options?

TransformValueOptions & object

Returns

void

Examples

import { createTransformer, type BehaviorSchemaFn } from '@reformer/core/behaviors';

// Сохраняем только цифры и форматируем СНИЛС: 000-000-000 00
const formatSnils = createTransformer<string>((v) => {
const d = (v ?? '').replace(/\D/g, '').slice(0, 11);
if (d.length < 9) return d;
return `${d.slice(0, 3)}-${d.slice(3, 6)}-${d.slice(6, 9)}${d.length > 9 ? ' ' + d.slice(9) : ''}`;
});

interface ProfileForm { snils: string }

export const profileBehavior: BehaviorSchemaFn<ProfileForm> = (path) => {
formatSnils(path.snils, { debounce: 100 });
};
import { createTransformer, type BehaviorSchemaFn } from '@reformer/core/behaviors';

// Все коды должны быть uppercase, но только после правки пользователем
const upperOnUserEdit = createTransformer<string>(
(v) => (v ?? '').toUpperCase(),
{ onUserChangeOnly: true, debounce: 100 },
);

interface PromoForm { promoCode: string; partnerCode: string }

export const promoBehavior: BehaviorSchemaFn<PromoForm> = (path) => {
upperOnUserEdit(path.promoCode);
upperOnUserEdit(path.partnerCode);
};