Skip to content

Latest commit

 

History

History
87 lines (49 loc) · 2.43 KB

README.md

File metadata and controls

87 lines (49 loc) · 2.43 KB

nanocontext

Build Status JavaScript Style Guide standard-readme compliant

Small module to create a stateful context tree object to extend through decorators.

It provides a way to build a context object that you can share, extend and be sure that only the root can modify it.

Features:

  • Creates light secure copies of the root context to share.

Install

$ npm install nanocontext

Usage

import { nanocontext } from 'nanocontext'

const ctxRoot = nanocontext({ name: 'alice' })

ctxRoot.hello = () => 'hello from root'

console.log(ctxRoot.hello()) // hello from root

const ctxChild = ctxRoot.clone()

console.log(ctxChild.hello()) // hello from root (by inheritance)

ctxChild.hello = () => 'hello from child'

console.log(ctxRoot.hello()) // hello from root (it doesn't change)

console.log(ctxChild.hello()) // hello from child

API

const ctx = nanocontext(source, options)

It creates a new nanocontext instance based on an initial object.

  • source = {}: The initial source context object.

Options can be:

  • builtInMethods = true: Defines a set of built-in methods to work with the context. You can disabled and access to these methods from generic functions.
  • parent = null: Parent object to inherit.

ctx.root

Return the root context.

Alternative: getRoot(ctx)

ctx.parent

Return the parent context.

Alternative: getParent(ctx)

ctx.clone(source = ctx)

Return a new context inherit from the current context (ctx) or from a new object.

const child = ctx.clone()

Alternative: getClone(ctx, source)

Issues

🐛 If you found an issue we encourage you to report it on github. Please specify your OS and the actions to reproduce it.

Contributing

👥 Ideas and contributions to the project are welcome. You must follow this guideline.

License

MIT © A GEUT project