better-randstr
🌞 Fully-customizable random string generator. Useful for testing applications, etc.
- Works with browsers, NodeJS and DenoJS (JavaScript 6+ and TypeScript)
- No external dependencies
- Unit-tested
- Semantic Versioning
- It can be used with your preferred pseudo-random generator (PRNG).
Install
npm i better-randstr
Declaration
Browser
Global:
<script crossorigin src="https://unpkg.com/better-randstr" >
<script>console.log( randstr() );</script>
ESM:
<script type="module" >
import { randstr } from 'https://unpkg.com/better-randstr/index.esm.js';
console.log( randstr() );
</script>
NodeJS
const { randstr } = require('better-randstr');
console.log( randstr() );
Deno
import { randstr } from 'https://unpkg.com/better-randstr/index.esm.js';
console.log( randstr() );
Usage Examples
randstr();
const myRandomFunc = ;
randstr( { random: myRandomFunc } );
randstr( { length: 10 } );
randstr( { length: [ 10 ] } );
randstr( { length: [ 0, 10 ] } );
randstr( { length: [ 2, 60 ] } );
randstr( { length: 10, chars: 'ABC123' } );
randstr( { length: 10, chars: [ 32, 127 ] } );
randstr( { length: 10, chars: [ 'A'.charCodeAt( 0 ), 'Z'.charCodeAt( 0 ) ] } );
const { NUMBERS } = require( 'better-randstr' );
randstr( { length: 10, chars: NUMBERS } )
const { ALPHA_NUMERIC } = require( 'better-randstr' );
randstr( { length: 10, chars: ALPHA_NUMERIC } );
randstr( { length: 10, acceptable: function( chr ) {
return '%#&$'.indexOf( chr ) < 0;
} } );
randstr( { length: 10, replacer: function( chr ) {
switch ( chr ) {
case '"': return '\\"';
case "'": return "\\'";
}
return chr;
} } );
randstr( { includeControlChars: true } );
randstr( { chars: [ 32, 1000 ] } );
See example.js
API
function randstr( options: Options ): string;
where Options
is :
{
random?: function () => number;
length?: number | number[];
chars?: string | number[];
acceptable?: function ( string ) => boolean;
replacer?: function ( string ) => string;
includeControlChars?: boolean;
}
See also
License
MIT © Thiago Delgado Pinto