You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
10 KiB

  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. > Amazingly short non-sequential url-friendly unique id generator.
  3. 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.
  4. * By default 7-14 url-friendly characters: `A-Z`, `a-z`, `0-9`, `_-`
  5. * Non-sequential so they are not predictable.
  6. * Supports `cluster` (automatically), custom seeds, custom alphabet.
  7. * Can generate any number of ids without duplicates, even millions per day.
  8. * Perfect for games, especially if you are concerned about cheating so you don't want an easily guessable id.
  9. * Apps can be restarted any number of times without any chance of repeating an id.
  10. * Popular replacement for Mongo ID/Mongoose ID.
  11. * Works in Node, io.js, and web browsers.
  12. * Includes [Mocha](http://visionmedia.github.com/mocha/) tests.
  13. ### Usage
  14. ```js
  15. var shortid = require('shortid');
  16. console.log(shortid.generate());
  17. //PPBqWA9
  18. ```
  19. Mongoose Unique Id
  20. ```js
  21. _id: {
  22. type: String,
  23. unique: true,
  24. 'default': shortid.generate
  25. },
  26. ```
  27. ### Browser Compatibility
  28. The best way to use `shortid` in the browser is via [browserify](http://browserify.org/) or [webpack](http://webpack.github.io/).
  29. These tools will automatically only include the files necessary for browser compatibility.
  30. All tests will run in the browser as well:
  31. ```bash
  32. ## build the bundle, then open Mocha in a browser to see the tests run.
  33. $ grunt build open
  34. ```
  35. ### Example
  36. ```js
  37. ~/projects/shortid ❯ node examples/examples.js
  38. eWRhpRV
  39. 23TplPdS
  40. 46Juzcyx
  41. dBvJIh-H
  42. 2WEKaVNO
  43. 7oet_d9Z
  44. dogPzIz8
  45. nYrnfYEv
  46. a4vhAoFG
  47. hwX6aOr7
  48. ```
  49. #### Real World Examples
  50. `shortId` was created for Node Knockout 2011 winner for Most Fun [Doodle Or Die](http://doodleordie.com).
  51. Millions of doodles have been saved with `shortId` filenames. Every log message gets a `shortId` to make it easy
  52. for us to look up later.
  53. Here are some other projects that use shortId:
  54. * [bevy](https://npmjs.org/package/bevy) - A simple server to manage multiple Node services.
  55. * [capre](https://npmjs.org/package/capre) - Cross-Server Data Replication.
  56. * [cordova-build](https://www.npmjs.org/package/cordova-build) - an alternative to phonegap build that runs on your servers/agents.
  57. * [couchdb-tools](https://www.npmjs.org/package/couchdb-tools) - A library of handy functions for use when working with CouchDB documents.
  58. * [CleverStack/clever-email](https://github.com/CleverStack/clever-email) - E-mail system for CleverStack.
  59. * [CloudTypes](https://github.com/ticup/CloudTypes) - JavaScript end2end implementation of the Cloud Types model for Eventual Consistency programming.
  60. * [dnode-tarantula](https://github.com/jutaz/dnode-tarantula) - an asynchronous rpc and event system for node.js based on dnode-protocol and TCP sockets.
  61. * [mongoose-url-shortener](https://www.npmjs.org/package/mongoose-url-shortener) - A simple URL Shortening library for NodeJS using Promises/A+ results.
  62. * [mozilla/smokejumper](https://github.com/mozilla/smokejumper) - The Smoke Jumper project is an effort to bring dead simple, secure, P2P file sharing to Firefox.
  63. * [shortness](https://npmjs.org/package/shortness) - Node based URL shortener that uses SQLite.
  64. * [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.
  65. * [resume-generator](https://www.npmjs.org/package/resume-generator) - Resume Generator.
  66. * [riffmint](https://npmjs.org/package/riffmint) - Collaboration in musical space.
  67. * [rap1ds/dippa](https://github.com/rap1ds/dippa) - Dippa Editor – A web-based LaTeX editor
  68. ### API
  69. `var shortid = require('shortid');`
  70. ---------------------------------------
  71. #### `shortid.generate()`
  72. __Returns__ `string` non-sequential unique id.
  73. __Example__
  74. ```js
  75. users.insert({
  76. _id: shortid.generate()
  77. name: ...
  78. email: ...
  79. });
  80. ```
  81. ---------------------------------------
  82. #### `characters(string)`
  83. __Default:__ `'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'`
  84. __Returns__ new alphabet as a `string`
  85. __Recommendation:__ If you don't like _ or -, you can to set new characters to use.
  86. __Optional__
  87. Change the characters used.
  88. You must provide a string of all 64 unique characters. Order is not important.
  89. The default characters provided were selected because they are url safe.
  90. __Example__
  91. ```js
  92. // use $ and @ instead of - and _
  93. shortid.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@');
  94. ```
  95. ```js
  96. // any 64 unicode characters work, but I wouldn't recommend this.
  97. shortid.characters('ⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ①②③④⑤⑥⑦⑧⑨⑩⑪⑫');
  98. ```
  99. ---------------------------------------
  100. #### `isValid(id)`
  101. __Returns__ `boolean`
  102. 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.
  103. __Example__
  104. ```js
  105. shortid.isValid('41XTDbE');
  106. // true
  107. ```
  108. ```js
  109. shortid.isValid('i have spaces');
  110. // false
  111. ```
  112. ---------------------------------------
  113. #### `shortid.worker(integer)`
  114. __Default:__ `process.env.NODE_UNIQUE_ID || 0`
  115. __Recommendation:__ You typically won't want to change this.
  116. __Optional__
  117. 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.
  118. If you do not do this there is very little chance of two servers generating the same id, but it is theatrically possible
  119. 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.
  120. __Example__
  121. ```js
  122. shortId.seed(1000);
  123. ```
  124. ---------------------------------------
  125. #### `shortid.seed(float)`
  126. __Default:__ `1`
  127. __Recommendation:__ You typically won't want to change this.
  128. __Optional__
  129. 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.
  130. Most developers won't need to use this, it's mainly for testing ShortId.
  131. If you are worried about users somehow decrypting the id then use it as a secret value for increased encryption.
  132. __Example__
  133. ```js
  134. shortId.seed(1000);
  135. ```
  136. ### About the Author
  137. Hi! Thanks for checking out this project! My name is **Dylan Greene**. When not overwhelmed with my two young kids I enjoy contributing
  138. to 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)
  139. Here's some of my other Node projects:
  140. | Name | Description | npm Downloads |
  141. |---|---|---|
  142. | [`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) |
  143. | [`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) |
  144. | [`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) |
  145. | [`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) |
  146. | [`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) |
  147. | [`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) |
  148. | [`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) |
  149. | [`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) |
  150. | [`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) |
  151. | [`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) |
  152. _This list was generated using [anthology](https://github.com/dylang/anthology)._
  153. ### License
  154. Copyright (c) 2015 Dylan Greene, contributors.
  155. Released under the [MIT license](https://tldrlegal.com/license/mit-license).
  156. Screenshots are [CC BY-SA](http://creativecommons.org/licenses/by-sa/4.0/) (Attribution-ShareAlike).
  157. ***
  158. _Generated using [grunt-readme](https://github.com/assemble/grunt-readme) with [grunt-templates-dylang](https://github.com/dylang/grunt-templates-dylang) on Sunday, April 5, 2015._
  159. _To make changes to this document look in `/templates/readme/`