Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional suffix parameter #21

Merged
merged 1 commit into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
![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.

```
npm install uniqid
```

## Usage

```js
var uniqid = require('uniqid');

Expand All @@ -18,50 +20,63 @@ 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
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**
Expand Down
6 changes: 3 additions & 3 deletions examples/browserify/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ================================================
Expand Down
6 changes: 5 additions & 1 deletion examples/simple/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
var uniqid = require('../../');
console.log(uniqid(), uniqid());
console.log(uniqid(), uniqid());
console.log(uniqid('prefix-'));
console.log(uniqid('prefix-', '-suffix'));
console.log(uniqid('', '-suffix'));
console.log(uniqid(undefined, '-suffix'));
2 changes: 1 addition & 1 deletion examples/webpack/dist/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,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
// ================================================
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.