Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve committed Apr 30, 2018
0 parents commit ee49139
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
.env
node_modules/
bower_components/
local/cache/*
local/storage/*
local/temp/*
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Generate Unique ID
Generate a unique random ID string.

## Installation
```html
npm install generate-unique-id --save
```

## Usage
```html
const generateUniqueId = require('generate-unique-id');

// example 1
const id = generateUniqueId.init();

// example 2
const id = generateUniqueId.init({
length: 32,
useLetters: false
});

// example 3
const id = generateUniqueId.init({
includeSymbols: ['@','#','|'],
excludeSymbols: ['0']
});
```

## Options
<table>
<tr>
<th>Option</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
<tr>
<td>length</td>
<td>string</td>
<td>20</td>
<td>Length of the generated ID.</td>
</tr>
<tr>
<td>useLetters</td>
<td>boolean</td>
<td>true</td>
<td>Use letters (English alphabet) as part of the generated ID.</td>
</tr>
<tr>
<td>useNumbers</td>
<td>boolean</td>
<td>true</td>
<td>Use numbers as part of the generated ID.</td>
</tr>
<tr>
<td>includeSymbols</td>
<td>array</td>
<td>[]</td>
<td>Use additional letters as part of the generated ID.</td>
</tr>
<tr>
<td>excludeSymbols</td>
<td>array</td>
<td>[]</td>
<td>Do not use these symbols as part of the generated ID.</td>
</tr>
</table>

## License
[MIT license](http://www.opensource.org/licenses/MIT)
50 changes: 50 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

var generateUniqueId = {
defaultOpts: {
length: 10,
useLetters: true,
useNumbers: true,
includeSymbols: [],
excludeSymbols: []
},
init: function (opts={}) {
this.config = Object.assign({}, this.defaultOpts, opts);
let mainArr = [];
let lettersArr = [];
let numbersArr = [];

if (this.config.useLetters) {
if (this.config.excludeSymbols.length) this.filterSymbols('letters');
lettersArr = this.letters.split('');
}

if (this.config.useNumbers) {
if (this.config.excludeSymbols.length) this.filterSymbols('numbers');
numbersArr = this.numbers.split('');
}

this.pool = mainArr.concat(lettersArr, numbersArr, this.config.includeSymbols);

return this.createID();
},
letters: 'abcdefghijklmnopqrstuvwxyz',
numbers: '0123456789',
pool: [],
filterSymbols: function(group) {
this.config.excludeSymbols.map(item => this[group] = this[group].replace(item, ''));
},
getRandomNumber: function() {
return Math.floor(Math.random()*this.pool.length);
},
createID: function() {
let id = '';
for (let i=0;i<this.config.length;i++){
id += this.pool[this.getRandomNumber()];
}

return id;
}
}

module.exports = generateUniqueId;
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "generate-unique-id",
"version": "1.0.0",
"description": "Generate a unique random ID string.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"generate",
"create",
"unique",
"random",
"id",
"serial",
"string"
],
"author": {
"name": "steve-232",
"email": "steve232np@gmail.com"
},
"repository": {
"type": "git",
"url": "https://github.com/steve-232/generate-unique-id"
},
"license": "MIT"
}

0 comments on commit ee49139

Please sign in to comment.