Coder Social home page Coder Social logo

behaviors's Introduction

Behaviors

A (really) simple way to check a modules exports in node.js.

This module is useful for projects implementing a plugin-style API, where warning the developer of missing or invalid exports is more friendly than simply starting up a server and letting all hell break loose.

This approach is similar to behaviors in Erlang (hence the name), where a module is checked for required callbacks at compile time. In this case, we check a module when it is require()'d.

Usage

An example behavior description module (it could also just be an object):

exports.id = [Number, String];
exports.name = [String, undefined];
exports.init = Function;
exports.reserved = undefined;

A module implementing this behavior MUST export an id of type Number or String, and an init function. It MUST NOT export 'reserved' (unless it exports undefined as its value). It MAY export name, which must be a string.

Using a behavior description:

var behaviors = require('behaviors');
var plugin_behavior = require('./behaviors/plugin_behavior');

var plugin1 = behaviors.require('./plugins/plugin1', plugin_behavior);

In the above example, an exception will be thrown if plugin1 does not meet the requirements defined in plugin_behavior. The exception message will show some useful information on whether an expected property was the wrong type or missing.

Supported Export Types

  • Boolean
  • Function
  • Number
  • Object
  • String
  • null
  • undefined

Tests

To run the tests you'll need to get testrunner, which is a submodule in this respository. This can be done by the following commands:

git submodule init
git submodule update

The tests can then be run by doing:

node test.js

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.