1.1.2 • Published 6 years ago

typux v1.1.2

Weekly downloads
14
License
MIT
Repository
github
Last release
6 years ago

DANGER! Under deep development

Typux

Core functions for best code

Attributes (inline usage)

const type = Symbol('type');
const validate = Symbol('validate');

class Entity
{

    @Attribute(type, Number)
    public id : number;

}

class User extends Entity
{

    @Attribute(type, String)
    public name : string

    @Attribute(type, String)
    @Attribute(validate, Email)
    public email : string

}

const userInfo = metadata.getClassInfo(User);
console.log(userInfo.getProperties())
// [
//     PropertyInfo(id), <- Entity property
//     PropertyInfo(name), <- User property
//     PropertyInfo(email), <- User property
// ]

userInfo.getProperties(/** Recursive = true by default */).forEah(prop => {
    if (prop.hasAttribute(type /** Symbol */)) { // id = true, name = true, email = true
        prop.getAttribute(type /** Symbol */) // id = Number, name = String, email = String
    }
    if (prop.hasAttribute(validate /** Symbol */)) { // id = false, name = false, email = true
        prop.getAttribute(validate /** Symbol */) // id = Error, name = Error, email = Email
    }
})

Custom attributes

const EMAIL_SYMBOL = Symbol('validate.email')

// Custom validation function
export const EmailValidator = (value : string) => true;

// Custom attribute
export const Email = () => Attribute(EMAIL_SYMBOL, EmailValidator) as PropertyDecorator;


class User
{
    
    @Email()
    public email : string;
    
}

Custom Class attributes support (Coming soon)

class EmailValidator extends Validator
{
    
    public validate(value : any) {
        ....
    }
    
}

const Email = Attribute(new EmailValidator()) as PropertyDecotator;

class User
{

    @Email
    public email : string;
    
}
1.1.2

6 years ago

1.1.1

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago