{"id":18606187,"url":"https://github.com/bmacnaughton/string-generator","last_synced_at":"2026-05-01T06:34:07.871Z","repository":{"id":41495607,"uuid":"345383335","full_name":"bmacnaughton/string-generator","owner":"bmacnaughton","description":"A very simple random string generator.","archived":false,"fork":false,"pushed_at":"2022-08-16T11:57:55.000Z","size":232,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-04-03T10:02:33.954Z","etag":null,"topics":["generator","javascript","mock","mocking","mocks","random-generation","random-string","simple","strings","typescript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bmacnaughton.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}},"created_at":"2021-03-07T15:35:20.000Z","updated_at":"2023-04-05T11:53:49.000Z","dependencies_parsed_at":"2022-09-05T04:20:18.936Z","dependency_job_id":null,"html_url":"https://github.com/bmacnaughton/string-generator","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/bmacnaughton/string-generator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bmacnaughton%2Fstring-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bmacnaughton%2Fstring-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bmacnaughton%2Fstring-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bmacnaughton%2Fstring-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bmacnaughton","download_url":"https://codeload.github.com/bmacnaughton/string-generator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bmacnaughton%2Fstring-generator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32487588,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-30T13:12:12.517Z","status":"online","status_checked_at":"2026-05-01T02:00:05.856Z","response_time":64,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["generator","javascript","mock","mocking","mocks","random-generation","random-string","simple","strings","typescript"],"created_at":"2024-11-07T02:24:43.461Z","updated_at":"2026-05-01T06:34:07.852Z","avatar_url":"https://github.com/bmacnaughton.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# string-generator\n\n![build status](https://github.com/bmacnaughton/string-generator/actions/workflows/on-new-code.yml/badge.svg?branch=master) [![codecov](https://codecov.io/gh/bmacnaughton/string-generator/branch/master/graph/badge.svg?token=MUWZJSTWPJ)](https://codecov.io/gh/bmacnaughton/string-generator)\n\nThis is a simple template-based string generator. I wanted to generate\nrandom strings that met specific criteria for tests.\n\nWhy not use an existing package that does everything, like [faker](https://github.com/Marak/Faker.js)?\nIf you need the complexity that comes with `faker` then by all means use\nit. But if you want to work with a very simple, template-driven API with\nno dependencies, then this might be helpful. It's simple, small, flexible,\nand moderately extensible.\n\nVersion 3 has [major breaking changes](#breaking-changes-from-version-2).\n\n## Installing\n\nYou know the routine.\n\n`$ npm install --save-dev @bmacnaughton/string-generator@3`\n\n## Usage\n\nHere is documentation by example usage.\n\n```js\nimport Generator from '@bmacnaughton/string-generator';\nconst g = new Generator();\n// get the tagFunction (bound to `g`) for template literals.\nconst gen = g.tagFunction();\n\ngen`${'[A-F0-9]'}`;             // one random hex character\ngen`${'=hex'}`;                 // one random lowercase hex character\ngen`${'=HEX\u003c10\u003e'}`;             // 10 random uppercase hex characters\ngen`${'=hex\u003c4\u003e}:${=hex\u003c6\u003e}:${=hex\u003c2\u003e'}`; // dead:beefca:fe (random)\ngen`${'[ab]\u003c10\u003e'}`;             // 'abbbaabbba' (random)\ngen`${'(this|that|else)\u003c2\u003e'}`;  // 'thiselse'\ngen`${'\"literal\"\u003c2\u003e'}`;           // 'literalliteral'\ngen`${'=hex\u003c2:8\u003e'}`;            // between 2 and 8 hex characters (inclusive)\ngen`${'=hex\u003c2|5|9\u003e'}`;          // 2, 5, or 9 hex characters\ngen`${`\\` + someFunc()}`;\n```\n\nIn all the above cases, the `g.decode()` function can be used on the\nstring value with the `${..}` construct, e.g., `g.decode('=hex\u003c2:8\u003e')`\nto convert the value directly. This can be useful if your code already\nuses a tag function.\n\n```js\nimport Generator from '@bmacnaughton/string-generator';\nconst g = new Generator();\n// get the decode function (bound to `g`) for decoding literals\nconst decode = g.decodeFunction();\n\ng.decode('[A-F0-9]');   // one random hex character\n// or\ndecode('[A-F0-9]');\n```\n\n## Options\n\nThe `Generator` constructor takes an options object.\n\n- `random` - replace `Math.random` with this function that must have the same signature.\n- `codeWords` - an object of `word: function()` pairs. code words are referenced using `=word`. if\na code word is the same as a built-in code word (`hex`, `HEX`, etc.) then the built-in word is\nreplaced. `function()` must return an indexable value, e.g., string or array.\n\n## Breaking changes from version 2\n\nVersion 3 uses ES modules. You must use an `import` statement or the `import()` function.\nThere is only a default export, the `Generator` class.\n\nVersion 3 takes string-generator in a new direction. Version 2 embedded string-template-like\npatterns in a string. Version 3 embeds string patterns within string-templates that are executed\nby a tag-function (or by calling a decode function directly).\n\n- v2: `gen('${=alpha\u003c20\u003e}')`\n- v3: ``gen`${'=alpha\u003c20\u003e'}` `` or `decode('=alpha\u003c20\u003e')`\n\nThere are a number of other less significant changes.\n- literal-specs use `\"` or `'`\n- it is possible to quote the first interpolated character with `\\` to avoid it\nbeing interpreted by `string-generator`.\n- repeat-specs ranges use `:` instead of `,`. Now `\u003c1:5\u003e` means the range 1 to 5.\n\n\n## Historical napkin scrawlings\n\nI wanted a simple string generator. These are my original working notes.\n\n```js\n/**\n * format:\n * '${pattern}${pattern}literal'\n *\n * pattern:\n * substitution-spec\u003ccount-spec\u003e   // count-spec is optional\n *\n * substitution-spec:\n * [range-spec]\n * =code-word\n * (choice-spec)\n * literal\n *\n * count-spec:\n * count-range | count-oneof\n *\n * count-range:\n * min, max=min  // default when not present \u003c1, 1\u003e\n *\n * count-oneof:\n * n(|m)*\n *\n * range-spec\n * a-zA-Z0       // if - is desired must be first character\n *\n * code-word:\n * base58\n * alpha 'A-Za-z'\n * numeric '0-9'\n * alphanumeric 'A-Za-z0-9'\n * hex 'a-f0-9'\n * HEX 'A-F0-9'\n *\n * choice-spec:\n * this|that...\n *\n * literal-spec:\n * literal characters    // mostly useful for repeating\n *\n * characters not in a ${pattern} group are literal.\n */\n```\n\n## todos\n\n- ~~test suite~~\n- ~~allow \u003cn|m|o\u003e syntax on count spec to choose one of the given lengths~~\n- ~~convert to class~~\n- ~~supply random number generator~~\n- ~~options to supply own code-words~~\n- ~~make basics.test iterate using optional random function.~~\n- allow code-word functions to have arguments (wip)\n- add base64 (convert given string to base64) like what syntax? @b64(arg)?\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbmacnaughton%2Fstring-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbmacnaughton%2Fstring-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbmacnaughton%2Fstring-generator/lists"}