Skip to content

Commit

Permalink
Merge pull request #3 from rspective/browserize
Browse files Browse the repository at this point in the history
make the module work in a browser
  • Loading branch information
tpindel committed Dec 2, 2015
2 parents a0703ae + c806251 commit b2cc4b6
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 34 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Generate unique, hard to guess coupon, voucher codes.

This library originates from [Voucherify](http://www.voucherify.io/?utm_source=inbound&utm_medium=github&utm_campaign=js-voucher-code-generator-beta).

### Usage
### Installation

#### In Node.js

Install with npm:

Expand All @@ -13,10 +15,19 @@ $ npm install --save voucher-code-generator
```

Include with require:

```
var voucher_codes = require('voucher-code-generator');
```

#### In a browser

```
<script src="voucher_codes.js"></script>
```

### Usage

Generate 5 codes, each 8 characters long:
```
voucher_codes.generate({
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "voucher-code-generator",
"version": "0.0.2",
"version": "0.0.3",

"homepage": "http://www.voucherify.io/",
"description": "Voucher Code Generator",
Expand Down
21 changes: 21 additions & 0 deletions test/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script src="../voucher_codes.js"></script>
</head>
<body>
<ul id="codes"></ul>
<script>
var codesList = document.getElementById("codes");
var codes = voucher_codes.generate({
length: 6,
count: 4
});
codes.forEach(function(code) {
codesList.innerHTML += "<li>" + code + "</li>";
});
</script>
</body>
</html>
78 changes: 46 additions & 32 deletions voucher_codes.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,50 @@
"use strict";

function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

function randomElem(arr) {
return arr[randomInt(0, arr.length-1)];
}

function generateOne(config) {
var length = config.length || 8;
var charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var code = "";
for (var i = 0; i < length; i++) {
code += randomElem(charset);
;(function() {
"use strict";

var root = this;

function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

function randomElem(arr) {
return arr[randomInt(0, arr.length - 1)];
}

function generateOne(config) {
var length = config.length || 8;
var charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var code = "";
for (var i = 0; i < length; i++) {
code += randomElem(charset);
}
return code;
}

function generate(config) {
var count = config.count || 1;
var codes = {};
while (count > 0) {
var code = generateOne(config);
if (codes[code] === undefined) {
codes[code] = true;
count--;
}
}
return Object.keys(codes);
}
return code;
}

function generate(config) {
var count = config.count || 1;
var codes = {};
while (count > 0) {
var code = generateOne(config);
if (codes[code] === undefined) {
codes[code] = true;
count--;

var voucher_codes = {
generate: generate
};

if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = voucher_codes;
}
exports = voucher_codes;
} else {
root.voucher_codes = voucher_codes;
}
return Object.keys(codes);
}

module.exports = {
generate: generate
};
}).call(this);

0 comments on commit b2cc4b6

Please sign in to comment.