UNPKG

4.18 kBMarkdownView Raw
1# ShortId [![Build Status](https://secure.travis-ci.org/dylang/shortid.png)](http://travis-ci.org/dylang/shortid)
2
3[![NPM](https://nodei.co/npm/shortid.png?downloads=true)](https://nodei.co/npm/shortid/)
4
5ShortId creates amazingly short non-sequential url-friendly unique ids. Perfect for url shorteners, MongoDB and Reddis ids, and any other id users might see.
6
7 * By default 7-12 url-friendly characters: `A-Z`, `a-z`, `0-9`, `_-`
8 * Non-sequential so they are not predictable.
9 * Supports `cluster` (automatically), custom seeds, custom alphabet.
10 * Includes [Mocha](http://visionmedia.github.com/mocha/) tests.
11
12- - -
13
14## Install
15
16```shell
17$ npm install shortid --save
18```
19
20`shortid` has no dependencies.
21
22- - -
23
24## Usage
25
26```js
27var shortId = require('shortid');
28
29console.log(shortId.generate());
30
31PPBqWA9
32```
33
34- - -
35
36## Example
37
38```js
39~/projects/shortid ❯ node examples/examples.js
40eWRhpRV
4123TplPdS
4246Juzcyx
43dBvJIh-H
442WEKaVNO
457oet_d9Z
46dogPzIz8
47nYrnfYEv
48a4vhAoFG
49hwX6aOr7
50```
51
52- - -
53
54## API
55
56### `generate()`
57
58Returns an amazingly short non-sequential unique id.
59
60__Alias:__ `shortId()`
61
62__Example__
63
64```js
65users.insert({
66 _id: shortId.generate()
67 name: ...
68 email: ...
69 });
70```
71
72---------------------------------------
73
74### `seed(float)`
75
76__Default:__ `1`
77
78__Optional__
79
80Choose 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 before using `shortId` and always use the same value in your application.
81
82__Example__
83
84```js
85shortId.seed(1000);
86```
87
88---------------------------------------
89
90### `characters(string)`
91
92__Default:__ `'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'`
93
94__Optional__
95
96Change the characters used.
97
98You must provide a string of all 64 unique characters. Order is not important.
99
100__Example__
101
102```js
103// use $ and @ instead of - an _
104shortId.alphabet('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@');
105```
106
107```js
108// any 64 unicode charcters work
109shortId.alphabet('ⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ①②③④⑤⑥⑦⑧⑨⑩⑪⑫');
110```
111
112- - -
113
114### Some projects using `shortId`
115
116`shortId` was created for Node Knockout 2011 winner for Most Fun [Doodle Or Die](http://doodleordie.com).
117Millions of doodles have been saved with `shortId` filenames. Every log message gets a `shortId` to make it easy
118for us to look up later.
119
120Here are some others:
121
122[bevy](https://npmjs.org/package/bevy) - A simple server to manage multiple Node services.
123
124[capre](https://npmjs.org/package/capre) - Cross-Server Data Replication.
125
126[riffmint](https://npmjs.org/package/riffmint) - Collaboration in musical space.
127
128[shortness](https://npmjs.org/package/shortness) - Node based URL shortener that uses SQLite.
129
130[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.
131
132- - -
133
134## License
135
136(The MIT License)
137
138Copyright (c) 2011-2013 Dylan Greene <dylang@gmail.com>
139
140Permission is hereby granted, free of charge, to any person obtaining a copy
141of this software and associated documentation files (the "Software"), to deal
142in the Software without restriction, including without limitation the rights
143to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
144copies of the Software, and to permit persons to whom the Software is
145furnished to do so, subject to the following conditions:
146
147The above copyright notice and this permission notice shall be included in
148all copies or substantial portions of the Software.
149
150THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
152FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
153AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
154LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
155OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
156THE SOFTWARE.
\No newline at end of file