context-manager

0.2.0 • Public • Published

context-manager NPM version

Manage context for templates.

Install with npm

npm i context-manager --save

Usage

var context = require('context-manager');

API

Context

Create an instance of Context.

.setContext

Add a context level, optionally passing a value to start with.

  • name {String}: The name of the context to add.
  • level {Number}: Numerical value representing the order in which this level should be merged versus other lvl.
  • value {Object}: Optionally pass an object to start with.
context.setContext('locals', {a: 'b'});

.getContext

Get the raw (un-merged) context for name.

  • name {String}: The context to get.
  • returns: {*}
context.setContext('a', {a: 'b'});
context.getContext('a');
// => {a: 'b'}

.extendContext

Extend context name with the given value

  • name {String}
  • key {String}
  • value {Object}
  • returns: {String}
context.setContext('locals', {a: 'b'});

.setLevel

Set the level for a context. This determines the order in which the context will be merged when .calculate() is called.

  • name {String}: The name of the context.
  • level {Number}: The level (number) to set for the level.
if (foo) {
  context.setLevel('locals', 10);
} else {
  context.setLevel('locals', 0);
}

.calculate

Calculate the context, optionally passing a callback fn for sorting. (Note that sorting must be done on levels, not on the context names).

  • keys {String|Array}: Key, or array of keys for context levels to include.
  • fn {Function}: Sort function for determining the order of merging.
app.calculate(['a', 'b'], function(a, b) {
  return app.lvl[a] - app.lvl[a];
});

.resetContexts

Clear all contexts.

Author

Jon Schlinkert

License

Copyright (c) 2015 Jon Schlinkert
Released under the license


This file was generated by verb on February 21, 2015.

Package Sidebar

Install

npm i context-manager

Weekly Downloads

1

Version

0.2.0

License

MIT

Last publish

Collaborators

  • doowb
  • jonschlinkert