{"id":28342233,"url":"https://github.com/neplextech/directive-to-hof","last_synced_at":"2025-06-19T14:32:11.709Z","repository":{"id":288641417,"uuid":"968747636","full_name":"neplextech/directive-to-hof","owner":"neplextech","description":"Transform directives into a higher order function","archived":false,"fork":false,"pushed_at":"2025-04-22T14:10:11.000Z","size":19,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-11T10:09:56.443Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://npm.im/directive-to-hof","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/neplextech.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,"zenodo":null}},"created_at":"2025-04-18T16:54:06.000Z","updated_at":"2025-04-22T14:10:15.000Z","dependencies_parsed_at":"2025-04-22T15:26:23.713Z","dependency_job_id":null,"html_url":"https://github.com/neplextech/directive-to-hof","commit_stats":null,"previous_names":["twlite/directive-to-hof","neplextech/directive-to-hof"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/neplextech/directive-to-hof","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neplextech%2Fdirective-to-hof","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neplextech%2Fdirective-to-hof/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neplextech%2Fdirective-to-hof/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neplextech%2Fdirective-to-hof/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/neplextech","download_url":"https://codeload.github.com/neplextech/directive-to-hof/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neplextech%2Fdirective-to-hof/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260769112,"owners_count":23060011,"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-05-27T05:17:56.475Z","updated_at":"2025-06-19T14:32:11.697Z","avatar_url":"https://github.com/neplextech.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![\"no runtime magic\"](https://img.shields.io/badge/runtime-magic--free-success?style=flat-square)\n![\"compiles like a boss\"](https://img.shields.io/badge/compiles-like%20a%20boss-blueviolet?style=flat-square)\n![\"pragmas are code too\"](https://img.shields.io/badge/%22use%20once%22-actually%20means%20something-yellow?style=flat-square)\n\n# directive-to-hof\n\nTransform directives into a higher order function.\n\n# Installation\n\n```sh\n$ npm i directive-to-hof\n```\n\n# Features\n\n- **No runtime magic**: The directive is transformed into a higher order function call.\n- **Compiles like a boss**: The directive is transformed into a higher order function call at build time.\n- **Pragmas are code too**: Treat directives as code. This means you can use them in any context where you would use a function call.\n\n# Usage\n\n## Vite/Rollup Plugin\n\n```ts\nimport { vite as viteDirective } from 'directive-to-hof';\n\nexport default defineConfig({\n  plugins: [\n    viteDirective({\n      directive: 'use once', // the directive to look for\n      importPath: './use-once-function.js', // lib name/path to import the higher order function from\n      importName: 'useOnce', // the higher order function name to import\n      asyncOnly: false, // only allow async functions\n    }),\n  ],\n});\n```\n\n\u003e import `rollup` for rollup\n\n## esbuild\n\n```ts\nimport { esbuild } from 'directive-to-hof';\n\n// plugins array\nplugins: [\n  esbuild({\n    directive: 'use once', // the directive to look for\n    importPath: './use-once-function.js', // lib name/path to import the higher order function from\n    importName: 'useOnce', // the higher order function name to import\n    asyncOnly: false, // only allow async functions\n  }),\n];\n```\n\n## API\n\n```js\n// use-once-function.js\nexport function useOnce(fn) {\n  let result;\n\n  return (...args) =\u003e {\n    if (result !== undefined) return result;\n    return (result = fn(...args));\n  };\n}\n```\n\n```ts\nimport { createDirectiveTransformer } from 'directive-to-hof';\n\nconst transformer = createDirectiveTransformer({\n  directive: 'use once', // the directive to look for\n  importPath: './use-once-function.js', // lib name/path to import the higher order function from\n  importName: 'useOnce', // the higher order function name to import\n  asyncOnly: false, // only allow async functions\n});\n\nconst code = `\nlet count = 0;\nfunction increment() {\n  'use once';\n  return ++count;\n}\n`;\n\nconst { contents } = await transformer(code, { path: import.meta.filename });\n\nconsole.log(contents);\n/*\nimport { useOnce } from \"./use-once-function.js\"\nlet count = 0;\n\nconst increment = useOnce(() =\u003e {\n  return ++count;\n});\n*/\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneplextech%2Fdirective-to-hof","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fneplextech%2Fdirective-to-hof","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneplextech%2Fdirective-to-hof/lists"}