Lightweight, solid, framework agnostic and easy to use library which provides reflection features to deal with Cross Cutting Concerns and improve modularity in your code.
Try it!
Clone the repo:
git clone https://github.com/k1r0s/kaop.git
Run showcase:
node showcase.js
Run tests:
npm test
Features, from bottom to top.
- ES6 class alternative
- Inheritance
- Composition
- Method Overriding
- Dependency Injection
- AOP Extensions
Get started
npm install kaop --save
// Array.prototype.includes() polyfillconst MyArray = ; const arr = 1 2 3 4; arr; // truearr; // false
Easy, right? lets try something else.
Say that for calculating John Doe's age we have to waste a lot of resources so we want to apply memoization to one method.
// create a spy functionconst methodSpy = jest; const Person = // ... test it;
// we're creating a group of advices which provides memoizationconst Memoize = { const CACHE_KEY = "#CACHE"; return read: reflect write: reflect };
O_O what are advices?
Advices are pieces of code that can be plugged in several places within OOP paradigm like 'beforeMethod', 'afterInstance'.. etc. Advices are used to change, extend, modify the behavior of methods and constructors non-invasively.
If you're looking for better experience using advices and vanilla ES6 classes you should check kaop-ts which has a nicer look with ES7 Decorators.
Advices
right?
But this library isn't only about This library tries to provide an alternative to ES6 class constructors which can be nicer in some way but do not allow reflection (it seems that ES7 Decorators are the way to go but they're still experimental) compared to createClass
prototyping shell which provides a nice interface to put pieces of code that allows declarative Inversion of Control.
Once you have reflection...
Building Dependency Injection system is trivial. For example:
// having the following serviceconst Storage = ; // you declare a singleton provider (you can use a factory for multiple instances)const StorageProvider = provider; // and then you inject it in several classesconst Model1 = ; const Model2 = ; const m1 = ;const m2 = ; m1storage instanceof Storage // truem2storage instanceof Storage // true // and they are the same instance coz `StorageProvider` returns a single instance `singleton`
TODO
Way more documentation about Aspect Oriented, Dependency Injection, Composition, Asynchronous Advices, etc.
Tests are the most useful documentation nowadays, that should change soon.