@buka/class-transformer-extra

中文文档
2.1.4发布于 2024/12/19MITTypeScriptESM + CJS构建来源已验证近 30 天下载 4.8万

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

原始使用文档
文档目录9 节
  1. Install
  2. Usage
  3. String
  4. Date
  5. Number
  6. Boolean
  7. Array
  8. Contribute
  9. Q&A

@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.

版本与发布记录

@buka/class-transformer-extra 共发布 7 个版本,首次发布于 2024/01/08;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.1.4latest2024/12/191.9 年前
2.1.22024/10/202 年前
2.1.12024/09/122 年前
2.1.02024/09/032 年前
2.0.02024/08/292 年前
1.1.12024/02/192 年前

关于 @buka/class-transformer-extra 的常见问题

@buka/class-transformer-extra 是什么?

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

如何安装 @buka/class-transformer-extra?

在项目目录运行 npm install @buka/class-transformer-extra,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:class-transformer(^0.5.1)、class-validator(^0.14.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@buka/class-transformer-extra 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@buka/class-transformer-extra 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@buka/class-transformer-extra 可以商用吗?

@buka/class-transformer-extra 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@buka/class-transformer-extra 的下载量有多少?

根据 npm 官方下载统计,@buka/class-transformer-extra 在 2026/10/09 – 2026/11/07 这 30 天内被下载 47,759 次,日均约 1,592 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@buka/class-transformer-extra 还在维护吗?

最新版本 2.1.4 发布于 2024/12/19(1.9 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/01/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。