UNPKG

10.5 kBMarkdownView Raw
1## shortid [![Build Status](http://img.shields.io/travis/dylang/shortid.svg)](https://travis-ci.org/dylang/shortid) [![shortid](http://img.shields.io/npm/dm/shortid.svg)](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 Redis ids, and any other id users might see.
13
14 * By default 7-14 url-friendly characters: `A-Z`, `a-z`, `0-9`, `_-`
15 * Supports `cluster` (automatically), custom seeds, custom alphabet.
16 * Can generate any number of ids without duplicates, even millions per day.
17 * Perfect for games, especially if you are concerned about cheating so you don't want an easily guessable id.
18 * Apps can be restarted any number of times without any chance of repeating an id.
19 * Popular replacement for Mongo ID/Mongoose ID.
20 * Works in Node, io.js, and web browsers.
21 * Includes [Mocha](http://mochajs.org/) tests.
22
23ShortId does not generate cryptographically secure ids, so don't rely on it to make IDs which are impossible to guess.
24
25
26### Usage
27
28```js
29const shortid = require('shortid');
30
31console.log(shortid.generate());
32// PPBqWA9
33```
34
35Mongoose Unique Id
36```js
37_id: {
38 'type': String,
39 'default': shortid.generate
40},
41```
42
43
44
45### Browser Compatibility
46
47The best way to use `shortid` in the browser is via [browserify](http://browserify.org/) or [webpack](http://webpack.github.io/).
48
49These tools will automatically only include the files necessary for browser compatibility.
50
51All tests will run in the browser as well:
52
53```bash
54## build the bundle, then open Mocha in a browser to see the tests run.
55$ grunt build open
56```
57
58
59
60### Example
61
62```bash
63~/projects/shortid ❯ node examples/examples.js
64eWRhpRV
6523TplPdS
6646Juzcyx
67dBvJIh-H
682WEKaVNO
697oet_d9Z
70dogPzIz8
71nYrnfYEv
72a4vhAoFG
73hwX6aOr7
74```
75
76
77#### Real World Examples
78
79`shortId` was created for Node Knockout 2011 winner for Most Fun [Doodle Or Die](http://doodleordie.com).
80Millions of doodles have been saved with `shortId` filenames. Every log message gets a `shortId` to make it easy
81for us to look up later.
82
83Here are some other projects that use shortId:
84
85* [bevy](https://npmjs.org/package/bevy) - A simple server to manage multiple Node services.
86* [capre](https://npmjs.org/package/capre) - Cross-Server Data Replication.
87* [cordova-build](https://www.npmjs.org/package/cordova-build) - an alternative to phonegap build that runs on your servers/agents.
88* [couchdb-tools](https://www.npmjs.org/package/couchdb-tools) - A library of handy functions for use when working with CouchDB documents.
89* [CleverStack/clever-email](https://github.com/CleverStack/clever-email) - E-mail system for CleverStack.
90* [CloudTypes](https://github.com/ticup/CloudTypes) - JavaScript end2end implementation of the Cloud Types model for Eventual Consistency programming.
91* [dnode-tarantula](https://github.com/jutaz/dnode-tarantula) - an asynchronous rpc and event system for node.js based on dnode-protocol and TCP sockets.
92* [mongoose-url-shortener](https://www.npmjs.org/package/mongoose-url-shortener) - A simple URL Shortening library for NodeJS using Promises/A+ results.
93* [mozilla/smokejumper](https://github.com/mozilla/smokejumper) - The Smoke Jumper project is an effort to bring dead simple, secure, P2P file sharing to Firefox.
94* [shortness](https://npmjs.org/package/shortness) - Node based URL shortener that uses SQLite.
95* [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.
96* [resume-generator](https://www.npmjs.org/package/resume-generator) - Resume Generator.
97* [riffmint](https://npmjs.org/package/riffmint) - Collaboration in musical space.
98* [rap1ds/dippa](https://github.com/rap1ds/dippa) - Dippa Editor – A web-based LaTeX editor
99
100
101
102
103### API
104
105```js
106var shortid = require('shortid');
107```
108
109---------------------------------------
110
111#### `shortid.generate()`
112
113__Returns__ `string` non-sequential unique id.
114
115__Example__
116
117```js
118users.insert({
119 _id: shortid.generate(),
120 name: '...',
121 email: '...'
122});
123```
124
125---------------------------------------
126
127#### `shortid.characters(string)`
128
129__Default:__ `'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'`
130
131__Returns__ new alphabet as a `string`
132
133__Recommendation:__ If you don't like _ or -, you can to set new characters to use.
134
135__Optional__
136
137Change the characters used.
138
139You must provide a string of all 64 unique characters. Order is not important.
140
141The default characters provided were selected because they are url safe.
142
143__Example__
144
145```js
146// use $ and @ instead of - and _
147shortid.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@');
148```
149
150```js
151// any 64 unicode characters work, but I wouldn't recommend this.
152shortid.characters('ⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ①②③④⑤⑥⑦⑧⑨⑩⑪⑫');
153```
154
155
156---------------------------------------
157
158#### `shortid.isValid(id)`
159
160__Returns__ `boolean`
161
162Check 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.
163
164__Example__
165
166```js
167shortid.isValid('41XTDbE');
168// true
169```
170
171```js
172shortid.isValid('i have spaces');
173// false
174```
175
176---------------------------------------
177
178#### `shortid.worker(integer)`
179
180__Default:__ `process.env.NODE_UNIQUE_ID || 0`
181
182__Recommendation:__ You typically won't want to change this.
183
184__Optional__
185
186If 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.
187If you do not do this there is very little chance of two servers generating the same id, but it is theoretically possible
188if 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.
189
190__Example__
191
192```js
193shortid.worker(1);
194```
195
196---------------------------------------
197
198#### `shortid.seed(integer)`
199
200__Default:__ `1`
201
202__Recommendation:__ You typically won't want to change this.
203
204__Optional__
205
206Choose 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.
207
208Most developers won't need to use this, it's mainly for testing ShortId.
209
210If you are worried about users somehow decrypting the id then use it as a secret value for increased encryption.
211
212__Example__
213
214```js
215shortid.seed(1000);
216```
217
218
219
220
221
222
223### About the Author
224
225Hi! Thanks for checking out this project! My name is **Dylan Greene**. When not overwhelmed with my two young kids I enjoy contributing
226to the open source community. I'm also a tech lead at [Opower](http://opower.com). [![@dylang](https://img.shields.io/badge/github-dylang-green.svg)](https://github.com/dylang) [![@dylang](https://img.shields.io/badge/twitter-dylang-blue.svg)](https://twitter.com/dylang)
227
228Here's some of my other Node projects:
229
230| Name | Description | npm Downloads |
231|---|---|---|
232| [`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) |
233| [`grunt‑notify`](https://github.com/dylang/grunt-notify) | Automatic desktop notifications for Grunt errors and warnings. Supports OS X, Windows, Linux. | [![grunt-notify](https://img.shields.io/npm/dm/grunt-notify.svg?style=flat-square)](https://www.npmjs.org/package/grunt-notify) |
234| [`space‑hogs`](https://github.com/dylang/space-hogs) | Discover surprisingly large directories from the command line. | [![space-hogs](https://img.shields.io/npm/dm/space-hogs.svg?style=flat-square)](https://www.npmjs.org/package/space-hogs) |
235| [`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) |
236| [`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) |
237| [`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) |
238| [`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) |
239| [`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) |
240| [`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) |
241| [`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) |
242| [`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) |
243
244_This list was generated using [anthology](https://github.com/dylang/anthology)._
245
246
247### License
248Copyright (c) 2016 Dylan Greene, contributors.
249
250Released under the [MIT license](https://tldrlegal.com/license/mit-license).
251
252Screenshots are [CC BY-SA](http://creativecommons.org/licenses/by-sa/4.0/) (Attribution-ShareAlike).