diff --git a/README.md b/README.md index 973e068..7f394c4 100644 --- a/README.md +++ b/README.md @@ -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: @@ -13,10 +15,19 @@ $ npm install --save voucher-code-generator ``` Include with require: + ``` var voucher_codes = require('voucher-code-generator'); ``` +#### In a browser + +``` + +``` + +### Usage + Generate 5 codes, each 8 characters long: ``` voucher_codes.generate({ diff --git a/package.json b/package.json index 628d5b3..4f50123 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/test.html b/test/test.html new file mode 100644 index 0000000..34647b3 --- /dev/null +++ b/test/test.html @@ -0,0 +1,21 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/voucher_codes.js b/voucher_codes.js index 6c1d74c..1769163 100644 --- a/voucher_codes.js +++ b/voucher_codes.js @@ -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); \ No newline at end of file