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

meta-json

v0.0.1

Published

JavaScript meta data for runtime behavior and static analysis

Downloads

1

Readme

meta-json

JavaScript meta data for runtime behavior and static analysis

  • Embed meta data declared as JSON within JavaScript source
  • Wrap meta data with a meta function call
  • Include a meta function implementation
  • Utilize meta data at runtime to dictate behavior
  • Parse meta data statically to generate documentation tied directly to runtime behavior

Runtime API

The runtime API consists of a single function: meta, that accepts a single argument: data. To allow for easy static anlysis data must be a valid JSON object literal. data is passed through and can then be used to dictate behavior. meta has no side effects. The purpose of meta is to make parsing meta data easy and to provide a mechanism for runtime transformation of meta data JSON if necessary (see Data Transformation).

The simpliest implementation of meta is:

function meta (data) {
    return data;
}

Static Analysis

The generation of documentation from the meta data is divided into 3 steps:

  1. Parser
The first step in generating documentation using `meta` is the parser. The
parser's only responsibility is to pull the meta data out of the source.
The parser accepts a JavaScript source string and returns an array of meta
data objects.

This project includes a parser implementation.
  1. Processor
The next step is the processor. The processor accepts an array of meta data
objects returned from the parser and reduces it to an object. It may relate
objects to other or fill in missing context or default values. The processor
does whatever needs to be done to prepare the meta data to be handed to the
reporter.

Implementations of processor will be dependent upon the structure or schema
of the meta data. This project does not include a processor implementation.
  1. Reporter
The last step is the reporter. The reporter accepts a meta data object from
the processor and generates a documentation report.

Implementations of reporter will be dependent upon the structure or schema
of the meta data. This project does not inclue a reporter implementation.

Options Pattern

A common pattern in JavaScript is to pass an argument into a function using an object literal. Often this argument is named options. Because meta returns an object, functions that follow this pattern can have their options supplied by meta. Static analysis can easily parse out the same options as runtime evaluation. This ties documentation directly to runtime behavior.

Consider the example below where a method is attached to a class using meta. myClass.method is defined elsewhere and it accepts two arguments. An object, options, and a function which will serve as the resulting method's implementation.

myClass.method(
    meta({
        "name": "set",
        "type": "method",
        "signature": "string, any"
        "description": "Sets the value of a property"
    }),
    function () {
        ...
    }
);

Data Transformation

Sometimes, to be most useful at runtime, meta data needs to be complex values that cannot be declared using JSON. For example a constructor function or regular expression. In these cases the implementation of meta could transform simple values to complex values where needed. It could transform a string module name into a module value using require or create a RegExp instance from a regular expression source string.

Two important aspects of meta data transformation to consider:

  1. meta function is sychronous
  2. Every meta data member should be meaningful as JSON

In or Out of Comments

Sometimes it won't always be feasible to utilized meta data at runtime as in the options pattern example above. Because it is just JavaScript, meta is extremely flexible and can be place anywhere in source. All of the following usages are valid:

meta({
    "path": "some/where/important",
    "awesome": true
});

/*meta({
    "path": "some/where/important",
    "awesome": true
})*/

var config = meta({
    "path": "some/where/important",
    "awesome": true
});

Why JSON?

  • JSON is a subset of JavaScript

    One language to read and write for both implementation and documentation

  • Well suited for static analysis

    There are a plethora of tools for working with JSON in many languages

  • Available at runtime

    JSON is JavaScript so utilizing meta data at runtime is easy and requires no special tools

Why not javadocs?

In the context of JavaScript source, JSON is more machine-readable and human-readable than javadoc comments.

My experience maintaining javadoc comments within JavaScript motivated me to imagine a better way to document JavaScript. Javadoc style comments are good but not the most elegant solution for JavaScript. They are more likely to be maintained than external documentation but can easily become outdated or contain errors because they are not directly tied to runtime behavior.