-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from rspective/browserize
make the module work in a browser
- Loading branch information
Showing
4 changed files
with
80 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |