Socket
Socket
Sign inDemoInstall

@buka/class-transformer-extra

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@buka/class-transformer-extra

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


Version published
Weekly downloads
35
decreased by-45.31%
Maintainers
1
Weekly downloads
 
Created
Source

@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

methodbefore transformerafter 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

methodbefore transformerafter 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

methodbefore transformerafter transformer
@ToNumber({ optional: true })"123"123
@ToBigInt({ optional: true })"123"123n

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

Boolean

methodbefore transformerafter transformer
@ToBoolean()1true

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

methodbefore transformerafter 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.

Keywords

FAQs

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