Socket
Socket
Sign inDemoInstall

base

Package Overview
Dependencies
8
Maintainers
3
Versions
46
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    base

base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting with a handful of common methods, like `set`, `get`, `del` and `use`.


Version published
Weekly downloads
9M
decreased by-19.37%
Maintainers
3
Install size
416 kB
Created
Weekly downloads
 

Package description

What is base?

The 'base' npm package is a framework for rapidly building high-quality node.js applications, using plugins like building blocks. It's designed to be a foundation on which to build complex applications.

What are base's main functionalities?

Plugin Management

Base allows you to easily manage and use plugins, enabling modular application development. Plugins can add specific functionalities to the base application, which can be initialized and used as needed.

const Base = require('base');
const app = new Base();
app.use(require('some-plugin'));
app.init();

Event Handling

Base provides an event handling system. You can define custom events and handlers, making it easy to manage events within your application.

const Base = require('base');
const app = new Base();
app.on('event', () => console.log('Event triggered!'));
app.emit('event');

Task Execution

Base supports defining and running tasks. This feature allows you to organize code into tasks that can be executed on demand, simplifying complex operations into manageable units.

const Base = require('base');
const app = new Base();
app.task('do something', () => console.log('Task done!'));
app.run('do something');

Other packages similar to base

Readme

Source

base NPM version Build Status

base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting with a handful of common methods, like set, get, del and use.

TOC

(TOC generated by verb using markdown-toc)

Install

Install with npm:

$ npm install base --save

Usage

var base = require('base');

inherit

function App() {
  base.call(this);
}
base.extend(App);

var app = new App();
app.set('a', 'b');
app.get('a');
//=> 'b';

instantiate

var app = base();
app.set('foo', 'bar');
console.log(app.foo);
//=> 'bar'

Inherit or instantiate with a namespace

A .namespace() method is exposed on the exported function to allow you to create a custom namespace for setting/getting on the instance.

var Base = require('base')
var base = Base.namespace('cache');

var app = base();
app.set('foo', 'bar');
console.log(app.cache.foo);
//=> 'bar'

API

All of the methods from cache-base are exposed on the base API, as well as the following methods.

Base

Create an instance of Base with config and options.

Params

  • config {Object}: passed to cache-base
  • options {Object}

Example

var app = new Base({baz: 'qux'}, {yeah: 123, nope: 456});

app.set('foo', 'bar');

console.log(app.get('foo')); //=> 'bar'
console.log(app.get('baz')); //=> 'qux'
console.log(app.get('yeah')); //=> undefined

console.log(app.foo); //=> 'bar'
console.log(app.baz); //=> 'qux'
console.log(app.yeah); //=> undefined

console.log(app.options.yeah); //=> 123
console.log(app.options.nope); //=> 456

.is

Set the given name on app._name and app.is* properties. Used for doing lookups in plugins.

Params

  • name {String}
  • returns {Boolean}

Example

app.is('foo');
console.log(app._name);
//=> 'foo'
console.log(app.isFoo);
//=> true
app.is('bar');
console.log(app.isFoo);
//=> true
console.log(app.isBar);
//=> true
console.log(app._name);
//=> 'bar'

.isRegistered

Returns true if a plugin has already been registered on an instance.

Plugin implementors are encouraged to use this first thing in a plugin to prevent the plugin from being called more than once on the same instance.

Params

  • name {String}: The plugin name.
  • register {Boolean}: If the plugin if not already registered, to record it as being registered pass true as the second argument.
  • returns {Boolean}: Returns true if a plugin is already registered.

Events

  • emits: plugin Emits the name of the plugin.

Example

var base = new Base();
base.use(function(app) {
  if (app.isRegistered('myPlugin')) return;
  // do stuff to `app`
});

// to also record the plugin as being registered
base.use(function(app) {
  if (app.isRegistered('myPlugin', true)) return;
  // do stuff to `app`
});

.assertPlugin

Throws an error when plugin name is not registered.

Params

  • name {String}: The plugin name.

Example

var base = new Base();
base.use(function(app) {
  app.assertPlugin('base-foo');
  app.assertPlugin('base-bar');
  app.assertPlugin('base-baz');
});

.use

Define a plugin function to be called immediately upon init. Plugins are chainable and the only parameter exposed to the plugin is the application instance.

Params

  • fn {Function}: plugin function to call
  • returns {Object}: Returns the item instance for chaining.

Events

  • emits: use with no arguments.

Example

var app = new Base()
  .use(foo)
  .use(bar)
  .use(baz)

.lazy

Lazily invoke a registered plugin. Note that this method can only be used with:

  1. plugins that add a single method or property to app
  2. plugins that do not (themselves) add a getter/setter property (they're already lazy)
  3. plugins that do not return a function

Params

  • prop {String}: The name of the property or method added by the plugin.
  • fn {Function}: The plugin function
  • options {Object}: Options to use when the plugin is invoked.
  • returns {Object}: Returns the instance for chaining

Example

app.lazy('store', require('base-store'));

.define

Define a non-enumerable property on the instance. Dot-notation is not supported with define.

Params

  • key {String}: The name of the property to define.
  • value {any}
  • returns {Object}: Returns the instance for chaining.

Events

  • emits: define with key and value as arguments.

Example

// arbitrary `render` function using lodash `template`
define('render', function(str, locals) {
  return _.template(str)(locals);
});

.mixin

Mix property key onto the Base prototype. If base-methods is inherited using Base.extend this method will be overridden by a new mixin method that will only add properties to the prototype of the inheriting application.

Params

  • key {String}
  • val {Object|Array}
  • returns {Object}: Returns the instance for chaining.

.base

Getter/setter for a base (or shared) instance of base applications.

This property only works when a "parent" instance is created on app. If app.parent is defined, the base getter ensures that the base instance is always the very first instance.

Example

var a = new Base();
a.foo = 'bar';

var b = new Base();
b.parent = a;

var c = new Base();
c.parent = b;

console.log(c.foo);
//=> undefined
console.log(c.base.foo);
//=> 'bar'

.use

Static method for adding global plugin functions that will be added to an instance when created.

Params

  • fn {Function}: Plugin function to use on each instance.

Example

Base.use(function(app) {
  app.foo = 'bar';
});
var app = new Base();
console.log(app.foo);
//=> 'bar'

.extend

Static method for inheriting both the prototype and static methods of the Base class. See class-utils for more details.

.Base.mixin

Static method for adding mixins to the prototype. When a function is returned from the mixin plugin, it will be added to an array so it can be used on inheriting classes via Base.mixins(Child).

Params

  • fn {Function}: Function to call

Example

Base.mixin(function fn(proto) {
  proto.foo = function(msg) {
    return 'foo ' + msg;
  };
  return fn;
});

.Base.mixins

Static method for running currently saved global mixin functions against a child constructor.

Params

  • Child {Function}: Constructor function of a child class

Example

Base.extend(Child);
Base.mixins(Child);

.inherit

Similar to util.inherit, but copies all static properties, prototype properties, and descriptors from Provider to Receiver. class-utils for more details.

Test coverage

Statements   : 100% ( 117/117 )
Branches     : 100% ( 31/31 )
Functions    : 100% ( 24/24 )
Lines        : 100% ( 116/116 )

There are a number of different plugins available for extending base. Let us know if you create your own!

  • base-argv: Plugin that post-processes the argv object from simplify how args are mapped to options, tasks… more | homepage
  • base-cli: Plugin for base-methods that maps built-in methods to CLI args (also supports methods from a… more | homepage
  • base-config: base-methods plugin that adds a config method for mapping declarative configuration values to other 'base'… more | homepage
  • base-cwd: Base plugin that adds a getter/setter for the current working directory. | homepage
  • base-data: adds a data method to base-methods. | homepage
  • base-fs: base-methods plugin that adds vinyl-fs methods to your 'base' application for working with the file… more | homepage
  • base-fs-rename: Plugin for 'base' applications that adds a rename method that can be passed to app.dest()more | homepage
  • base-generators: Adds project-generator support to your base application. | homepage
  • base-list: base-runner plugin that prompts the user to choose from a list of registered applications and… more | homepage
  • base-option: Adds a few options methods to base, like option, enable and disable. See the readme… more | homepage
  • base-pipeline: base-methods plugin that adds pipeline and plugin methods for dynamically composing streaming plugin pipelines. | homepage
  • base-pkg: Base plugin for adding a pkg object with get/set methods for getting data from package.json… more | homepage
  • base-plugins: Upgrade's plugin support in base applications to allow plugins to be called any time after… more | homepage
  • base-project: Base plugin that adds a project getter to the instance for getting the name of… more | homepage
  • base-questions: Plugin for base-methods that adds methods for prompting the user and storing the answers on… more | homepage
  • base-runner: Orchestrate multiple instances of base-methods at once. | homepage
  • base-store: Plugin for getting and persisting config values with your base-methods application. Adds a 'store' object… more | homepage
  • base-task: base plugin that provides a very thin wrapper around https://github.com/doowb/composer for adding task methods to… more | homepage
  • base-tree: Add a tree method to generate a hierarchical tree structure representing nested applications and child… more | homepage

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Building docs

Generate readme and API documentation with verb:

$ npm install verb && npm run docs

Or, if verb is installed globally:

$ verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Jon Schlinkert

License

Copyright © 2016 Jon Schlinkert Released under the MIT license.


This file was generated by verb, v0.9.0, on March 11, 2016.

Keywords

FAQs

Last updated on 11 Mar 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc