{"id":15877903,"url":"https://github.com/k1r0s/kaop","last_synced_at":"2025-06-20T10:08:46.801Z","repository":{"id":57142759,"uuid":"71287686","full_name":"k1r0s/kaop","owner":"k1r0s","description":"Advanced OOP Library with createClass, inheritance, providers, injectors, advices which enables handy Inversion of Control techniques","archived":false,"fork":false,"pushed_at":"2023-01-04T21:36:30.000Z","size":534,"stargazers_count":39,"open_issues_count":4,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T18:50:30.906Z","etag":null,"topics":["aop-architecture","aop-aspects","aspect-oriented-programming","composition","dependency-injection","inheritance","object-oriented-programming","reflection"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/k1r0s.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-18T20:17:41.000Z","updated_at":"2023-08-18T05:54:48.000Z","dependencies_parsed_at":"2023-02-02T20:46:09.937Z","dependency_job_id":null,"html_url":"https://github.com/k1r0s/kaop","commit_stats":null,"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"purl":"pkg:github/k1r0s/kaop","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k1r0s%2Fkaop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k1r0s%2Fkaop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k1r0s%2Fkaop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k1r0s%2Fkaop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/k1r0s","download_url":"https://codeload.github.com/k1r0s/kaop/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k1r0s%2Fkaop/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260924532,"owners_count":23083524,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aop-architecture","aop-aspects","aspect-oriented-programming","composition","dependency-injection","inheritance","object-oriented-programming","reflection"],"created_at":"2024-10-06T02:04:48.661Z","updated_at":"2025-06-20T10:08:41.786Z","avatar_url":"https://github.com/k1r0s.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![kaop](http://i.imgur.com/6biEpsq.png)\n\n[![Image travis](https://travis-ci.org/k1r0s/kaop-ts.svg?branch=master)](https://travis-ci.org/k1r0s/)\n[![version](https://img.shields.io/npm/v/kaop.svg)](https://www.npmjs.com/package/kaop/)\n[![Coverage Status](https://coveralls.io/repos/github/k1r0s/kaop/badge.svg?branch=master)](https://coveralls.io/github/k1r0s/kaop?branch=master)\n[![dependencies](https://david-dm.org/k1r0s/kaop/status.svg)](https://david-dm.org/k1r0s/kaop/status.svg)\n[![dev-dependencies](https://david-dm.org/k1r0s/kaop/dev-status.svg)](https://www.npmjs.com/package/kaop)\n[![downloads](https://img.shields.io/npm/dm/kaop.svg)](https://www.npmjs.com/package/kaop)\n[![Known Vulnerabilities](https://snyk.io/test/npm/kaop/badge.svg)](https://snyk.io/test/npm/kaop)\n\nLightweight, solid, framework agnostic and **easy to use** library which provides reflection features to deal with *Cross Cutting Concerns* and improve modularity in your code.\n\n### Try it!\n\nClone the repo:\n`git clone https://github.com/k1r0s/kaop.git`\n\nRun showcase:\n`node showcase.js`\n\nRun tests:\n`npm test`\n\n### Features, from bottom to top.\n\n- ES6 class alternative\n- Inheritance\n- Composition\n- Method Overriding\n- Dependency Injection\n- AOP Extensions\n\n### Get started\n\n```bash\nnpm install kaop --save\n```\n\n```javascript\nimport { extend } from 'kaop'\n\n// Array.prototype.includes() polyfill\nconst MyArray = extend(Array, {\n  includes(value) {\n    return this.indexOf(value) \u003e -1;\n  }\n});\n\nconst arr = new MyArray(1, 2, 3, 4);\n\narr.includes(2); // true\narr.includes(5); // false\n```\n\nEasy, right? lets try something else.\n\nSay that for calculating John Doe's age we have to waste a lot of resources so we want to apply memoization to one method.\n\n```javascript\n// create a spy function\nconst methodSpy = jest.fn();\n\nconst Person = createClass({\n  constructor(name, yearBorn) {\n    this.name = name;\n    this.age = new Date(yearBorn, 1, 1);\n  },\n\n  // note that `sayHello` always calls `veryHeavyCalculation`\n  veryHeavyCalculation: [Memoize.read, function() {\n      // call spy function\n      methodSpy();\n      const today = new Date();\n      return today.getFullYear() - this.age.getFullYear();\n  }, Memoize.write],\n\n  sayHello(){\n    return `hello, I'm ${this.name}, and I'm ${this.veryHeavyCalculation()} years old`;\n  }\n})\n\n// ... test it\nit(\"cache advices should avoid 'veryHeavyCalculation' to be called more than once\", () =\u003e {\n  const personInstance = new Person(\"John Doe\", 1990);\n  personInstance.sayHello();\n  personInstance.sayHello();\n  personInstance.sayHello();\n  expect(methodSpy).toHaveBeenCalledTimes(1);\n});\n\n```\n\n```javascript\n\n// we're creating a group of advices which provides memoization\nconst Memoize = (function() {\n  const CACHE_KEY = \"#CACHE\";\n  return {\n    read: reflect.advice(meta =\u003e {\n      if(!meta.scope[CACHE_KEY]) meta.scope[CACHE_KEY] = {};\n\n      if(meta.scope[CACHE_KEY][meta.key]) {\n        meta.result = meta.scope[CACHE_KEY][meta.key];\n        meta.break();\n      }\n    }),\n    write: reflect.advice(meta =\u003e {\n      meta.scope[CACHE_KEY][meta.key] = meta.result;\n    })\n  }\n})();\n\n```\n\n### O_O what are advices?\n\nAdvices 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.\n\nIf you're looking for better experience using advices and vanilla ES6 classes you should check [kaop-ts](https://github.com/k1r0s/kaop-ts) which has a nicer look with ES7 Decorators.\n\n### But this library isn't only about `Advices` right?\n\nThis 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__.\n\n### Once you have reflection...\n\nBuilding Dependency Injection system is trivial. For example:\n\n```javascript\nimport { createClass, inject, provider } from 'kaop'\n\n\n// having the following service\nconst Storage = createClass({\n  constructor: function() {\n    this.store = {};\n  },\n  get: function(key){\n    return this.store[key];\n  },\n  set: function(key, val){\n    return this.store[key] = val;\n  }\n});\n\n// you declare a singleton provider (you can use a factory for multiple instances)\nconst StorageProvider = provider.singleton(Storage);\n\n// and then you inject it in several classes\nconst Model1 = createClass({\n  constructor: [inject.args(StorageProvider), function(_storageInstance) {\n    this.storage = _storageInstance;\n  }]\n});\n\nconst Model2 = createClass({\n  constructor: [inject.args(StorageProvider), function(_storageInstance) {\n    this.storage = _storageInstance;\n  }]\n});\n\nconst m1 = new Model1;\nconst m2 = new Model2;\n\nm1.storage instanceof Storage // true\nm2.storage instanceof Storage // true\n\n// and they are the same instance coz `StorageProvider` returns a single instance `singleton`\n```\n\n### TODO\n\nWay more documentation about Aspect Oriented, Dependency Injection, Composition, Asynchronous Advices, etc.\n\nTests are the most useful documentation nowadays, that should change soon.\n\n### Similar resources\n\n- [mgechev/aspect.js](https://github.com/mgechev/aspect.js)\n- [cujojs/meld](https://github.com/cujojs/meld)\n- [kaop-ts](https://github.com/k1r0s/kaop-ts)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fk1r0s%2Fkaop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fk1r0s%2Fkaop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fk1r0s%2Fkaop/lists"}