{"id":18576085,"url":"https://github.com/thiagodp/better-randstr","last_synced_at":"2025-05-16T00:34:43.435Z","repository":{"id":45183509,"uuid":"139978442","full_name":"thiagodp/better-randstr","owner":"thiagodp","description":"🌞 Fully customizable random string generator. Test your applications better!","archived":false,"fork":false,"pushed_at":"2022-01-03T01:05:47.000Z","size":283,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-21T08:04:32.644Z","etag":null,"topics":["better","custom","customizable","generate","generation","length","rand","rand-str","random","randstr","str","string","unicode","utf"],"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/thiagodp.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":"2018-07-06T11:51:50.000Z","updated_at":"2022-01-03T00:17:25.000Z","dependencies_parsed_at":"2022-07-26T16:32:01.647Z","dependency_job_id":null,"html_url":"https://github.com/thiagodp/better-randstr","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fbetter-randstr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fbetter-randstr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fbetter-randstr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fbetter-randstr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thiagodp","download_url":"https://codeload.github.com/thiagodp/better-randstr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254447870,"owners_count":22072754,"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":["better","custom","customizable","generate","generation","length","rand","rand-str","random","randstr","str","string","unicode","utf"],"created_at":"2024-11-06T23:23:33.071Z","updated_at":"2025-05-16T00:34:43.403Z","avatar_url":"https://github.com/thiagodp.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# better-randstr\n\n[![Build Status](https://travis-ci.com/thiagodp/better-randstr.svg?branch=master)](https://travis-ci.com/thiagodp/better-randstr)\n[![npm version](https://badge.fury.io/js/better-randstr.svg)](https://badge.fury.io/js/better-randstr)\n\n\u003e 🌞 Fully-customizable random string generator. Useful for testing applications, etc.\n\n- Works with browsers, [NodeJS](https://nodejs.org/) and [DenoJS](https://deno.land/) (JavaScript 6+ and TypeScript)\n- No external dependencies\n- Unit-tested\n- Semantic Versioning\n- It can be used with your preferred pseudo-random generator (PRNG).\n\n## Install\n\n```bash\nnpm i better-randstr\n```\n\n## Declaration\n\n### Browser\n\nGlobal:\n```html\n\u003cscript crossorigin src=\"https://unpkg.com/better-randstr\" \u003e\n\u003cscript\u003econsole.log( randstr() );\u003c/script\u003e\n```\n\nESM:\n```html\n\u003cscript type=\"module\" \u003e\nimport { randstr } from 'https://unpkg.com/better-randstr/index.esm.js';\nconsole.log( randstr() );\n\u003c/script\u003e\n```\n\n### NodeJS\n\n```javascript\nconst { randstr } = require('better-randstr');\nconsole.log( randstr() );\n```\n\n### Deno\n\n```typescript\nimport { randstr } from 'https://unpkg.com/better-randstr/index.esm.js';\nconsole.log( randstr() );\n```\n\n## Usage Examples\n\n```javascript\n// Default:\n// - Up to 100 characters\n// - No control characters\n// - ASCII + ISO (UTF-8)\nrandstr();\n// =\u003e ;Þë?Ç¡m{îU4I0_%L*qV\n\n// Using a customized function as random function, instead of Math.random()\nconst myRandomFunc = /* a function that returns a number between 0 and 1 */ ;\nrandstr( { random: myRandomFunc } );\n// =\u003e cy¦¼Óé6Lcy\n\n// Exactly 10 characters\nrandstr( { length: 10 } );\n// =\u003e »²y+iÀëC#Ù\n\n// At least 10 characters\nrandstr( { length: [ 10 ] } );\n// =\u003e ü¢ ß~å¿Û¿\\ÓÜtÈ[\"ª9¡.:`i¡{ã«?®Q=\u003e?v\u0026ëÿ2\"Âë\n\n// At most 10 characters\nrandstr( { length: [ 0, 10 ] } );\n// =\u003e È¹t×úÓ¯ÖÃj\n\n// Between 2 and 60 characters\nrandstr( { length: [ 2, 60 ] } );\n// =\u003e ZÔý­ÛÏaæ»û_Eâo9¨§­:Çu!ÕÄø|FNß¨j)1n¦Í:\n\n// Exactly 10 characters, with only the specified characters\nrandstr( { length: 10, chars: 'ABC123' } );\n// =\u003e A31AAB3AB1\n\n// Exactly 10 characters, only characters in the range, no control characters\nrandstr( { length: 10, chars: [ 32, 127 ] } ); // ASCII range\n// =\u003e EA*bY7{*cL\n\n// Exactly 10 characters, between 'A' and 'Z'\nrandstr( { length: 10, chars: [ 'A'.charCodeAt( 0 ), 'Z'.charCodeAt( 0 ) ] } );\n// =\u003e SPQJNORXXR\n\n// Exactly 10 characters, only numbers\nconst NUMBERS = require( 'better-randstr' ).NUMBERS;\nrandstr( { length: 10, chars: NUMBERS } ) // same as passing '0123456789'\n// =\u003e 7450625283\n\n// Exactly 10 characters, alfanumeric\nconst ALPHA_NUMERIC = require( 'better-randstr' ).ALPHA_NUMERIC;\nrandstr( { length: 10, chars: ALPHA_NUMERIC } );\n// =\u003e s1wMa7QVmg\n\n// Exactly 10 characters, all characters in ASCII/ISO (UTF-8) except the specified\nrandstr( { length: 10, acceptable: function( chr ) {\n    return '%#\u0026$'.indexOf( chr ) \u003c 0; // acceptable if not found\n} } );\n// =\u003e ´NvÄ~]¿Gº]\n\n// Exactly 10 characters, escaping quotes and single-quotes\nrandstr( { length: 10, replacer: function( chr ) {\n    switch ( chr ) {\n        case '\"': return '\\\\\"';\n        case \"'\": return \"\\\\'\";\n    }\n    return chr;\n} } );\n// =\u003e ñ;}éÔÝf«\\'\n\n// Include control characters\nrandstr( { includeControlChars: true } );\n// =\u003e x@HA$÷°GÝ³:^Ê%¼¤®ý±#Sh+Ò+Å|\n\n// Wider range than UTF-8 - control characters not avoided!\nrandstr( { chars: [ 32, 1000 ] } );\n// =\u003e r΅4ƹǭ̻ɍĿΠsɊQȍάĠĲȤċƳȭɧĄƹĜʋ͏Ʒȥĭ˟͢\"Ȓǭ̼Ζ˂̀ƖǛ̚3\u0026΃ƏϧȷɥŃ\n```\n\nSee [example.js](examples/example.js)\n\n## API\n\n```typescript\nfunction randstr( options: Options ): string;\n```\n\nwhere `Options` is :\n\n```typescript\n{\n\n    /**\n     * Random function to be used. By default, it assumes `Math.random()`.\n     *\n     * The function does not expect arguments and must return a number between 0 and 1.\n     */\n    random?: function () =\u003e number;\n\n    /**\n     * Length or length range. By default, it assumes `[ 0, 100 ]`.\n     *\n     * - Number: Minimum and maximum length will be equal to the provided number. Example: `{ length: 20 }`.\n     * - One-length array: Maximum will be a random number greater than or equal to the minimum. Example: `{ length: [ 2 ] }`.\n     * - Two-length array: Minimum and maximum length will assume the provided numbers. Example: `{ length: [ 0, 50 ] }`.\n     */\n    length?: number | number[];\n\n    /**\n     * Allowed characters or byte range. By default, it assumes `[32, 255]`\n     * which is a range containing the first printable ASCII character (`32`)\n     * and the last ISO character (`255`).\n     *\n     * For example, `\"0123456789\"`, which gives the same result as\n     * `['0'.charCodeAt(0), '9'.charCodeAt(0)]`.\n     */\n    chars?: string | number[];\n\n    /**\n     * Function that evaluates whether the generated character is acceptable.\n     * By default, it is `undefined`.\n     * The function expects a character and returns a boolean.\n     */\n    acceptable?: function ( string ) =\u003e boolean;\n\n    /**\n     * Function that replaces a character by other character or characters.\n     * By default, it is `undefined`.\n     * You may use it for escaping or replacing certain characters, for example.\n     */\n    replacer?: function ( string ) =\u003e string;\n\n    /**\n     * Whether is desired to include control characters. By default, it is `false`.\n     */\n    includeControlChars?: boolean;\n}\n```\n\n## See also\n\n- [one-wise](https://github.com/thiagodp/one-wise) - One-wise combinatorial testing generator\n\n- [seedrandom](https://github.com/davidbau/seedrandom) - Predictive PRNG\n\n- [spec-pattern](https://github.com/thiagodp/spec-pattern) - Build complex validation rules or filters easily.\n\n\n## License\n\n[MIT](LICENSE) © [Thiago Delgado Pinto](https://github.com/thiagodp)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthiagodp%2Fbetter-randstr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthiagodp%2Fbetter-randstr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthiagodp%2Fbetter-randstr/lists"}