From c6ce072d0655fdf2554cb362e0d1db34665d491f Mon Sep 17 00:00:00 2001 From: Brandon Mitchell Date: Fri, 17 Aug 2018 17:08:34 -0400 Subject: [PATCH] Add optional suffix parameter --- Readme.md | 35 +++++++++++++++++++++++++---------- examples/browserify/bundle.js | 6 +++--- examples/simple/index.js | 6 +++++- examples/webpack/dist/main.js | 2 +- index.js | 6 +++--- package-lock.json | 2 +- 6 files changed, 38 insertions(+), 19 deletions(-) diff --git a/Readme.md b/Readme.md index 1bb2b4f..806bb49 100644 --- a/Readme.md +++ b/Readme.md @@ -1,8 +1,9 @@ ![uniqid logo](http://i.imgur.com/OrZC1lc.png) -![unqiid npm badge](http://img.shields.io/npm/v/uniqid.svg) ![uniqid npm downloads badge](https://img.shields.io/npm/dm/uniqid.svg) +![unqiid npm badge](http://img.shields.io/npm/v/uniqid.svg) ![uniqid npm downloads badge](https://img.shields.io/npm/dm/uniqid.svg) + +### A Unique Hexatridecimal ID generator. -### A Unique Hexatridecimal ID generator. It will always create unique id's based on the current time, process and machine name. ``` @@ -10,6 +11,7 @@ npm install uniqid ``` ## Usage + ```js var uniqid = require('uniqid'); @@ -18,32 +20,43 @@ console.log(uniqid(), uniqid()); // -> 4n5pxq24kriob12ogd, 4n5pxq24ksiob12ogl ``` ## Features + - Very fast - Generates unique id's on multiple processes and machines even if called at the same time. - Shorter 8 and 12 byte versions with less uniqueness. - # How it works + - With the current time the ID's are always unique in a single process. - With the Process ID the ID's are unique even if called at the same time from multiple processes. - With the MAC Address the ID's are unique even if called at the same time from multiple machines and processes. ## API: -#### **uniqid(** prefix *optional string* **)** -Generate 18 byte unique id's based on the time, process id and mac address. Works on multiple processes and machines. -```js +#### **uniqid(** prefix _optional string_ , suffix _optional string_ **)** + +Generate 18 byte unique id's based on the time, process id and mac address. Works on multiple processes and machines. + +```js "zu4850jkyfoxok" uniqid() -> "4n5pxq24kpiob12og9" uniqid('hello-') -> "hello-4n5pxq24kpiob12og9" +uniqid('hello-', '-goodbye') -> "hello-4n5pxq24kpiob12og9-goodbye" + +// usage with suffix only +uniqid('', '-goodbye') -> "4n5pxq24kpiob12og9-goodbye" +uniqid(undefined, '-goodbye') -> "4n5pxq24kpiob12og9-goodbye" ``` -#### **uniqid.process(** prefix *optional string* **)** +#### **uniqid.process(** prefix _optional string_ , suffix _optional string_ **)** + 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. + ```js uniqid.process() -> "24ieiob0te82" ``` -#### **uniqid.time(** prefix *optional string* **)** +#### **uniqid.time(** prefix _optional string_ , suffix _optional string_ **)** + Generate 8 byte unique id's based on the current time only. Recommended only on a single process on a single machine. ```js @@ -51,17 +64,19 @@ uniqid.time() -> "iob0ucoj" ``` ## Webpack and Browserify + 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. ## Debug + 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. ```js // enable debug messages -module.uniqid_debug = true +module.uniqid_debug = true; // require the module -var uniqid = require('uniqid') +var uniqid = require('uniqid'); ``` ## **License** diff --git a/examples/browserify/bundle.js b/examples/browserify/bundle.js index 5c84e85..d389f53 100644 --- a/examples/browserify/bundle.js +++ b/examples/browserify/bundle.js @@ -278,9 +278,9 @@ if(typeof __webpack_require__ !== 'function'){ // Exports // ================================================ -module.exports = module.exports.default = function(prefix){ return (prefix || '') + address + pid + now().toString(36); } -module.exports.process = function(prefix){ return (prefix || '') + pid + now().toString(36); } -module.exports.time = function(prefix){ return (prefix || '') + now().toString(36); } +module.exports = module.exports.default = function(prefix = '', suffix = ''){ return prefix + address + pid + now().toString(36) + suffix; } +module.exports.process = function(prefix = '', suffix = ''){ return prefix + pid + now().toString(36) + suffix; } +module.exports.time = function(prefix = '', suffix = ''){ return prefix + now().toString(36) + suffix; } // Helpers // ================================================ diff --git a/examples/simple/index.js b/examples/simple/index.js index 3b3cde1..ac37ff3 100644 --- a/examples/simple/index.js +++ b/examples/simple/index.js @@ -1,2 +1,6 @@ var uniqid = require('../../'); -console.log(uniqid(), uniqid()); \ No newline at end of file +console.log(uniqid(), uniqid()); +console.log(uniqid('prefix-')); +console.log(uniqid('prefix-', '-suffix')); +console.log(uniqid('', '-suffix')); +console.log(uniqid(undefined, '-suffix')); \ No newline at end of file diff --git a/examples/webpack/dist/main.js b/examples/webpack/dist/main.js index cbc65d3..0b5f4dc 100644 --- a/examples/webpack/dist/main.js +++ b/examples/webpack/dist/main.js @@ -1 +1 @@ -!function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=2)}([function(t,e){var n,r,o=t.exports={};function i(){throw new Error("setTimeout has not been defined")}function u(){throw new Error("clearTimeout has not been defined")}function c(t){if(n===setTimeout)return setTimeout(t,0);if((n===i||!n)&&setTimeout)return n=setTimeout,setTimeout(t,0);try{return n(t,0)}catch(e){try{return n.call(null,t,0)}catch(e){return n.call(this,t,0)}}}!function(){try{n="function"==typeof setTimeout?setTimeout:i}catch(t){n=i}try{r="function"==typeof clearTimeout?clearTimeout:u}catch(t){r=u}}();var l,f=[],a=!1,s=-1;function p(){a&&l&&(a=!1,l.length?f=l.concat(f):s=-1,f.length&&d())}function d(){if(!a){var t=c(p);a=!0;for(var e=f.length;e;){for(l=f,f=[];++s1)for(var n=1;ne?t:e+1}t.exports=t.exports.default=function(t){return(t||"")+""+n+r().toString(36)},t.exports.process=function(t){return(t||"")+n+r().toString(36)},t.exports.time=function(t){return(t||"")+r().toString(36)}}).call(this,n(0))},function(t,e,n){var r=n(1);window.onload=function(){for(var t=document.getElementsByTagName("ul")[0],e=0;e<1e3;e++){var n=document.createElement("li");n.innerHTML=r(),t.appendChild(n)}}}]); \ No newline at end of file +!function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){var r=n(1);window.onload=function(){for(var t=document.getElementsByTagName("ul")[0],e=0;e<1e3;e++){var n=document.createElement("li");n.innerHTML=r(),t.appendChild(n)}}},function(t,e,n){(function(e){var n=e&&e.pid?e.pid.toString(36):"";function r(){var t=Date.now(),e=r.last||t;return r.last=t>e?t:e+1}t.exports=t.exports.default=function(t="",e=""){return t+""+n+r().toString(36)+e},t.exports.process=function(t="",e=""){return t+n+r().toString(36)+e},t.exports.time=function(t="",e=""){return t+r().toString(36)+e}}).call(this,n(2))},function(t,e){var n,r,o=t.exports={};function i(){throw new Error("setTimeout has not been defined")}function u(){throw new Error("clearTimeout has not been defined")}function c(t){if(n===setTimeout)return setTimeout(t,0);if((n===i||!n)&&setTimeout)return n=setTimeout,setTimeout(t,0);try{return n(t,0)}catch(e){try{return n.call(null,t,0)}catch(e){return n.call(this,t,0)}}}!function(){try{n="function"==typeof setTimeout?setTimeout:i}catch(t){n=i}try{r="function"==typeof clearTimeout?clearTimeout:u}catch(t){r=u}}();var l,f=[],a=!1,s=-1;function p(){a&&l&&(a=!1,l.length?f=l.concat(f):s=-1,f.length&&d())}function d(){if(!a){var t=c(p);a=!0;for(var e=f.length;e;){for(l=f,f=[];++s1)for(var n=1;n