npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

class-annotations

v0.9.4-beta

Published

A reader annotations from class write with ES6 syntax

Downloads

7

Readme

class-annotations

use a reader of annotations for write you models with ES6 new syntax , easy usage.

Note: A comment not starting with an: "@" character not be considered as an annotation, but may be brought up in the data provided via the table: excludes.

installation

warning use in production is not recommanded while version beta.

usage

index.js

const ClassAnnotations = require('class-annotations')( __dirname ) ;

const annotations = new ClassAnnotations( './src/Foo.js' ) ;

console.log( annotations ) ;

./src/Foo.js


/**
 * @Controller = "Foo"
 */
class Foo {

    /**
     * @TODO: think write unit test ^.^
     */

    /**
     * @Route({
     *      name: "index",
     *      path: "/bar",
     *      methods: ["GET","POST"]
     * })
     */
    bar() {

        // ...
    }

} ;

static directory

class-annotations return a function the arguments 1 should be static directory, the root directory for your next read file.s.

const createClassAnnotations = require('class-annotations') ;

const staticDirectory = __dirname ;

const ClassAnnotations = createClassAnnotations(
    /* your static directory in arg1 */
    staticDirectory
) ;

The static directory should be a absolute path.

auto persist the data type

class-annotations auto convert the data type of your annotations value, but you can remote real value as: valueBrut.

Foo.js


/**
 * @Controller = "Foo"
 */
class Foo {

    /**
     * @TODO: think write unit test ^.^
     */

    /**
     * @shouldBeArray = ["hello", "wold", 42]
     *
     * @shouldBeNumber = 42.24
     *
     * @shouldBeString = "hello world"
     *
     * @ShouldBeParseError = hello world !
     */
    bar() {

        // ...
    }

} ;
ClassAnnotations {

    items: ['Foo'],

    countClass: 1,

    Foo: {

        classname: 'Foo',

        data: {
            Controller: {
                valueBrut: '"Foo"', value: 'Foo'
            }
        }
    } ,

    methods: {

        Foo: [
            {
                classname: 'Foo',
                method: 'bar',
                data: {
                    shouldBeArray: {
                        valueBrut: '["hello", "wold", 42]',
                        value: ["hello", "wold", 42],
                    }
                    shouldBeNumber: {
                        valueBrut: '42.24',
                        value: '42.24'
                    }
                    shouldBeString: {
                        valueBrut: '"hello world"',
                        value: "hello world"
                    }
                    ShouldBeParseError: {
                        valueBrut: "hello world !",
                        value: "Parse Error to line: 7 after opened annotations of class: Foo, error: hello wo..."
                    }
                }
            }
        ]
    }
}

support-value-multilines

class-annotations can read an annotations extends multiline with this format syntax:

Foo.js


/**
 * @Controller = "Foo"
 */
class Foo {

    /**
     * @Route( {
     *      name: "stuff",
     *      path: "/stuff",
     *      method: ['GET','POST']
     * } )
     */
    bar() {

        // ...
    }

} ;

read recursive folders

class-annotations can read a structure folders a filters files not: .js

Admitting this structure:

/project-name
    - index.js

    / src
        - Foo.js
        - Bar.js
        - README.md

        / stuff
            - Foo.js
            - Bar.js

from: index.js

const createClassAnnotations = require('class-annotations') ;

const ClassAnnotations = createClassAnnotations( __dirname ) ;

const annotations = new ClassAnnotations('./src/') ;

annotations:

ClassAnnotations {

    src: ReadDirecory {

        items: ['Foo.js','Bar.js','README.md','stuff'] ,

        'Foo.js': ClassAnnotations {
            // final file class annotations
            // ...
        } ,

        'Bar.js': ClassAnnotations {
            // final file class annotations
            // ...
        } ,

        stuff: ReadDirectory {

            items: ['Foo.js','Bar.js'] ,

            'Foo.js': ClassAnnotations {
                // final file class annotations
                // ...
            } ,

            'Bar.js': ClassAnnotations {
                // final file class annotations
                // ...
            } ,
        }
    }
}

read methods annotations

class-annotations read annotations inside body class and associate annotations to a specific method class.

But you should follow rules associating annotations to a method, for exploit this feature, this rules enable you of write a simply comment in the body class and not considerate as annotation.

A annotations associate to a method should be not contains empty line before header method.

class Foo {

    /**
     * @Test = 35
     */
    bar() {


    }

} ;

inside of exemple above the commentary:

/**
 * @Test = 35
 */

is considerate as associate to index method.

but below bar method do not contains annotation

class Foo {

    /**
     * @Test = 35
     */


    bar() {


    }

} ;

filter methods annotations

after have read a file or structure folders you can have needs use filter for annotation from method by name.

Stuff.js

class Foo {

    /**
     * @a = 1
     * @b = 2
     * @c = 3
     * @d = 4
     */
    index() {


    }
} ;

class Bar {

    /**
     * @a = 4
     * @b = 3
     * @c = 2
     * @d = 1
     */
    index() {

    }
}

annotations output:

ClassAnnotations {

    countClass: 2,

    methods: {

        Foo: [
            {
                classname: "Foo",
                method: "index",
                data: {
                    a: {
                        valueBrut: '1',
                        value: 1
                    } ,
                    b: {
                        valueBrut: '2',
                        value: 2
                    } ,
                    c: {
                        valueBrut: '3',
                        value: 3
                    } ,
                    d: {
                        valueBrut: '4',
                        value: 4
                    }
                }
            } ,
            getWidth: [Function]
        ] ,

        Bar: [
            {
                classname: "Bar",
                method: 'index',
                data: {
                    a: {
                        valueBrut: '4',
                        value: 4
                    } ,
                    b: {
                        valueBrut: '3',
                        value: 3
                    } ,
                    c: {
                        valueBrut: '2',
                        value: 2
                    } ,
                    d: {
                        valueBrut: '1',
                        value: 1
                    }
                }
            },
            getWidth: [Function]
        ]
    }
}

the function getWidth can filter annotation array and contains currently an type because should be getWith as ( matcher ) => object[]

matcher arg1 should be an string or a instanceof RegExp.

If you have detect an bug or anormal behavior with ClassAnnotaions please remote a issues on github