{"id":18523617,"url":"https://github.com/mongoosejs/kareem","last_synced_at":"2025-04-12T06:00:33.760Z","repository":{"id":24444823,"uuid":"27846925","full_name":"mongoosejs/kareem","owner":"mongoosejs","description":"Next-generation take on pre/post function hooks","archived":false,"fork":false,"pushed_at":"2025-03-19T21:01:30.000Z","size":200,"stargazers_count":82,"open_issues_count":5,"forks_count":9,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-05T03:01:01.538Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mongoosejs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-12-11T00:41:29.000Z","updated_at":"2024-09-18T08:22:45.000Z","dependencies_parsed_at":"2024-06-18T13:35:35.994Z","dependency_job_id":"1ad71c38-b214-464c-abba-6fd046c0b7c1","html_url":"https://github.com/mongoosejs/kareem","commit_stats":{"total_commits":188,"total_committers":6,"mean_commits":"31.333333333333332","dds":0.5797872340425532,"last_synced_commit":"a5326367ddfa0a4480b843283a4fb74bfe56cc5d"},"previous_names":["vkarpov15/kareem"],"tags_count":56,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoosejs%2Fkareem","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoosejs%2Fkareem/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoosejs%2Fkareem/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoosejs%2Fkareem/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mongoosejs","download_url":"https://codeload.github.com/mongoosejs/kareem/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248525137,"owners_count":21118616,"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":[],"created_at":"2024-11-06T17:36:35.173Z","updated_at":"2025-04-12T06:00:33.621Z","avatar_url":"https://github.com/mongoosejs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kareem\n\n  [![Build Status](https://github.com/mongoosejs/kareem/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/mongoosejs/kareem/actions/workflows/test.yml)\n  \u003c!--[![Coverage Status](https://img.shields.io/coveralls/vkarpov15/kareem.svg)](https://coveralls.io/r/vkarpov15/kareem)--\u003e\n\nRe-imagined take on the [hooks](http://npmjs.org/package/hooks) module, meant to offer additional flexibility in allowing you to execute hooks whenever necessary, as opposed to simply wrapping a single function.\n\nNamed for the NBA's all-time leading scorer Kareem Abdul-Jabbar, known for his mastery of the [hook shot](http://en.wikipedia.org/wiki/Kareem_Abdul-Jabbar#Skyhook)\n\n\u003cimg src=\"http://upload.wikimedia.org/wikipedia/commons/0/00/Kareem-Abdul-Jabbar_Lipofsky.jpg\" width=\"220\"\u003e\n\n# API\n\n## pre hooks\n\nMuch like [hooks](https://npmjs.org/package/hooks), kareem lets you define\npre and post hooks: pre hooks are called before a given function executes.\nUnlike hooks, kareem stores hooks and other internal state in a separate\nobject, rather than relying on inheritance. Furthermore, kareem exposes\nan `execPre()` function that allows you to execute your pre hooks when\nappropriate, giving you more fine-grained control over your function hooks.\n\n\n#### It runs without any hooks specified\n\n```javascript\nhooks.execPre('cook', null, function() {\n  // ...\n});\n```\n\n#### It runs basic serial pre hooks\n\npre hook functions take one parameter, a \"done\" function that you execute\nwhen your pre hook is finished.\n\n\n```javascript\nvar count = 0;\n\nhooks.pre('cook', function(done) {\n  ++count;\n  done();\n});\n\nhooks.execPre('cook', null, function() {\n  assert.equal(1, count);\n});\n```\n\n#### It can run multipe pre hooks\n\n```javascript\nvar count1 = 0;\nvar count2 = 0;\n\nhooks.pre('cook', function(done) {\n  ++count1;\n  done();\n});\n\nhooks.pre('cook', function(done) {\n  ++count2;\n  done();\n});\n\nhooks.execPre('cook', null, function() {\n  assert.equal(1, count1);\n  assert.equal(1, count2);\n});\n```\n\n#### It can run fully synchronous pre hooks\n\nIf your pre hook function takes no parameters, its assumed to be\nfully synchronous.\n\n\n```javascript\nvar count1 = 0;\nvar count2 = 0;\n\nhooks.pre('cook', function() {\n  ++count1;\n});\n\nhooks.pre('cook', function() {\n  ++count2;\n});\n\nhooks.execPre('cook', null, function(error) {\n  assert.equal(null, error);\n  assert.equal(1, count1);\n  assert.equal(1, count2);\n});\n```\n\n#### It properly attaches context to pre hooks\n\nPre save hook functions are bound to the second parameter to `execPre()`\n\n\n```javascript\nhooks.pre('cook', function(done) {\n  this.bacon = 3;\n  done();\n});\n\nhooks.pre('cook', function(done) {\n  this.eggs = 4;\n  done();\n});\n\nvar obj = { bacon: 0, eggs: 0 };\n\n// In the pre hooks, `this` will refer to `obj`\nhooks.execPre('cook', obj, function(error) {\n  assert.equal(null, error);\n  assert.equal(3, obj.bacon);\n  assert.equal(4, obj.eggs);\n});\n```\n\n#### It can execute parallel (async) pre hooks\n\nLike the hooks module, you can declare \"async\" pre hooks - these take two\nparameters, the functions `next()` and `done()`. `next()` passes control to\nthe next pre hook, but the underlying function won't be called until all\nasync pre hooks have called `done()`.\n\n\n```javascript\nhooks.pre('cook', true, function(next, done) {\n  this.bacon = 3;\n  next();\n  setTimeout(function() {\n    done();\n  }, 5);\n});\n\nhooks.pre('cook', true, function(next, done) {\n  next();\n  var _this = this;\n  setTimeout(function() {\n    _this.eggs = 4;\n    done();\n  }, 10);\n});\n\nhooks.pre('cook', function(next) {\n  this.waffles = false;\n  next();\n});\n\nvar obj = { bacon: 0, eggs: 0 };\n\nhooks.execPre('cook', obj, function() {\n  assert.equal(3, obj.bacon);\n  assert.equal(4, obj.eggs);\n  assert.equal(false, obj.waffles);\n});\n```\n\n#### It supports returning a promise\n\nYou can also return a promise from your pre hooks instead of calling\n`next()`. When the returned promise resolves, kareem will kick off the\nnext middleware.\n\n\n```javascript\nhooks.pre('cook', function() {\n  return new Promise(resolve =\u003e {\n    setTimeout(() =\u003e {\n      this.bacon = 3;\n      resolve();\n    }, 100);\n  });\n});\n\nvar obj = { bacon: 0 };\n\nhooks.execPre('cook', obj, function() {\n  assert.equal(3, obj.bacon);\n});\n```\n\n## post hooks\n\nacquit:ignore:end\n\n#### It runs without any hooks specified\n\n```javascript\nhooks.execPost('cook', null, [1], function(error, eggs) {\n  assert.ifError(error);\n  assert.equal(1, eggs);\n  done();\n});\n```\n\n#### It executes with parameters passed in\n\n```javascript\nhooks.post('cook', function(eggs, bacon, callback) {\n  assert.equal(1, eggs);\n  assert.equal(2, bacon);\n  callback();\n});\n\nhooks.execPost('cook', null, [1, 2], function(error, eggs, bacon) {\n  assert.ifError(error);\n  assert.equal(1, eggs);\n  assert.equal(2, bacon);\n});\n```\n\n#### It can use synchronous post hooks\n\n```javascript\nvar execed = {};\n\nhooks.post('cook', function(eggs, bacon) {\n  execed.first = true;\n  assert.equal(1, eggs);\n  assert.equal(2, bacon);\n});\n\nhooks.post('cook', function(eggs, bacon, callback) {\n  execed.second = true;\n  assert.equal(1, eggs);\n  assert.equal(2, bacon);\n  callback();\n});\n\nhooks.execPost('cook', null, [1, 2], function(error, eggs, bacon) {\n  assert.ifError(error);\n  assert.equal(2, Object.keys(execed).length);\n  assert.ok(execed.first);\n  assert.ok(execed.second);\n  assert.equal(1, eggs);\n  assert.equal(2, bacon);\n});\n```\n\n#### It supports returning a promise\n\nYou can also return a promise from your post hooks instead of calling\n`next()`. When the returned promise resolves, kareem will kick off the\nnext middleware.\n\n\n```javascript\nhooks.post('cook', function(bacon) {\n  return new Promise(resolve =\u003e {\n    setTimeout(() =\u003e {\n      this.bacon = 3;\n      resolve();\n    }, 100);\n  });\n});\n\nvar obj = { bacon: 0 };\n\nhooks.execPost('cook', obj, obj, function() {\n  assert.equal(obj.bacon, 3);\n});\n```\n\n## wrap()\n\nacquit:ignore:end\n\n#### It wraps pre and post calls into one call\n\n```javascript\nhooks.pre('cook', true, function(next, done) {\n  this.bacon = 3;\n  next();\n  setTimeout(function() {\n    done();\n  }, 5);\n});\n\nhooks.pre('cook', true, function(next, done) {\n  next();\n  var _this = this;\n  setTimeout(function() {\n    _this.eggs = 4;\n    done();\n  }, 10);\n});\n\nhooks.pre('cook', function(next) {\n  this.waffles = false;\n  next();\n});\n\nhooks.post('cook', function(obj) {\n  obj.tofu = 'no';\n});\n\nvar obj = { bacon: 0, eggs: 0 };\n\nvar args = [obj];\nargs.push(function(error, result) {\n  assert.ifError(error);\n  assert.equal(null, error);\n  assert.equal(3, obj.bacon);\n  assert.equal(4, obj.eggs);\n  assert.equal(false, obj.waffles);\n  assert.equal('no', obj.tofu);\n\n  assert.equal(obj, result);\n});\n\nhooks.wrap(\n  'cook',\n  function(o, callback) {\n    assert.equal(3, obj.bacon);\n    assert.equal(4, obj.eggs);\n    assert.equal(false, obj.waffles);\n    assert.equal(undefined, obj.tofu);\n    callback(null, o);\n  },\n  obj,\n  args);\n```\n\n## createWrapper()\n\n#### It wraps wrap() into a callable function\n\n```javascript\nhooks.pre('cook', true, function(next, done) {\n  this.bacon = 3;\n  next();\n  setTimeout(function() {\n    done();\n  }, 5);\n});\n\nhooks.pre('cook', true, function(next, done) {\n  next();\n  var _this = this;\n  setTimeout(function() {\n    _this.eggs = 4;\n    done();\n  }, 10);\n});\n\nhooks.pre('cook', function(next) {\n  this.waffles = false;\n  next();\n});\n\nhooks.post('cook', function(obj) {\n  obj.tofu = 'no';\n});\n\nvar obj = { bacon: 0, eggs: 0 };\n\nvar cook = hooks.createWrapper(\n  'cook',\n  function(o, callback) {\n    assert.equal(3, obj.bacon);\n    assert.equal(4, obj.eggs);\n    assert.equal(false, obj.waffles);\n    assert.equal(undefined, obj.tofu);\n    callback(null, o);\n  },\n  obj);\n\ncook(obj, function(error, result) {\n  assert.ifError(error);\n  assert.equal(3, obj.bacon);\n  assert.equal(4, obj.eggs);\n  assert.equal(false, obj.waffles);\n  assert.equal('no', obj.tofu);\n\n  assert.equal(obj, result);\n});\n```\n\n## clone()\n\nacquit:ignore:end\n\n#### It clones a Kareem object\n\n```javascript\nvar k1 = new Kareem();\nk1.pre('cook', function() {});\nk1.post('cook', function() {});\n\nvar k2 = k1.clone();\nassert.deepEqual(Array.from(k2._pres.keys()), ['cook']);\nassert.deepEqual(Array.from(k2._posts.keys()), ['cook']);\n```\n\n## merge()\n\n#### It pulls hooks from another Kareem object\n\n```javascript\nvar k1 = new Kareem();\nvar test1 = function() {};\nk1.pre('cook', test1);\nk1.post('cook', function() {});\n\nvar k2 = new Kareem();\nvar test2 = function() {};\nk2.pre('cook', test2);\nvar k3 = k2.merge(k1);\nassert.equal(k3._pres.get('cook').length, 2);\nassert.equal(k3._pres.get('cook')[0].fn, test2);\nassert.equal(k3._pres.get('cook')[1].fn, test1);\nassert.equal(k3._posts.get('cook').length, 1);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmongoosejs%2Fkareem","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmongoosejs%2Fkareem","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmongoosejs%2Fkareem/lists"}