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

shortid-extend

v0.0.3

Published

Alternative versión with options from Dylan Greene ShortId

Downloads

202

Readme

shortid-extend (https://www.npmjs.org/package/shortid-extend)

Amazingly short non-sequential url-friendly unique id generator.

This is a fork of the original ShortId module (https://github.com/dylang/shortid) from Dylan Greene(https://github.com/dylang) for adapt the module to my work needs.

ShortId-extend creates amazingly short non-sequential url-friendly unique ids. Perfect for url shorteners, MongoDB and Redis ids, and any other id users might see.

** NEW FUNCTIONALITY ** The module have now a config method for override the default settings for the alphabet and the default generated ID Length. IF you configure the module for have this config object overrides the default configuration and have the capability of sets the desired ID lenght also your desired alphabet lenght.

  • By default 7-14 url-friendly characters: A-Z, a-z, 0-9, _-
  • NEW Load and use characters of any length.
  • NEW Set the default generated ID Length.
  • Supports cluster (automatically), custom seeds, custom alphabet.
  • Can generate any number of ids without duplicates, even millions per day.
  • Perfect for games, especially if you are concerned about cheating so you don't want an easily guessable id.
  • Apps can be restarted any number of times without any chance of repeating an id.
  • Popular replacement for Mongo ID/Mongoose ID.
  • Works in Node, io.js, and web browsers.
  • Includes Mocha tests.

ShortId-extend does not generate cryptographically secure ids, so don't rely on it to make IDs which are impossible to guess.

Usage

const shortid = require('shortid-extend');

console.log(shortid.generate());
// PPBqWA9

Mongoose Unique Id

_id: {
  'type': String,
  'default': shortid.generate
},

Browser Compatibility

The best way to use shortid in the browser is via browserify or webpack.

These tools will automatically only include the files necessary for browser compatibility.

All tests will run in the browser as well:

## build the bundle, then open Mocha in a browser to see the tests run.
$ grunt build open

Example

~/projects/shortid ❯ node examples/examples.js
eWRhpRV
23TplPdS
46Juzcyx
dBvJIh-H
2WEKaVNO
7oet_d9Z
dogPzIz8
nYrnfYEv
a4vhAoFG
hwX6aOr7

API

var shortid = require('shortid');

shortid.config(object)

Params boolean disableDefaultAlphabetLength: Disables the default characters length setting.

Params boolean disableDefaultIdLength: Disables the default generated ID Length.

Params number idLength: REQUIRED IF disableDefaultIdLength sets to true Desired ID Length.

Example

shortid.config({
  disableDefaultAlphabetLength: true,
  disableDefaultIdLength: true,
  idLength: 6
})

shortid.generate()

Returns string non-sequential unique id.

Example

users.insert({
  _id: shortid.generate(),
  name: '...',
  email: '...'
});

shortid.characters(string)

Default: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'

Returns new alphabet as a string

Recommendation: If you don't like _ or -, you can to set new characters to use.

Optional

Change the characters used.

You must provide a string of all 64 unique characters. Order is not important. (You can overwrite this setting with config object)

If you sets

The default characters provided were selected because they are url safe.

Example

// use $ and @ instead of - and _
shortid.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@');
// any 64 unicode characters work, but I wouldn't recommend this.
shortid.characters('ⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ①②③④⑤⑥⑦⑧⑨⑩⑪⑫');

shortid.isValid(id)

Returns boolean

Check to see if an id is a valid shortid. Note: This only means the id could have been generated by shortid, it doesn't guarantee it.

Example

shortid.isValid('41XTDbE');
// true
shortid.isValid('i have spaces');
// false

shortid.worker(integer)

Default: process.env.NODE_UNIQUE_ID || 0

Recommendation: You typically won't want to change this.

Optional

If you are running multiple server processes then you should make sure every one has a unique worker id. Should be an integer between 0 and 16. If you do not do this there is very little chance of two servers generating the same id, but it is theoretically possible if both are generated in the exact same second and are generating the same number of ids that second and a half-dozen random numbers are all exactly the same.

Example

shortid.worker(1);

shortid.seed(integer)

Default: 1

Recommendation: You typically won't want to change this.

Optional

Choose a unique value that will seed the random number generator so users won't be able to figure out the pattern of the unique ids. Call it just once in your application before using shortId and always use the same value in your application.

Most developers won't need to use this, it's mainly for testing ShortId.

If you are worried about users somehow decrypting the id then use it as a secret value for increased encryption.

Example

shortid.seed(1000);

About the Original Author of the module.

Visit your Github page: https://github.com/dylang

License

Copyright (c) 2016 Dylan Greene, contributors.

Released under the MIT license.

Screenshots are CC BY-SA (Attribution-ShareAlike).