@gallant/annotate
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

@gallant/annotate

Tools for working with annotations in JavaScript/TypeScript classes

Installation

npm install --save @gallant/annotate

Usage

import { Annotate } from '@gallant/annotate';

export const HookSchema = Annotate.schema<HookOptions>( Symbol( 'Hook' ), true, false );

export interface HookOptions {
    name: string;
}

export function Hook ( hook ?: HookOptions | string ) {
    return ( target : any, property : string | symbol ) => {
        if ( !hook ) {
            hook = property.toString();

            // If the property name starts with 'on' and the next letter is uppercase
            if ( hook.startsWith( 'on' ) && hook[ 2 ] === hook[ 2 ].toUpperCase() ) {
                hook = hook[ 2 ].toLowerCase() + hook.slice( 3 );
            }
        }
    
        if ( typeof hook === 'string' ) {
            hook = { name: hook };
        }

        Annotate.add( target, property, HookSchema, hook );
    };
}

class Component {
    @Hook()
    onInit () { }
}

console.log( Annotate.get( Component.prototype, HookSchema ) );

Package Sidebar

Install

npm i @gallant/annotate

Weekly Downloads

0

Version

0.0.1

License

ISC

Unpacked Size

38.4 kB

Total Files

23

Last publish

Collaborators

  • pedromsilva