# decorator-dto

> dto装饰器扩展

Latest version **2.0.2** (published 2022-05-10) · ISC license · 0 weekly downloads

## Install

```sh
npm install decorator-dto
pnpm add decorator-dto
yarn add decorator-dto
bun add decorator-dto
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2022-05-10 |
| First published | 2022-01-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 14.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | larry |
| Maintainers | larry-wz |

## Links

- npm: https://www.npmjs.com/package/decorator-dto
- npm.io page: https://npm.io/package/decorator-dto

## Dependencies (1)

- [deepcopy](https://npm.io/package/deepcopy.md) ^2.1.0

## Recent versions

- 2.0.2 (latest) — 2022-05-10
- 2.0.1 — 2022-03-30
- 2.0.0 — 2022-03-19
- 1.0.10 — 2022-03-17
- 1.0.8 — 2022-02-16
- 1.0.7 — 2022-02-15
- 1.0.6 — 2022-02-15
- 1.0.5 — 2022-02-15
- 1.0.3 — 2022-01-21
- 1.0.2 — 2022-01-21
- 1.0.1 — 2022-01-20
- 1.0.0 — 2022-01-20

## README

## decorator-dto插件使用说明
1. @Dto:用在class的上面，见下面demo
2. @Format(fn):用在class的属性上面，参数为function，fn函数接收当前属性值并进行处理，执行format后将fn返回的值赋值到当前属性上.见下面demo
3. @Link(k,fn):用在class的属性上面，参数分别为string和function，fn函数接收属性k(class未申明的属性)的值并进行处理，执行format后将fn返回的值赋值到当前属性上.见下面demo
4. @OneToOne(k, fn) 用在class的属性上面，参数k(class申明的属性)为string，fn为可选的转换函数,执行format后将属性k值赋值到当前属性上.见下面demo
5. @ManyToOne([K,...],fn) 用在class的属性上面，参数为[k,..],k必须为class申明的属性，fn为可选的转换函数,执行format后判断是否存在数组里的属性，若存在则将任一个值赋值到当前属性上.见下面demo
6. @Property，用在class的属性上面，收集属性字段，当于其他装饰器同时存在时无意义,见下面demo
6. @Inject(k, fn)，用在class的属性上面, 第一个参数为当前类上的属性，可以为数组，在Format、Link、OneToOne、ManyToOne、Inject执行完后Condition执行前执行。
7. @Condition(fn)，用在class的属性上面，参数为function，fn的参数为当前最新对象，fn的返回值必须为boolean,若为true则当前属性显示，反之当前属性不显示,见下面demo
8. warp: 当前class的静态方法用于批量格式化，见下面demo
### 注：
   - Link不能与OneToOne、ManyToOne、Format一起使用在同一属性上。
   - OneToOne与ManyToOne不能相互作用
``` 

@Dto
class Demo extends BaseDto  {

    @Format(val => val + 1)
    test = 1

    @Link('name', val => val + 1)
    test1 = 2;

    @OneToOne('test1')
    test2 = 2;

    @ManyToOne(['name2'])
    test3 = 2;

    @ManyToOne(['name3'], val => val + 1 )
    test4 = 2;

    @OneToOne('name4', val => val + 1)
    @Inject('test4', (v, m) => {
        return v + m
    })
    test5 = 2;

    @Condition(obj => true)
    test6 = 2;
}

// 调用
const demo = new Demo();

const tmp = Demo.warp([{ test: 3, name: 4, name1: 5, name2: 6, name3: 7, name4: 8 }])

console.log(tmp) 
//打印 [
    {
        test: 4,
        test1: 5,
        test2: 5,
        test3: 6,
        test4: 8,
        test5: 17,
        test6: 2
    }
]

demo.name = 2
demo.name1 = 33
demo.name2 = 44
demo.name3 = 55
demo.name4 = 66

const t = demo.format();

console.log(t.test) // 2
console.log(t.test1) // 3
console.log(t.test2) // 33
console.log(t.test3) // 44
console.log(t.test4) // 56 = 55 + 1
console.log(t.test5) // 67 = 66 + 1
console.log(t.test6) // 2


```


## 欢迎提出各种bug,有问题可以直接发送邮件到          `shmilylp9314wz@163.com`

---
_Source: https://npm.io/package/decorator-dto · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
