2021.6.2200 • Published 3 years ago

@kanzhucai/util v2021.6.2200

Weekly downloads
8
License
ISC
Repository
github
Last release
3 years ago

newf

When you use typescript, you try receive some object from request body or a form. It will contain some spam column property you don't like want, and some time it's very dangerous. For example, your db table contain a userId or a verifyCode, it's secrecy, you don't want user change it. And you lazy! you just use Object.assign to copy properties from form body to the table entity, then save it. Ok, if the data user send to you excluded the securityColumn, nothing happen. But if they request a body contain the column, you'll save and change it at db table.

So, this function I used to fill and filter it, only take Class clear I want.

Your Class's property need to assign a initial value, or the function won't effect, because of feature of javascript standard.

Should only worked on typescript language.

example

class User{
    id:number = 0;
    name:string = "";
}
function main(){
    const user = new User();
    user.age = 18;
    user.id = 1;
    user.name = "bill";
    console.log(user)
    // {
    //     "id": 1,
    //     "name": "bill",
    //     "age": 18
    // }
    const sample = newf(User);
    console.log(user)
    // {
    //     "id": 0,
    //     "name": "",
    // }
    const want = newf(User,user);
    // {
    //     "id": 1,
    //     "name": "bill",
    // }
}
2021.6.2200

3 years ago

2021.1.7

3 years ago

2021.1.6

3 years ago

2021.1.3

3 years ago