{"id":21654647,"url":"https://github.com/localvoid/incode","last_synced_at":"2025-06-20T22:40:46.385Z","repository":{"id":57273292,"uuid":"108086091","full_name":"localvoid/incode","owner":"localvoid","description":":syringe: Code injector","archived":false,"fork":false,"pushed_at":"2018-05-18T14:47:08.000Z","size":69,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-25T06:25:20.047Z","etag":null,"topics":["codegen","injector"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/localvoid.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}},"created_at":"2017-10-24T06:28:15.000Z","updated_at":"2018-05-18T14:47:09.000Z","dependencies_parsed_at":"2022-09-17T02:00:32.268Z","dependency_job_id":null,"html_url":"https://github.com/localvoid/incode","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/localvoid%2Fincode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/localvoid%2Fincode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/localvoid%2Fincode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/localvoid%2Fincode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/localvoid","download_url":"https://codeload.github.com/localvoid/incode/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244554121,"owners_count":20471173,"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":["codegen","injector"],"created_at":"2024-11-25T08:28:36.365Z","updated_at":"2025-03-20T04:42:07.031Z","avatar_url":"https://github.com/localvoid.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [incode](https://github.com/localvoid/incode) \u0026middot; [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/localvoid/incode/blob/master/LICENSE) [![npm version](https://img.shields.io/npm/v/incode.svg)](https://www.npmjs.com/package/incode) [![codecov](https://codecov.io/gh/localvoid/incode/branch/master/graph/badge.svg)](https://codecov.io/gh/localvoid/incode) [![CircleCI Status](https://circleci.com/gh/localvoid/incode.svg?style=shield\u0026circle-token=:circle-token)](https://circleci.com/gh/localvoid/incode) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/localvoid/incode)\n\n`incode` is a library for building code injectors.\n\n## Example\n\nCode with injectable regions:\n\n```js\nclass User {\n  // inj:emit(\"User\", \"pck\")\n  // inj:end\n}\n```\n\nCode injector:\n\n```js\nimport { createDirectiveMatcher, inject } from \"incode\";\n\nconst s = inject(\n  text, // code with injectable regions\n  createDirectiveMatcher(\"inj\"),\n  (region) =\u003e {\n    return `${region.args[1]}() { console.log(\"${region.args[0]} injected method\"); }`;\n  },\n);\n```\n\nResult after injection:\n\n```js\n// inj:assign({ schema: \"User\" })\nclass User {\n  // inj:emit(\"pck\")\n  pck() { console.log(\"User injected method\"); }\n  // inj:end\n}\n```\n\n## Features\n\n- Block-scoped variables\n- Indentation autodection for injectable regions\n- Automatic removal of existing code in injectable regions\n\n## Directives\n\n- `begin` - begin local scope\n- `end` - end region\n- `assign(data: JSON)` - assign data to a local scope `Object.assign`\n- `merge(data: JSON)` - merge data to a local scope `_.merge`\n- `emit(...args: Array\u003cJSON\u003e)` - emit code\n\n## API\n\n```ts\nfunction createDirectiveMatcher(prefix: string): RegExp;\n```\n\n`createDirectiveMatcher` creates a `RegExp` object that will be used as a directive matcher.\n\n```ts\ninterface InjectableRegion {\n  readonly args: any[];\n  readonly data: {};\n  readonly padding: string;\n  readonly start: number;\n  readonly end: number;\n}\n\nfunction extractRegions(\n  text: string,\n  directiveMatcher: RegExp,\n  data = {},\n): InjectableRegion[];\n```\n\n`extractRegions` extracts `InjectableRegions` from `text`.\n\n```ts\nfunction inject(\n  text: string,\n  directiveMatcher: RegExp,\n  cb: (region: InjectableRegion) =\u003e string,\n  data = {},\n): string;\n```\n\n`inject` invokes `cb` function and injects its result into a text.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flocalvoid%2Fincode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flocalvoid%2Fincode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flocalvoid%2Fincode/lists"}