UNPKG

8.81 kBMarkdownView Raw
1## shortid [![Build Status](http://img.shields.io/travis/dylang/shortid.svg?style=flat-square)](https://travis-ci.org/dylang/shortid) [![shortid](http://img.shields.io/npm/dm/shortid.svg?style=flat-square)](https://www.npmjs.org/package/shortid)
2
3> Amazingly short non-sequential url-friendly unique id generator.
4
5
6
7
8
9
10
11
12ShortId creates amazingly short non-sequential url-friendly unique ids. Perfect for url shorteners, MongoDB and Reddis ids, and any other id users might see.
13
14 * By default 7-14 url-friendly characters: `A-Z`, `a-z`, `0-9`, `_-`
15 * Non-sequential so they are not predictable.
16 * Supports `cluster` (automatically), custom seeds, custom alphabet.
17 * Can generate any number of ids without duplicates, even millions per day.
18 * Perfect for games, especially if you are concerned about cheating so you don't want an easily guessable id.
19 * Apps can be restarted any number of times without any chance of repeating an id.
20 * Popular replacement for Mongo ID/Mongoose ID.
21 * Includes [Mocha](http://visionmedia.github.com/mocha/) tests.
22
23
24
25### Usage
26
27```js
28var shortId = require('shortid');
29
30console.log(shortId.generate());
31
32//PPBqWA9
33```
34
35Mongoose Unique Id
36```js
37_id: {
38 type: String,
39 unique: true,
40 'default': shortId.generate
41},
42```
43
44
45
46### Example
47
48```js
49~/projects/shortid ❯ node examples/examples.js
50eWRhpRV
5123TplPdS
5246Juzcyx
53dBvJIh-H
542WEKaVNO
557oet_d9Z
56dogPzIz8
57nYrnfYEv
58a4vhAoFG
59hwX6aOr7
60```
61
62
63#### Real World Examples
64
65`shortId` was created for Node Knockout 2011 winner for Most Fun [Doodle Or Die](http://doodleordie.com).
66Millions of doodles have been saved with `shortId` filenames. Every log message gets a `shortId` to make it easy
67for us to look up later.
68
69Here are some other projects that use shortId:
70
71* [bevy](https://npmjs.org/package/bevy) - A simple server to manage multiple Node services.
72* [capre](https://npmjs.org/package/capre) - Cross-Server Data Replication.
73* [cordova-build](https://www.npmjs.org/package/cordova-build) - an alternative to phonegap build that runs on your servers/agents.
74* [couchdb-tools](https://www.npmjs.org/package/couchdb-tools) - A library of handy functions for use when working with CouchDB documents.
75* [CleverStack/clever-email](https://github.com/CleverStack/clever-email) - E-mail system for CleverStack.
76* [CloudTypes](https://github.com/ticup/CloudTypes) - JavaScript end2end implementation of the Cloud Types model for Eventual Consistency programming.
77* [dnode-tarantula](https://github.com/jutaz/dnode-tarantula) - an asynchronous rpc and event system for node.js based on dnode-protocol and TCP sockets.
78* [mongoose-url-shortener](https://www.npmjs.org/package/mongoose-url-shortener) - A simple URL Shortening library for NodeJS using Promises/A+ results.
79* [mozilla/smokejumper](https://github.com/mozilla/smokejumper) - The Smoke Jumper project is an effort to bring dead simple, secure, P2P file sharing to Firefox.
80* [shortness](https://npmjs.org/package/shortness) - Node based URL shortener that uses SQLite.
81* [file-db](https://npmjs.org/package/file-db) - Document database that uses directories and files to store its data, supporting nested key-value objects in named collections.
82* [resume-generator](https://www.npmjs.org/package/resume-generator) - Resume Generator.
83* [riffmint](https://npmjs.org/package/riffmint) - Collaboration in musical space.
84* [rap1ds/dippa](https://github.com/rap1ds/dippa) - Dippa Editor – A web-based LaTeX editor
85
86
87
88
89### API
90
91#### `generate()`
92
93Returns an amazingly short non-sequential unique id.
94
95__Alias:__ `shortId()`
96
97__Example__
98
99```js
100users.insert({
101 _id: shortId.generate()
102 name: ...
103 email: ...
104 });
105```
106
107---------------------------------------
108
109#### `seed(float)`
110
111__Default:__ `1`
112
113__Optional__
114
115Choose 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.
116
117Most developers won't need to use this, it's mainly for testing ShortId.
118
119If you are worried about users somehow decrypting the id then use it as a secret value for increased encryption.
120
121__Example__
122
123```js
124shortId.seed(1000);
125```
126
127---------------------------------------
128
129#### `characters(string)`
130
131__Default:__ `'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'`
132
133__Optional__
134
135Change the characters used.
136
137You must provide a string of all 64 unique characters. Order is not important.
138
139The default characters provided were selected because they are url safe.
140
141__Example__
142
143```js
144// use $ and @ instead of - an _
145shortId.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@');
146```
147
148```js
149// any 64 unicode charcters work
150shortId.characters('ⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ①②③④⑤⑥⑦⑧⑨⑩⑪⑫');
151```
152
153
154
155
156
157
158### About the Author [![@dylang](https://img.shields.io/badge/github-dylang-green.svg?style=flat-square)](https://github.com/dylang) [![@dylang](https://img.shields.io/badge/twitter-dylang-blue.svg?style=flat-square)](https://twitter.com/dylang)
159
160Hi! Thanks for checking `shortid`! My name is **Dylan Greene**. When not overwhelmed with my two young kids I enjoy contributing
161to the open source community. I'm also a tech lead at [Opower](http://opower.com).
162
163Here's some of my other Node projects:
164
165| Name | Description | npm Downloads |
166|---|---|--:|--:|
167| [`grunt‑notify`](https://github.com/dylang/grunt-notify) | Automatic desktop notifications for Grunt errors and warnings using Growl for OS X or Windows, Mountain Lion and Mavericks Notification Center, and Notify-Send. | [![grunt-notify](https://img.shields.io/npm/dm/grunt-notify.svg?style=flat-square)](https://www.npmjs.org/package/grunt-notify) |
168| [`grunt‑prompt`](https://github.com/dylang/grunt-prompt) | Interactive prompt for your Grunt config using console checkboxes, text input with filtering, password fields. | [![grunt-prompt](https://img.shields.io/npm/dm/grunt-prompt.svg?style=flat-square)](https://www.npmjs.org/package/grunt-prompt) |
169| [`npm‑check`](https://github.com/dylang/npm-check) | Check for outdated, incorrect, and unused dependencies. | [![npm-check](https://img.shields.io/npm/dm/npm-check.svg?style=flat-square)](https://www.npmjs.org/package/npm-check) |
170| [`rss`](https://github.com/dylang/node-rss) | RSS feed generator. Add RSS feeds to any project. Supports enclosures and GeoRSS. | [![rss](https://img.shields.io/npm/dm/rss.svg?style=flat-square)](https://www.npmjs.org/package/rss) |
171| [`xml`](https://github.com/dylang/node-xml) | Fast and simple xml generator. Supports attributes, CDATA, etc. Includes tests and examples. | [![xml](https://img.shields.io/npm/dm/xml.svg?style=flat-square)](https://www.npmjs.org/package/xml) |
172| [`changelog`](https://github.com/dylang/changelog) | Command line tool (and Node module) that generates a changelog in color output, markdown, or json for modules in npmjs.org's registry as well as any public github.com repo. | [![changelog](https://img.shields.io/npm/dm/changelog.svg?style=flat-square)](https://www.npmjs.org/package/changelog) |
173| [`grunt‑attention`](https://github.com/dylang/grunt-attention) | Display attention-grabbing messages in the terminal | [![grunt-attention](https://img.shields.io/npm/dm/grunt-attention.svg?style=flat-square)](https://www.npmjs.org/package/grunt-attention) |
174| [`observatory`](https://github.com/dylang/observatory) | Beautiful UI for showing tasks running on the command line. | [![observatory](https://img.shields.io/npm/dm/observatory.svg?style=flat-square)](https://www.npmjs.org/package/observatory) |
175| [`anthology`](https://github.com/dylang/anthology) | Module information and stats for any @npmjs user | [![anthology](https://img.shields.io/npm/dm/anthology.svg?style=flat-square)](https://www.npmjs.org/package/anthology) |
176| [`grunt‑cat`](https://github.com/dylang/grunt-cat) | Echo a file to the terminal. Works with text, figlets, ascii art, and full-color ansi. | [![grunt-cat](https://img.shields.io/npm/dm/grunt-cat.svg?style=flat-square)](https://www.npmjs.org/package/grunt-cat) |
177
178_This list was generated using [anthology](https://github.com/dylang/anthology)._
179
180
181### License
182Copyright (c) 2014 Dylan Greene, contributors.
183
184Released under the [MIT license](https://tldrlegal.com/license/mit-license).
185
186Screenshots are [CC BY-SA](http://creativecommons.org/licenses/by-sa/4.0/) (Attribution-ShareAlike).
187
188***
189_Generated using [grunt-readme](https://github.com/assemble/grunt-readme) with [grunt-templates-dylang](https://github.com/dylang/grunt-templates-dylang) on Friday, November 7, 2014._
190