Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@buka/class-transformer-extra

buka-inc60.7kMIT2.1.4TypeScript support: included

class-transformer-extra contains methods that's aren't included in the class-transform package.

class-transformer, class, transformer, transform, extra, external

readme

@buka/class-transformer-extra

version downloads dependencies license Codecov

@buka/class-transformer-extra contains methods that's aren't included in the class-transformer package.

Install

npm install @buka/class-transformer-extra class-transformer
# OR
yarn install @buka/class-transformer-extra class-transformer
# OR
pnpm install @buka/class-transformer-extra class-transformer

Usage

String

method before transformer after transformer
@Split(",") "a,b,c" ["a", "b", "c"]
@Trim() " abc " "abc"
@ToString({ optional: true }) 123 "123"
@ToLowerCase() "ABC" "abc"
@ToUpperCase() "abc" "ABC"
@Replace("-", "_") "a-b-c" "a_b_c"

@Split, @Trim, @ToLowerCase, @ToUpperCase and @Replace will do nothing if the value isn't string. When set optional: true, undefined will not be transformed.

Date

method before transformer after transformer
@ToDate({ optional: true }) "2024-01-01" new Date("2024-01-01")
@FormatDate("YYYY/MM/DD", { optional: true }) "2024-01-01" "2024/01/01"

Number

method before transformer after transformer
@ToNumber({ optional: true }) "123" 123
@ToBigInt({ optional: true }) "123" 123n

ToNumber() and @ToBigInt() will return NaN if the value isn't number

Boolean

method before transformer after transformer
@ToBoolean() 1 true

ToBoolean() has multiple parameters to adapt to different needs, examples:

ToBoolean(v => Boolean(v)), { optional: true }): If the value is undefined, do nothing. Otherwise, v => Boolean(v) will be used to transform value.

ToBoolean(['0', 'false', false], { optional: true }):

If the value is '0' or 'false' or false, transform to false, and if the value is undefined, do nothing, otherwise value will be transform to true.

Array

method before transformer after transformer
@Filter((num: number) => num > 3) [1,2,3.4,5] [4,5]
@Flatten() [1, [2, [3, 4, [5]]]] [1,2,3,4,5]
@Uniq() [1,1,2,3,4,4,5] [1,2,3,4,5]
@UniqBy(Math.abs) [-1, 1, 2, 3, -3] [-1, 2, 3]

@Filter, @Flatten, @Uniq and @UniqBy will do nothing if the value isn't array.

Contribute

If you want to report bug or add new decorators, please submit an Issue or Pull Request.

Q&A

  1. Why can't it be used with the @IsOptional decorator?

    The latest version of class-validator@^0.14.1 is not set metadata that can identify @IsOptional. If the identifiers are added in later versions, I will also follow up. And the identifier had be add in develop branch of class-validator.

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

2.1.4 (2024-12-18)

Bug Fixes

  • repository.url in package.json (96e24da)

2.1.3 (2024-12-18)

Bug Fixes

  • rename toUpperCase function to ToUpperCase for consistency (e3af12c)

Performance Improvements

  • replace arrow functions with named functions in transformation decorators (a7df879)

2.1.2 (2024-10-20)

Bug Fixes

2.1.1 (2024-09-11)

Bug Fixes

2.1.0 (2024-09-03)

Features

  • support optional property (0bebc38)

2.0.0 (2024-08-29)

⚠ BREAKING CHANGES

  • The first parameter of ToBoolean changes

Features

  • add optional parameter and custom false values to ToBoolean transformer (9232fee)

1.1.1 (2024-02-19)

Bug Fixes

  • unabled published to npm (056bff2)

1.1.0 (2024-02-19)

Features

  • split add default option (d70988d)

1.0.0 (2024-01-08)