0.5.5 • Published 4 years ago

@wssz/modeler v0.5.5

Weekly downloads
29
License
MIT
Repository
github
Last release
4 years ago

@wssz/modeler

Easy expandable lib for decorating object's properties.

See my plugins:

Usage

  • Custom decorator
const CustomMarker = defineMarker<String>();
const CustomMarkerWithStaticValue = defineMarker<String>('flag');

class CustomClass {
    @CustomMarker('test')
    @CustomMarkerWithStaticValue()
    p: any;
}

getMarkers(CustomClass)     // class constructor to inspect
    .get('p')               // desired property key
    // data stored of provided decorator
    .get(CustomMarker))     // return 'test'
    // or
    .get(CustomMarkerWithStaticValue))     // return 'flag'

Build-in decorators

  • @Prop(type?)
class Example {
    @Prop()
    simpleType: string;
    
    @Prop(String)
    overwrittenType: any
}

getMarkers(Example).get('simpleType').get(Prop))        // String
getMarkers(Example).get('overwrittenType').get(Prop))   // also String
  • @Items(type?) and readonly @NestedItems
class NestedDate extends ArrayItems {
    @Items(Date)
    items: Date[];
}

class Example {
    @Items()
    simpleType: string[];
    
    @Items(Date)
    dataType: Date[];

    @Items(NestedDate)
    nestedType: Date[][];
}

getMarkers(Example).get('simpleType').get(Items)         // true (if no type specified)
getMarkers(Example).get('dataType').get(Items)           // Date
getMarkers(Example).get('nestedType').get(NestedItems)   // [Markers for each nested level]
getMarkers(Example).get('nestedType')
    .get(NestedItems)[0].get(Items)                      // Date
  • @Attach(decorators: Function[])
class Example {
    @Attach([Prop(), Required()])
    simpleType: string;
}

getMarkers(Example).get('simpleType').get(Prop))        // String
getMarkers(Example).get('simpleType').get(Required))    // true

Methods and build-in decorators

defineMarker(options: OptionsType): Decorator

  • create new decorator

getMarkers(classConstructor | className): Map<propKey, Map<Decorator, OptionsType>>

  • return data stored in class

hasMarkers(classConstructor | className):Boolean

  • return true if there are data stored in class

extractDecoratorMarkers(markers: Map<Decorator, OptionsType>, decorator: Function, defaultValue?: any)

  • return data for given decorator or defaultValue

Decorators

export enum Formats {
	Date = 'date',
	Time = 'time',
	DateTime = 'date-time',
	URI = 'uri',
	Email = 'email',
	Hostname = 'hostname',
	IPv4 = 'ipv4',
	IPv6 = 'ipv6',
	Regex = 'regex'
}

type ExamplesType = {[key: string]: {value: any}};
type BasicType = string | number | boolean | RegExp | Object;
type BasicFunction = () => BasicType;

Required<boolean = true>
Minimum<number>
Maximum<number>
ExclusiveMinimum<number>
ExclusiveMaximum<number>
MultipleOf<number>
MaxLength<number>
MinLength<number>
Format<Formats>
Pattern<RegExp>
Enum<Object | (string | number)[]>
MaxItems<number>
MinItems<number>
Default<BasicType | BasicType[] | BasicFunction>
Example<BasicType>
UniqueItems<boolean = true>
Examples<ExamplesType>
Description<string>
Nullable<boolean = true>
0.5.5

4 years ago

0.5.4

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.5.1

4 years ago

0.3.9

4 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.2

4 years ago

0.3.3

4 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.6

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago