{"id":23785806,"url":"https://github.com/foxfirecodes/method-inject","last_synced_at":"2025-11-10T17:03:37.940Z","repository":{"id":57295949,"uuid":"117909581","full_name":"foxfirecodes/method-inject","owner":"foxfirecodes","description":"A simple system for injecting and transforming methods","archived":false,"fork":false,"pushed_at":"2023-12-22T01:27:47.000Z","size":4,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-25T05:21:09.986Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/foxfirecodes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-18T00:28:28.000Z","updated_at":"2023-12-22T01:27:50.000Z","dependencies_parsed_at":"2024-10-11T03:44:40.137Z","dependency_job_id":"8d3fa388-f927-4813-bf78-93439e22cc3b","html_url":"https://github.com/foxfirecodes/method-inject","commit_stats":{"total_commits":1,"total_committers":1,"mean_commits":1.0,"dds":0.0,"last_synced_commit":"750ab817f9de1be9634b3b19b6d0931266dec103"},"previous_names":["foxfirecodes/method-inject"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foxfirecodes%2Fmethod-inject","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foxfirecodes%2Fmethod-inject/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foxfirecodes%2Fmethod-inject/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foxfirecodes%2Fmethod-inject/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/foxfirecodes","download_url":"https://codeload.github.com/foxfirecodes/method-inject/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240002103,"owners_count":19732164,"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":"2025-01-01T14:07:09.041Z","updated_at":"2025-11-10T17:03:37.888Z","avatar_url":"https://github.com/foxfirecodes.png","language":"JavaScript","readme":"# method-inject\n\n\u003e A simple system for injecting and transforming methods\n\n## Installation\n\n```bash\nyarn add method-inject\n```\n\n```bash\nnpm install --save method-inject\n```\n\n## Usage\n\nTo use method-inject, it's as simple as calling it on a method:\n\n```javascript\nconst inject = require(\"method-inject\");\n\n// Transform the first argument and prepend '[INFO] ' to it\nconst log = inject(console.log).transform(0, (text) =\u003e \"[INFO] \" + text);\n\nlog(\"Hello, there!\");\n// =\u003e [INFO] Hello, there!\n```\n\nThere are several methods, the rest of which are shown below:\n\n```javascript\nconst inject = require(\"method-inject\");\nconst someObject = {\n  buggedMethod(someArg) {\n    console.log(someArg + 1);\n  },\n};\n\n// Hmm, we've been getting `NaN` in the console from someObject.buggedMethod, let's check it out:\nsomeObject.buggedMethod = inject(someObject.buggedMethod, someObject).before(\n  (args) =\u003e console.log(\"buggedMethod called with the following args:\", args)\n);\n// Note that we passed someObject as the second parameter to inject, indicating that the method should be bound to that object.\n\n// Problem code:\nsomeObject.buggedMethod(\"ERRORS\");\n// =\u003e buggedMethod called with the following args: [ 'ERRORS' ]\n// =\u003e NaN\n```\n\n```javascript\nconst inject = require('method-inject');\n\nconst db = {\n    internal: {\n        life: 42\n    },\n    get(key, default) {\n        return this.internal[key] || default;\n    }\n};\n\nif (process.env.DB_VERBOSE) {\n    db.get = inject(db.get, db).after((returnValue, args) =\u003e {\n        console.log(`DB::GET(key=${args[0]},default=${args[1] || 'none'}) -\u003e ${returnValue}`);\n    });\n}\n\n// Assuming DB_VERBOSE=true:\nconsole.log(`The meaning of life is ${db.get('life', -1)}`);\n// =\u003e DB::GET(key=life,default=-1) -\u003e 42\n// =\u003e The meaning of life is 42\n```\n\n```javascript\nconst inject = require(\"method-inject\");\n\nconst multiply = (a, b) =\u003e {\n  return a * b;\n};\n\nconst multiplyAndSquare = inject(multiply).transformOutput(\n  (output) =\u003e output * output\n);\n\n// (2 * 3) * (2 * 3) = 6 * 6 = 36\nconsole.log(multiplyAndSquare(2, 3));\n// =\u003e 36\n```\n\nMethods can also be chained indefinitely:\n\n```javascript\nconst inject = require(\"method-inject\");\n\nconst multiply = (a, b) =\u003e a * b;\nconst addOne = (x) =\u003e x + 1;\nconst square = (x) =\u003e x * x;\n\nconst verboseComplexMath = inject(multiply)\n  .after((output) =\u003e console.log(`COMPLEX_CALC -\u003e ${output}`))\n  .transformOutput(addOne)\n  .transformOutput(square);\n\nconsole.log(verboseComplexMath(4, 5));\n// =\u003e COMPLEX_CALC -\u003e 441\n// =\u003e 441\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoxfirecodes%2Fmethod-inject","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffoxfirecodes%2Fmethod-inject","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoxfirecodes%2Fmethod-inject/lists"}