npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tokgen

v1.0.0

Published

Generate cryptographically secure token strings

Downloads

804

Readme

tokgen

npm Version npm Downloads Test Status Test Coverage MIT Licensed

Generate cryptographically secure token strings.

const TokenGenerator = require('tokgen');

let generator = new TokenGenerator();

let token = generator.generate();
// => 'Q1GM0wL95xUkE2JBXzBiV75MrGTw6GAk'

token = generator.generate();
// => 'RPTXBktJl1lgHMTVQOzKVpFlR3hfOIfI'
const TokenGenerator = require('tokgen');

let generator = new TokenGenerator({chars: '0-9a-f', length: 8});

let token = generator.generate();
// => '5f8ab69e'

token = generator.generate();
// => 'e5e3c24a'

Installation

npm install tokgen

API

const TokenGenerator = require('tokgen');

new TokenGenerator(options)

Create a new token generator with the specified options.

options

object, string, number (default = {})

TokenGenerator's constructor accepts these properties in the options object:

Note: If options is a string it's treated as chars. Note: If options is a number it's treated as length.

chars

string (default = '0-9a-zA-Z')

The character range used for tokens returned by this generator.

new TokenGenerator(); // default

new TokenGenerator({chars: 'a-z'});

new TokenGenerator('a-z'); // shortcut
length

number (default = 32)

The default length (in characters) of tokens returned by this generator.

new TokenGenerator(); // default

new TokenGenerator({length: 16});

new TokenGenerator(16); // shortcut

TokenGenerator#generate(length, callback)

Generates a new token string. The call is asynchronous if callback is specified.

length

number (default = undefined)

The length of the token to be generated. If length is not specified the default length as given to the constructor is used.

let generator = new TokenGenerator();

// default length
let token = generator.generate();
// => 'mY9LJeyGt5p6TcJ7kEOm4M0N7mdoIbGh'

// explicit length
token = generator.generate(8);
// => 'sySbqK9N'

callback

function (default = undefined)

Generate the token asynchronously and then pass it to the callback.

The callback is expected to be a (for Node.js typical) error-first callback.

let generator = new TokenGenerator();

// default length
generator.generate((error, token) => {
  if (error) {
    return console.error(error);
  }
  console.log(token);
  // => 'mY9LJeyGt5p6TcJ7kEOm4M0N7mdoIbGh'
});

// explicit length
generator.generate(8, (error, token) => {
  if (error) {
    return console.error(error);
  }
  console.log(token);
  // => 'sySbqK9N'
});

Tests

To run the test suite, install dependencies, then run npm test:

npm install
npm test

Coverage reports are generated by running npm run coverage.

Linting is done with npm run lint.