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

ecmas-annotations

v1.0.2

Published

Annotation parser library for ES2014/ES2015, ES5/ES6

Downloads

145

Readme

ecmas-annotations

Gitter NPM version NPM downloads Build status Dependency Status Code Climate

Overview

This is a Node.js library which allows you to add annotations describing metdata data about constructors, methods, and properties within javascript files.

Example:

/**
 * @MyConstructorAnnotation("some value")
 */
function HelloWorld(){}

HelloWorld.prototype = {
    
    /**
     * @MyPropertyAnnotation(foo=[1,2,3,4], bar={"hello":"there"})
     */
    myProperty: 'foo',

    /**
     * @MyMethodAnnotation("foobar", something="hello")
     */
    myMethod: function(){

    }
}

Installation

Need Node >= 4.0.0

> npm install ecmas-annotations

Usage

Full example in example folder (node main.js to test is).

Create an Annotation

// my-constructor-annotation.js
// ----------------------------
'use strict'
const Annotation = require('esmac-annotations').Annotation;

module.exports = class MyConstructorAnnotation extends Annotation {

    /**
     * The possible targets
     *
     * (Annotation.DEFINITION, Annotation.CONSTRUCTOR, Annotation.PROPERTY, Annotation.METHOD)
     *
     * @type {Array}
     */
    static get targets() { return [Annotation.CONSTRUCTOR] }

    /**
     * Constructor to add attributes
     * @type {Array}
     */
    constructor(data, filePath){
      super(data, filePath)
      /**
       * The main value
       *
       * @type {String}
       */
      value = 'default value'

      /**
       * An additional attribute
       *
       * @type {String}
       */
      sample = 'default value for sample'
    
    }
    
    /**
     * Optional initialization method that
     * can be used to transform data
     *
     * @param  {Object} data
     * @return {void}
     */
    init: function(data){
        // do something with data
    }
    
});

Add the Annotation to a File

// my-sample.js
// ------------

/**
 * @MyConstructorAnnotation("some value", sample="here is an attribute value")
 */
function MySample(){}

Parse the Annotation

// my-parser.js
// ------------

const path = require('path')
const annotations = require('ecmas-annotations')

// create the registry
const registry = new annotations.Registry()

// add annotations to the registry
registry.registerAnnotation(path.join(__dirname, 'my-constructor-annotation'))

// create the annotation reader
const reader = new annotations.Reader(registry)

// parse the annotations from a file, default parse ES6 file, Reader.ES5 to force ES5
reader.parse(path.join(__dirname, 'my-sample.js'), Reader.ES6)

// get the annotations
const definitionAnnotations = reader.definitionAnnotations
const constructorAnnotations = reader.constructorAnnotations
const methodAnnotations = reader.methodAnnotations
const propertyAnnotations = reader.propertyAnnotations

// loop through and handle the annotations
constructorAnnotations.forEach(function(annotation){

    // @MyConstructorAnnotation
    if (annotation.annotation === 'MyConstructorAnnotation'){

        // do something with the annotation data
        console.log(annotation.target); // -> "MySample"
        console.log(annotation.value); // -> "some value"
        console.log(annotation.sample); // -> "here is an attribute value"
    }

});

Supported Attribute Types

// string
@MyAnnotation(foo="a string")

// boolean
@MyAnnotation(foo=true)

// array
@MyAnnotation(foo=[1,2,3,4])

// object
@MyAnnotation(foo={hi:"there"})

// nested annotations
@MyAnnotation(foo=@MyNestedAnnotation("this is nested", foo=true))

// array of nested annotations
@MyAnnotation(foo=[@MyNestedAnnotation("nested 1"), @MyNestedAnnotation("nested 2")])

License

MIT