Codebase list node-uniqid / 2930a10
Import Upstream version 4.1.1 Pirate Praveen 6 years ago
3 changed file(s) with 147 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 ![uniqid logo](http://i.imgur.com/OrZC1lc.png)
1
2 ![unqiid npm badge](http://img.shields.io/npm/v/uniqid.svg) ![uniqid npm downloads badge](https://img.shields.io/npm/dm/uniqid.svg)
3
4 ### A Unique Hexatridecimal ID generator.
5 It will always create unique id's based on the current time, process and machine name.
6
7 ```
8 npm install uniqid
9 ```
10
11 ## Usage
12 ```js
13 var uniqid = require('uniqid');
14
15 console.log(uniqid()); // -> 4n5pxq24kpiob12og9
16 console.log(uniqid(), uniqid()); // -> 4n5pxq24kriob12ogd, 4n5pxq24ksiob12ogl
17 ```
18
19 ## Features
20 - Very fast
21 - Generates unique id's on multiple processes and machines even if called at the same time.
22 - Shorter 8 and 12 byte versions with less uniqueness.
23
24
25 # How it works
26 - With the current time the ID's are always unique in a single process.
27 - With the Process ID the ID's are unique even if called at the same time from multiple processes.
28 - With the MAC Address the ID's are unique even if called at the same time from multiple machines and processes.
29
30 ## API:
31 #### **uniqid(** prefix *optional string* **)**
32 Generate 18 byte unique id's based on the time, process id and mac address. Works on multiple processes and machines.
33
34 ```js
35 uniqid() -> "4n5pxq24kpiob12og9"
36 uniqid('hello-') -> "hello-4n5pxq24kpiob12og9"
37 ```
38
39 #### **uniqid.process(** prefix *optional string* **)**
40 Generate 12 byte unique id's based on the time and the process id. Works on multiple processes within a single machine but not on multiple machines.
41 ```js
42 uniqid.process() -> "24ieiob0te82"
43 ```
44
45 #### **uniqid.time(** prefix *optional string* **)**
46 Generate 8 byte unique id's based on the current time only. Recommended only on a single process on a single machine.
47
48 ```js
49 uniqid.time() -> "iob0ucoj"
50 ```
51
52 ## Webpack and Browserify
53 Since browsers don't provide a Process ID and in most cases neither give a Mac Address using uniqid from Webpack and Browserify falls back to `uniqid.time()` for all the other methods too. The browser is the single process, single machine case anyway.
54
55 ## Debug
56 Debug messages are turned of by default as of `v4.1.0`. To turn on debug messages you'll need to set `uniqid_debug` to `true` before you require the module.
57
58 ```js
59 // enable debug messages
60 module.uniqid_debug = true
61
62 // require the module
63 var uniqid = require('uniqid')
64 ```
65
66 ## **License**
67
68 (The MIT License)
69
70 Copyright (c) 2014 Halász Ádám <mail@adamhalasz.com>
71
72 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
73
74 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
75
76 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0 /*
1 (The MIT License)
2 Copyright (c) 2014 Halász Ádám <mail@adamhalasz.com>
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6 */
7
8 // Unique Hexatridecimal ID Generator
9 // ================================================
10
11 // Dependencies
12 // ================================================
13 var pid = process && process.pid ? process.pid.toString(36) : '' ;
14 var mac = typeof __webpack_require__ !== 'function' ? require('macaddress').one(macHandler) : null ;
15 var address = mac ? parseInt(mac.replace(/\:|\D+/gi, '')).toString(36) : '' ;
16
17 // Exports
18 // ================================================
19 module.exports = function(prefix){ return (prefix || '') + address + pid + now().toString(36); }
20 module.exports.process = function(prefix){ return (prefix || '') + pid + now().toString(36); }
21 module.exports.time = function(prefix){ return (prefix || '') + now().toString(36); }
22
23 // Helpers
24 // ================================================
25 function now(){
26 var time = Date.now();
27 var last = now.last || time;
28 return now.last = time > last ? time : last + 1;
29 }
30
31 function macHandler(error){
32 if(module.parent && module.parent.uniqid_debug){
33 if(error) console.error('Info: No mac address - uniqid() falls back to uniqid.process().', error)
34 if(pid == '') console.error('Info: No process.pid - uniqid.process() falls back to uniqid.time().')
35 }
36 }
0 {
1 "name": "uniqid",
2 "version": "4.1.1",
3 "description": "Unique ID Generator",
4 "homepage": "http://github.com/adamhalasz/diet-uniqid/",
5 "keywords": [
6 "unique id",
7 "uniqid",
8 "unique identifier",
9 "hexatridecimal"
10 ],
11 "bugs": {
12 "url": "http://github.com/adamhalasz/uniqid/issues",
13 "email": "mail@adamhalasz.com"
14 },
15 "repository": {
16 "type": "git",
17 "url": "https://github.com/adamhalasz/uniqid.git"
18 },
19 "files": [
20 "index.js"
21 ],
22 "license": "MIT",
23 "author": {
24 "name": "Halász Ádám",
25 "email": "mail@adamhalasz.com",
26 "url": "http://adamhalasz.com/"
27 },
28 "main": "index.js",
29 "dependencies": {
30 "macaddress": "^0.2.8"
31 }
32 }