https://github.com/geut/nanocontext
Small module to create a stateful context tree object to extend through decorators.
https://github.com/geut/nanocontext
context decorators freeze state tree
Last synced: about 1 month ago
JSON representation
Small module to create a stateful context tree object to extend through decorators.
- Host: GitHub
- URL: https://github.com/geut/nanocontext
- Owner: geut
- License: mit
- Created: 2019-10-29T19:07:54.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2023-04-21T13:51:59.000Z (over 2 years ago)
- Last Synced: 2025-08-30T22:52:55.153Z (about 1 month ago)
- Topics: context, decorators, freeze, state, tree
- Language: JavaScript
- Size: 124 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# nanocontext
[](https://travis-ci.com/geut/nanocontext)
[](https://standardjs.com)
[](https://github.com/RichardLitt/standard-readme)> 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.```
$ npm install nanocontext
``````javascript
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.
```javascript
const child = ctx.clone()
```Alternative: `getClone(ctx, source)`
:bug: If you found an issue we encourage you to report it on [github](https://github.com/geut/nanocontext/issues). Please specify your OS and the actions to reproduce it.
:busts_in_silhouette: Ideas and contributions to the project are welcome. You must follow this [guideline](https://github.com/geut/nanocontext/blob/master/CONTRIBUTING.md).
## License
MIT © A [**GEUT**](http://geutstudio.com/) project