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 |
|
5 | ShortId 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
|
27 | var shortId = require('shortid');
|
28 |
|
29 | console.log(shortId.generate());
|
30 |
|
31 | PPBqWA9
|
32 | ```
|
33 |
|
34 | - - -
|
35 |
|
36 | ## Example
|
37 |
|
38 | ```js
|
39 | ~/projects/shortid ❯ node examples/examples.js
|
40 | eWRhpRV
|
41 | 23TplPdS
|
42 | 46Juzcyx
|
43 | dBvJIh-H
|
44 | 2WEKaVNO
|
45 | 7oet_d9Z
|
46 | dogPzIz8
|
47 | nYrnfYEv
|
48 | a4vhAoFG
|
49 | hwX6aOr7
|
50 | ```
|
51 |
|
52 | - - -
|
53 |
|
54 | ## API
|
55 |
|
56 | ### `generate()`
|
57 |
|
58 | Returns an amazingly short non-sequential unique id.
|
59 |
|
60 | __Alias:__ `shortId()`
|
61 |
|
62 | __Example__
|
63 |
|
64 | ```js
|
65 | users.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 |
|
80 | 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 before using `shortId` and always use the same value in your application.
|
81 |
|
82 | __Example__
|
83 |
|
84 | ```js
|
85 | shortId.seed(1000);
|
86 | ```
|
87 |
|
88 | ---------------------------------------
|
89 |
|
90 | ### `characters(string)`
|
91 |
|
92 | __Default:__ `'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'`
|
93 |
|
94 | __Optional__
|
95 |
|
96 | Change the characters used.
|
97 |
|
98 | You 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 _
|
104 | shortId.alphabet('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@');
|
105 | ```
|
106 |
|
107 | ```js
|
108 | // any 64 unicode charcters work
|
109 | shortId.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).
|
117 | Millions of doodles have been saved with `shortId` filenames. Every log message gets a `shortId` to make it easy
|
118 | for us to look up later.
|
119 |
|
120 | Here 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 |
|
138 | Copyright (c) 2011-2013 Dylan Greene <dylang@gmail.com>
|
139 |
|
140 | Permission is hereby granted, free of charge, to any person obtaining a copy
|
141 | of this software and associated documentation files (the "Software"), to deal
|
142 | in the Software without restriction, including without limitation the rights
|
143 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
144 | copies of the Software, and to permit persons to whom the Software is
|
145 | furnished to do so, subject to the following conditions:
|
146 |
|
147 | The above copyright notice and this permission notice shall be included in
|
148 | all copies or substantial portions of the Software.
|
149 |
|
150 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
151 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
152 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
153 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
154 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
155 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
156 | THE SOFTWARE. |
\ | No newline at end of file |