Codebase list node-uniqid / 52970f7
New upstream version 5.2.0 Xavier Guimard 3 years ago
3 changed file(s) with 48 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
00 ![uniqid logo](http://i.imgur.com/OrZC1lc.png)
11
2 ![unqiid npm badge](http://img.shields.io/npm/v/uniqid.svg) ![uniqid npm downloads badge](https://img.shields.io/npm/dm/uniqid.svg)
2 ![unqiid npm badge](http://img.shields.io/npm/v/uniqid.svg) ![uniqid npm downloads badge](https://img.shields.io/npm/dm/uniqid.svg)
33
4 ### A Unique Hexatridecimal ID generator.
4 ### A Unique Hexatridecimal ID generator.
5
56 It will always create unique id's based on the current time, process and machine name.
67
78 ```
910 ```
1011
1112 ## Usage
13
1214 ```js
1315 var uniqid = require('uniqid');
1416
1719 ```
1820
1921 ## Features
22
2023 - Very fast
2124 - Generates unique id's on multiple processes and machines even if called at the same time.
2225 - Shorter 8 and 12 byte versions with less uniqueness.
2326
27 # How it works
2428
25 # How it works
2629 - With the current time the ID's are always unique in a single process.
2730 - With the Process ID the ID's are unique even if called at the same time from multiple processes.
2831 - With the MAC Address the ID's are unique even if called at the same time from multiple machines and processes.
2932
3033 ## 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.
3334
34 ```js
35 #### **uniqid(** prefix _optional string_ , suffix _optional string_ **)**
36
37 Generate 18 byte unique id's based on the time, process id and mac address. Works on multiple processes and machines.
38
39 ```js "zu4850jkyfoxok"
3540 uniqid() -> "4n5pxq24kpiob12og9"
3641 uniqid('hello-') -> "hello-4n5pxq24kpiob12og9"
42 uniqid('hello-', '-goodbye') -> "hello-4n5pxq24kpiob12og9-goodbye"
43
44 // usage with suffix only
45 uniqid('', '-goodbye') -> "4n5pxq24kpiob12og9-goodbye"
46 uniqid(undefined, '-goodbye') -> "4n5pxq24kpiob12og9-goodbye"
3747 ```
3848
39 #### **uniqid.process(** prefix *optional string* **)**
49 #### **uniqid.process(** prefix _optional string_ , suffix _optional string_ **)**
50
4051 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.
52
4153 ```js
4254 uniqid.process() -> "24ieiob0te82"
4355 ```
4456
45 #### **uniqid.time(** prefix *optional string* **)**
57 #### **uniqid.time(** prefix _optional string_ , suffix _optional string_ **)**
58
4659 Generate 8 byte unique id's based on the current time only. Recommended only on a single process on a single machine.
4760
4861 ```js
5063 ```
5164
5265 ## Webpack and Browserify
66
5367 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.
5468
5569 ## 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.
70 Debug messages are turned off 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.
5771
5872 ```js
5973 // enable debug messages
60 module.uniqid_debug = true
74 module.uniqid_debug = true;
6175
6276 // require the module
63 var uniqid = require('uniqid')
77 var uniqid = require('uniqid');
6478 ```
6579
6680 ## **License**
6781
6882 (The MIT License)
6983
70 Copyright (c) 2014 Halász Ádám <mail@adamhalasz.com>
84 Copyright (c) 2014-2019 [Halász Ádám](https://adamhalasz.com) <mail@adamhalasz.com>
7185
7286 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:
7387
00 /*
11 (The MIT License)
2 Copyright (c) 2014 Halász Ádám <mail@adamhalasz.com>
2 Copyright (c) 2014-2019 Halász Ádám <mail@adamhalasz.com>
33 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:
44 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
55 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.
1111 // Dependencies
1212 // ================================================
1313 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) : '' ;
14 var address = '';
15 if(typeof __webpack_require__ !== 'function'){
16 var mac = '', networkInterfaces = require('os').networkInterfaces();
17 for(let interface_key in networkInterfaces){
18 const networkInterface = networkInterfaces[interface_key];
19 const length = networkInterface.length;
20 for(var i = 0; i < length; i++){
21 if(networkInterface[i].mac && networkInterface[i].mac != '00:00:00:00:00:00'){
22 mac = networkInterface[i].mac; break;
23 }
24 }
25 }
26 address = mac ? parseInt(mac.replace(/\:|\D+/gi, '')).toString(36) : '' ;
27 }
1628
1729 // Exports
1830 // ================================================
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); }
31 module.exports = module.exports.default = function(prefix, suffix){ return (prefix ? prefix : '') + address + pid + now().toString(36) + (suffix ? suffix : ''); }
32 module.exports.process = function(prefix, suffix){ return (prefix ? prefix : '') + pid + now().toString(36) + (suffix ? suffix : ''); }
33 module.exports.time = function(prefix, suffix){ return (prefix ? prefix : '') + now().toString(36) + (suffix ? suffix : ''); }
2234
2335 // Helpers
2436 // ================================================
2739 var last = now.last || time;
2840 return now.last = time > last ? time : last + 1;
2941 }
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 }
00 {
11 "name": "uniqid",
2 "version": "4.1.1",
2 "version": "5.2.0",
33 "description": "Unique ID Generator",
4 "homepage": "http://github.com/adamhalasz/diet-uniqid/",
4 "homepage": "http://github.com/adamhalasz/uniqid/",
55 "keywords": [
66 "unique id",
77 "uniqid",
2626 "url": "http://adamhalasz.com/"
2727 },
2828 "main": "index.js",
29 "dependencies": {
30 "macaddress": "^0.2.8"
31 }
29 "dependencies": {},
30 "devDependencies": {}
3231 }