{"id":21839214,"url":"https://github.com/alibasiccoder/gen-js-block","last_synced_at":"2026-02-04T22:33:12.182Z","repository":{"id":246573550,"uuid":"821504142","full_name":"AliBasicCoder/gen-js-block","owner":"AliBasicCoder","description":"a js library to generate js-code using js code","archived":false,"fork":false,"pushed_at":"2024-08-21T14:23:12.000Z","size":84,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-25T11:50:18.486Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/AliBasicCoder.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-06-28T17:33:59.000Z","updated_at":"2024-08-21T14:23:16.000Z","dependencies_parsed_at":"2024-06-29T16:25:19.944Z","dependency_job_id":"fbf62356-6f71-4360-b368-53f3e80de0f2","html_url":"https://github.com/AliBasicCoder/gen-js-block","commit_stats":null,"previous_names":["alibasiccoder/js-gen","alibasiccoder/gen-js-block"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/AliBasicCoder/gen-js-block","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AliBasicCoder%2Fgen-js-block","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AliBasicCoder%2Fgen-js-block/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AliBasicCoder%2Fgen-js-block/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AliBasicCoder%2Fgen-js-block/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AliBasicCoder","download_url":"https://codeload.github.com/AliBasicCoder/gen-js-block/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AliBasicCoder%2Fgen-js-block/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29098234,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-04T21:05:08.033Z","status":"ssl_error","status_checked_at":"2026-02-04T21:04:53.031Z","response_time":62,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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-27T21:16:40.548Z","updated_at":"2026-02-04T22:33:12.155Z","avatar_url":"https://github.com/AliBasicCoder.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gen-js-block\n\n[![npm](https://img.shields.io/npm/dm/gen-js-block)](https://npmjs.com/package/gen-js-block)\n[![npm](https://img.shields.io/npm/v/gen-js-block)](https://npmjs.com/package/gen-js-block)\n\n[![GitHub issues](https://img.shields.io/github/issues/AliBasicCoder/gen-js-block)](https://github.com/AliBasicCoder/gen-js-block/issues)\n[![GitHub forks](https://img.shields.io/github/forks/AliBasicCoder/gen-js-block)](https://github.com/AliBasicCoder/gen-js-block/network)\n[![GitHub stars](https://img.shields.io/github/stars/AliBasicCoder/gen-js-block)](https://github.com/AliBasicCoder/gen-js-block/stargazers)\n[![GitHub license](https://img.shields.io/github/license/AliBasicCoder/gen-js-block)](https://github.com/AliBasicCoder/gen-js-block/blob/master/LICENSE)\n\na js-library to generate js code with js code\n\n\u003e if you like this package please star it in [github](https://github.com/AliBasicCoder/gen-js-block)\n\n## Usage\n\nall variables that start with $ are called template variables\n\nif you use a template variable in an if statement it becomes a template if-statement\n\nand the code inside will not be in the output\nif the condition is false\n\n```js\nimport { Block } from \"gen-js-block\";\n\nconst block = new Block(($condition) =\u003e {\n  if ($condition) {\n    console.log(\"$condition is true\");\n  } else {\n    console.log(\"$condition is false\");\n  }\n});\n\nconsole.log(block.build({ $condition: true })); // =\u003e {console.log(\"$condition is true\");}\nconsole.log(block.build({ $condition: false })); // =\u003e {console.log(\"$condition is false\");}\n```\n\nsimilarly for-of and for-in loops with template variables in them are called template loops (c-style loops can't be template maybe in the future)\n\n```js\nimport { Block } from \"gen-js-block\";\n\nconst block = new Block(($array) =\u003e {\n  for (const $item of $array) {\n    console.log($item);\n  }\n});\n\nconsole.log(block.build({ $array: [0, 1, 2] }));\n// =\u003e {const $item = 0;console.log($item);};{const $item = 1;console.log($item);};{const $item = 2;console.log($item)};\n```\n\nand also the value of template variables will be included in the code,\nso you can use them in the result code\n\n```js\nimport { Block } from \"gen-js-block\";\n\nconst block = new Block(($message) =\u003e {\n  console.log($message);\n});\n\nconsole.log(block.build({ $message: \"hello world!\" }));\n// =\u003e {const $message = \"hello world!\";console.log($message);}\n```\n\nalso if you call a function that starts with $ and it's an argument of the function passed to\nthe block class then the string returned from that call will be added to the result code as is\n\nexample: let's say you want to make code that returns a function that returns a random number\n\n```js\nimport { Block } from \"gen-js-block\";\n\nconst block = new Block(($fn) =\u003e {\n  function result() {\n    return $fn();\n  }\n  result;\n});\n\nconst code = block.build({ $fn: Math.random });\n// =\u003e {const $fn = undefined;function result() { return 0.5542537758332537 }; result;}\nconsole.log(eval(code)());\n// =\u003e 0.5542537758332537\nconsole.log(eval(code)());\n// =\u003e 0.5542537758332537\n```\n\nexample 2: let's say you want to print numbers from 3 to 0 but using recursion instead of loops...\n\n```js\nfunction recur(n: number) {\n  const block = new Block(($fn: any, $n: any) =\u003e {\n    if ($n \u003c 1) {\n      console.log(0);\n    } else {\n      console.log($n);\n      // these brackets are needed in these case\n      // and most cases\n      {\n        $fn($n - 1);\n      }\n    }\n  });\n\n  return block.build({ $n: n, $fn: recur });\n}\nconst code = recur(3);\nconsole.log(code);\n// =\u003e const $fn = undefined;const $n = 3;{console.log($n);{const $fn = undefined;const $n = 2;{console.log($n);{const $fn = undefined;const $n = 1;{console.log($n);{const $fn = undefined;const $n = 0;{console.log(0);};}};}};}}\neval(code);\n// =\u003e 3\n// =\u003e 2\n// =\u003e 1\n// =\u003e 0\n```\n\n## inline options\n\nyou might have noticed that instead of replacing template variables in the result code\ngen-js-block defines them on top, if you want it to replace it instead use this\n\n```js\nimport { Block } from \"gen-js-block\";\n\nconst block = new Block(\n  ($message) =\u003e {\n    console.log($message);\n  },\n  { inlineVariables: true }\n);\n\nconsole.log(block.build({ $message: \"hello world!\" }));\n// =\u003e console.log(\"hello world\");\n```\n\nif you want to inline specific variables to this\n\n```js\nimport { Block } from \"gen-js-block\";\n\nconst block = new Block(\n  ($message, $message2) =\u003e {\n    console.log($message, $message2);\n  },\n  { inlineVariables: [\"$message\"] }\n);\n\nconsole.log(\n  block.build({ $message: \"hello world!\", $message2: \"hello, again\" })\n);\n// =\u003e const $message2 = \"hello again\";console.log(\"hello world\", $message2);\n```\n\n## replace option\n\nthis will not consider if statements and for-loops that contain specified template vars as template if-statements \nand for loops instead it will replace it with given code\n\n```js\nimport { Block, insertCode } from \"gen-js-block\";\n\nconst block = new Block(($some) =\u003e {\n  if ($some === \"one\") $some = 1;\n  else if ($some === \"two\") $some = 2;\n  else $some = null;\n}, { replace: [\"$some\"] });\n\n// you must use insertCode function\nconsole.log(block.build({ $some: insertCode(\"hello.world\") }));\n// ==\u003e if (hello.world === \"one\") { hello.world = 1 } else if (hello.world === 2) { hello.world = 1 } else { hello.world = null; }\n```\n\n## eval method\n\nif you want to build the code then run it you can use the eval method\n\n```js\nimport { Block } from \"gen-js-block\";\n\nconst block = new Block(($message) =\u003e {\n  console.log($message);\n});\n\nblock.eval({ $message: \"hello world!\" });\n// prints \"hello world!\"\n```\n\n## usage in typescript\n\nall template variables must be typed like so\n\n```ts\nimport { Block } from \"gen-js-block\";\n\nconst block = new Block\u003c{ $message: string; $array: number[] }\u003e(\n  ($message: string, $array: number[]) =\u003e {\n    console.log($message, $array);\n  }\n);\n```\n\nand if there is a non-template variables that will be defined else where you pass it as an argument\n\n```ts\nconst block = new Block\u003c{}\u003e((definedElseWhere: string) =\u003e {\n  // no errors!\n  console.log(definedElseWhere);\n});\n```\n\n## important notes\n\n1. all template variables must be start with $\n1. new variables defined in template loop must start with $ (since they're template vars). example:\n\n   ```js\n   // will throw an error\n   const block = new Block(($array) =\u003e {\n     // should be $item...\n     for (const item of $array) {\n       console.log(item);\n     }\n   });\n\n   block.build({ $array: [0, 1, 2] });\n   ```\n\n1. result code inside template loops or ifs will be warped in a block. example:\n\n   ```js\n   // this wont error\n   const block = new Block(($condition) =\u003e {\n     const some = 0;\n     if ($condition) {\n       const some = 1;\n       console.log(some);\n     }\n     console.log(some);\n   });\n\n   block.build({ $condition: true });\n   // =\u003e {const some = 0;{const some = 1;console.log(some);}console.log(some);}\n   ```\n\n1. c-style loops can't be template loops (at least for now), example:\n\n   ```js\n   // won't work (will be in result code)\n   const block = new Block(($array) =\u003e {\n     for (let $i = 0; $i \u003c $array.length; $i++) {\n       console.log($array[$i]);\n     }\n   });\n\n   block.build({ $array: [0, 1, 2] });\n   // =\u003e {for (let $i = 0; $i \u003c $array.length; $i++) {console.log($array[$i]);}}\n   // not the output you want!\n   ```\n\n1. no template switch statements (at least for now)\n   ```js\n   // won't work!\n   const block = new Block(($number) =\u003e {\n     switch ($number) {\n       case 0:\n         console.log(\"zero\");\n       case 1:\n         console.log(\"one\");\n     }\n   });\n   ```\n1. can't non-identifier parameters to the function passed to block (since it doesn't make sense)\n\n   ```js\n   // all of these will error\n\n   const block = new Block((...$args) =\u003e /* ... */);\n\n   const block = new Block(([$item1, $item2]) =\u003e /* ... */);\n\n   const block = new Block(({ $key }) =\u003e /* ... */);\n   ```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falibasiccoder%2Fgen-js-block","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falibasiccoder%2Fgen-js-block","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falibasiccoder%2Fgen-js-block/lists"}