Code Monkey home page Code Monkey logo

random-string's Introduction

About The Package

There are lots of scenarios which one can use our Random string generator the most known or the best scenario is for

1.Password generation

2.Token generation.

3.Testing

4.id generation

This package generate random string based on the given parameters.

Getting Started

To start to use this on local machine build then first Test the program using the test.js

Install node modules

  • npm
npm install randomized-string
  • yarn
yarn add randomized-string

Usage

For generating a random string you call generate() function.By default it generate 16 character string.

const randomString = require('randomized-string');
randomString.generate(); // xabgtl3yb1Ac0MrQ

or you can use import statment

import randomString from 'randomized-string';

If you want some length to random string pass a number to the function.

randomString.generate(6); // lAo3Bi

To be more specific you can pass an object for more flexibility.This are the listed options you can provide

type options = {
  charset?: 'alphanumeric' | 'number' | 'alphabet' | 'hex' | 'binary' | 'octal';
  lowerCaseOnly?: boolean; //only lowercase letter
  range?: string; // user provided charset
  length: number; //length of the character if not provided generates 16 characters by default
  upperCaseOnly?: boolean; // only uppercase letter
  insertSymbol?: boolean; // insert symbols to given charset
  symbolsOnly: boolean; //only symbols
  prefix: string; // add before string
  suffix: string; // add after the string
};

passing down the options

Generate string only in the given string range

randomeString.generate({
  range: 'abc123',
  length: 6,
});

// a3cb21

You can set also set type of string

randomString.generate({
  charset: 'number',
  length: 10,
}); //2342612198

randomString.generate({
  charset: 'alphabet',
  length: 10,
}); //SoWhopDFTb

randomString.generate({
  charset: 'hex',
  length: 6,
}); // C0faDB

You can insert prefix prefix or suffix characters. Note that the length of the prefix or suffix is not counted with the length of the random string.

randomString.generate({
  length: 10,
  prefix: 'pre-',
}); //pre-GaKdvH8Bro
randomString.generate({
  length: 10,
  suffix: '-suff',
}); //0YcCeMISpE-suff

You can also insert symbols if you pass set the insertSymbol option to true. But it doesn't work for binary or octal or hex charset option.

randomString.generate({
  insertSymbol: true,
});
//bd@MK8ˆIvpGVoorO{FJkf]iMz,{1+-8g

You can pass the option upperCaseOnly to make the string only capital and lowerCaseOnly to make the string lowercase

randomString.generate({
  charset: 'alphabet',
  upperCaseOnly: true,
});
//ODEISDGQXUGGOHHG
randomString.generate({
  lowerCaseOnly: true,
});
//fnzkamf0svos4yso

You can get symbols only if you set option symbolsOnly:true,

randomString.generate({
  symbolsOnly: true,
  length: 10,
});
// ?[]}$'&,{]

For fun you can also generate random emojis using generateUnicodeEmoji(length)

randomString.generateUnicodeEmoji(1); //🍍

Command Line Usage

First the package should be installed globally

$ npm install randomized-string -g

$ yarn add randomized-string

$ randomized-string

 // Urp0YDaIHWn7YCCF

$ randomized-string  upperCaseOnly=true charset=alphanumeric

 // DX5ACJP1FJN5Q79Z

$ randomized-string   charset=alphanumeric insertSymbol=true length=8
// S8Cza8v^

$ randomized-string  prefix=pre-
 //pre-KOyWstwcpA6sLaH3

$ randomized-string  generateUnicodeEmoji length=5

 // ⏺️💤👇😰🗳️

API

randomString

generate(option|length)

  • option:

  • length: define the length of the output default 16 [Optional]

  • insert Symbol: to use Symbols when generatig random string output [Optional]

  • charset:

    • Alphabetic [a-zA-Z]

    • alphanumeric[0-9a-zAZ]

    • numeric [0-9]

    • Binary [01]

    • Octal [0-7]

    • Hexa [0-9a-fA-F]

  • capitalization:

    • upperCaseOnly : Only Capital Leters are used to generate the output [Optional]

    • lowerCaseOnly : Only Small Leters are used to generate the output [Optional]

  • symbols only:

    • used to generate symbols only
  • prefix:

    • Adds a character before the random string
  • suffix:

    • Adds character after the random string

generateUnicodeEmoji(length)

  • generates random emojis

Tests

  • npm install
  • npm run test

See the open issues for a full list of proposed features (and known issues).

(back to top)

support us

☕ Buy me a coffee

  • You can also give as star to our repo.

Authors

Author 1

👤 Dagmawi Zewdu

Author 2

👤 Sentayhu Berhanu

Contribution

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

Built With

Contact

Dagmawi Zewdu - @Dagi - [email protected]

Sentayhu Berhanu- @Sentayhu - [email protected]

Project Link: https://github.com/Dagic-zewdu/random-string

random-string's People

Contributors

dagic-zewdu avatar sentayhu19 avatar wuletawwonte avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

random-string's Issues

Alphanumeric and alphabet with Symbol

alpabet insertSymbol and alphanumeric insertSymbol are not generating the output with the exact given length

for example

randomized-string   charset=alpabet insertSymbol=true length=4
//  oRNcE]Ml
 
randomized-string   charset=alphanumeric insertSymbol=true length=2
// cb"O

Unicode emoji

It would be good to add Unicode emoji Generator as a feature

Insert Symbol with number

Insert Symbol with number

  • The function that should generate Symbol with number is only generating Numbers only

Generate Random Strings Matching Predefined Pattern or Format for Enhanced Flexibility and Control

Is your feature request related to a problem? Please describe.
It is not possible to get a random string based on pattern or format provided. For example provide "A0" as a pattern where A represents an uppercase letter and 0 represents a digit, and get something like "H7"

Describe the solution you'd like
It would be nice if the package provides a method like generateWithPattern(pattern) that takes a pattern as input and generates a random string based on that pattern.

Describe alternatives you've considered
One possible approach could be adding pattern as an argument in the options object.

Security issue

As the Math.random() function relies on a weak pseudorandom number generator, this function should not be used for security-critical applications or for protecting sensitive data.
Math.random function must not be used to generate the random string it is not Cryptographically Safe instead:

Crypto.getRandomValues() 

OR

Crypto.generateKey() 

to be used to generate randome values

Command line cli

Add command line cli for better interaction. You can add commands directly from the terminal

Add prefix and suffix option to the random string

You can also add prefix and suffix option like

randomString.generate({
 charset:"alphabet",
  prefix:"pre-",
  length:10
}) // pre-fa423osDp

//suffix option
randomString.generate({
 charset:"alphabet",
  suffix:"suff-",
  length:10
}) // fa423osDp-suff

//both
randomString.generate({
 charset:"alphabet",
   prefix:"pre-"
  suffix:"suff-",
  length:10
}) //pre-fa423osDp-suff

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.