作为前端,由于自己对代码变量有洁癖~~只用驼峰,看不得下划线和驼峰混用,看着揪心。
所以对于后端发过来的各种乱七八糟的变量,比如单字母,下划线变量,还有一些奇怪的数据(加密,二进制等),对于老接口,并不是接口协商能解决的,写了个小工具,算比较优雅的解决了~~
用法:
import { property, toClass, toPlain } from 'class-convert';
class UserModel {
@property('i')
id: number;
@property()
name: string;
}
const userRaw = {
i: 1234,
name: 'name',
};
// use toClass to convert plain object to class
const userModel = toClass(userRaw, UserModel);
// you will get a class, just like below one
{
id: 1234,
name: 'name',
}
const userModel = {
id: 1234,
name: 'name',
};
// use toPlain to convert plain object to class
const userRaw = toPlain(userModel, UserModel);
// you will get a class, just like below one
{
i: 1234,
name: 'name',
}
支持双向转换~ 详情请参阅: https://www.npmjs.com/package/class-converter