All Projects → k1r0s → kaop

k1r0s / kaop

Licence: other
Advanced OOP Library with createClass, inheritance, providers, injectors, advices which enables handy Inversion of Control techniques

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to kaop

Aspect4Delphi
Concepts of aspect-oriented programming (AOP) in Delphi.
Stars: ✭ 28 (-30%)
Mutual labels:  aspect-oriented-programming, aop-architecture
OOP-In-CPlusPlus
An Awesome Repository On Object Oriented Programming In C++ Language. Ideal For Computer Science Undergraduates, This Repository Holds All The Resources Created And Used By Me - Code & Theory For One To Master Object Oriented Programming. Filled With Theory Slides, Number Of Programs, Concept-Clearing Projects And Beautifully Explained, Well Doc…
Stars: ✭ 27 (-32.5%)
Mutual labels:  inheritance, object-oriented-programming
AspecTS
An aspect-oriented programming library implemented in TypeScript
Stars: ✭ 21 (-47.5%)
Mutual labels:  aspect-oriented-programming, aop-aspects
Clean Code Dotnet
🛁 Clean Code concepts and tools adapted for .NET
Stars: ✭ 4,425 (+10962.5%)
Mutual labels:  composition, inheritance
Mimick.Fody
An integrated framework for dependency injection and aspect-oriented processing.
Stars: ✭ 15 (-62.5%)
Mutual labels:  dependency-injection, aspect-oriented-programming
vesselize
⛵ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (-45%)
Mutual labels:  dependency-injection, composition
java-notes
Complete Java Note for colleges in Nepal.
Stars: ✭ 30 (-25%)
Mutual labels:  inheritance, object-oriented-programming
Clean Code Javascript
🛁 Clean Code concepts adapted for JavaScript
Stars: ✭ 62,912 (+157180%)
Mutual labels:  composition, inheritance
Asmin
Asmin is .NET CORE project infrastructure, to get a quick start on the project.
Stars: ✭ 89 (+122.5%)
Mutual labels:  dependency-injection, aspect-oriented-programming
Stampit
OOP is better with stamps: Composable object factories.
Stars: ✭ 3,021 (+7452.5%)
Mutual labels:  dependency-injection, composition
clean-code-javascript-ko
🛁 Clean Code concepts adapted for JavaScript - 한글 번역판 🇰🇷
Stars: ✭ 1,767 (+4317.5%)
Mutual labels:  composition, inheritance
NoMansWallpaperApp
Looking for your next No Man's Sky wallpaper?
Stars: ✭ 35 (-12.5%)
Mutual labels:  dependency-injection
HoaLibrary-Max
🔉 HoaLibrary for Max
Stars: ✭ 70 (+75%)
Mutual labels:  composition
Edward-the-App
Write your first novel with the world's most helpful writing tool. (Out of business as of Dec 2021)
Stars: ✭ 55 (+37.5%)
Mutual labels:  composition
awilix-express
Awilix helpers/middleware for Express
Stars: ✭ 100 (+150%)
Mutual labels:  dependency-injection
AnnotationInject
Compile-time Swift dependency injection annotations
Stars: ✭ 40 (+0%)
Mutual labels:  dependency-injection
swift-di-explorations
Functional DI explorations in Swift
Stars: ✭ 28 (-30%)
Mutual labels:  dependency-injection
Sushi-3.1
A GUI framework, all wrapped in rice and seaweed for extra flavor.
Stars: ✭ 25 (-37.5%)
Mutual labels:  object-oriented-programming
justuse
Just use() code from anywhere - a functional import alternative with advanced features like inline version checks, autoreload, module globals injection before import and more.
Stars: ✭ 49 (+22.5%)
Mutual labels:  aspect-oriented-programming
My Android Garage
A quick reference guide for Android development.
Stars: ✭ 66 (+65%)
Mutual labels:  inheritance

kaop

Image travis version Coverage Status dependencies dev-dependencies downloads Known Vulnerabilities

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
import { extend } from 'kaop'

// Array.prototype.includes() polyfill
const MyArray = extend(Array, {
  includes(value) {
    return this.indexOf(value) > -1;
  }
});

const arr = new MyArray(1, 2, 3, 4);

arr.includes(2); // true
arr.includes(5); // 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 function
const methodSpy = jest.fn();

const Person = createClass({
  constructor(name, yearBorn) {
    this.name = name;
    this.age = new Date(yearBorn, 1, 1);
  },

  // note that `sayHello` always calls `veryHeavyCalculation`
  veryHeavyCalculation: [Memoize.read, function() {
      // call spy function
      methodSpy();
      const today = new Date();
      return today.getFullYear() - this.age.getFullYear();
  }, Memoize.write],

  sayHello(){
    return `hello, I'm ${this.name}, and I'm ${this.veryHeavyCalculation()} years old`;
  }
})

// ... test it
it("cache advices should avoid 'veryHeavyCalculation' to be called more than once", () => {
  const personInstance = new Person("John Doe", 1990);
  personInstance.sayHello();
  personInstance.sayHello();
  personInstance.sayHello();
  expect(methodSpy).toHaveBeenCalledTimes(1);
});
// we're creating a group of advices which provides memoization
const Memoize = (function() {
  const CACHE_KEY = "#CACHE";
  return {
    read: reflect.advice(meta => {
      if(!meta.scope[CACHE_KEY]) meta.scope[CACHE_KEY] = {};

      if(meta.scope[CACHE_KEY][meta.key]) {
        meta.result = meta.scope[CACHE_KEY][meta.key];
        meta.break();
      }
    }),
    write: reflect.advice(meta => {
      meta.scope[CACHE_KEY][meta.key] = meta.result;
    })
  }
})();

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.

But this library isn't only about Advices right?

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:

import { createClass, inject, provider } from 'kaop'


// having the following service
const Storage = createClass({
  constructor: function() {
    this.store = {};
  },
  get: function(key){
    return this.store[key];
  },
  set: function(key, val){
    return this.store[key] = val;
  }
});

// you declare a singleton provider (you can use a factory for multiple instances)
const StorageProvider = provider.singleton(Storage);

// and then you inject it in several classes
const Model1 = createClass({
  constructor: [inject.args(StorageProvider), function(_storageInstance) {
    this.storage = _storageInstance;
  }]
});

const Model2 = createClass({
  constructor: [inject.args(StorageProvider), function(_storageInstance) {
    this.storage = _storageInstance;
  }]
});

const m1 = new Model1;
const m2 = new Model2;

m1.storage instanceof Storage // true
m2.storage 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.

Similar resources

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].