New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

voucher-code-generator

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

voucher-code-generator - npm Package Compare versions

Comparing version

to
1.1.0

2

package.json
{
"name": "voucher-code-generator",
"version": "1.0.0",
"version": "1.1.0",

@@ -5,0 +5,0 @@ "homepage": "http://www.voucherify.io/",

@@ -1,7 +0,9 @@

## Voucher Code Generator
## Coupon Code Generator
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).
Use cases: promo codes, loyalty coupons, gift vouchers, in-app purchases
This library originates from [Voucherify](http://www.voucherify.io/?utm_source=github&utm_medium=opensource&utm_campaign=acq).
### Installation

@@ -104,2 +106,20 @@

#### Infeasible configs
There exist some configs that are not feasible. For example it's not possible to generate 1000 codes if you want
your codes to be 2 characters long and consisting only of numbers. Voucher code generator detects such cases and
throws an error `"Not possible to generate requested number of codes."`.
```
try {
voucher_codes.generate({
count: 1000,
length: 2,
charset: "0123456789"
})
catch (e) {
console.log("Sorry, not possible.");
}
```
#### Config reference

@@ -129,2 +149,6 @@

npm run test
```
```
### License
Code released under the [MIT license](LICENSE).

@@ -102,2 +102,14 @@ var voucher_codes = require('../voucher_codes.js');

it('should detect infeasible config', function(){
var config = {
count: 1000,
charset: "abc",
length: 5
}; // there are only 125 (5^3) possible codes for this config
expect(function() {
voucher_codes.generate(config);
}).toThrow("Not possible to generate requested number of codes.");
})
});

@@ -0,1 +1,9 @@

/**
* Copyright 2015 rspective (http://rspective.com)
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
;(function() {

@@ -31,11 +39,16 @@ "use strict";

function Config(config) {
config = config || {};
this.count = config.count || 1;
this.length = config.length || 8;
this.charset = config.charset || charset("alphanumeric");
this.prefix = config.prefix || "";
this.postfix = config.postfix || "";
this.pattern = config.pattern || repeat("#", this.length);
}
function generateOne(config) {
var length = config.length || 8;
var chars = config.charset || charset("alphanumeric");
var prefix = config.prefix || "";
var postfix = config.postfix || "";
var pattern = config.pattern || repeat("#", length);
var code = pattern.split('').map(function(char) {
var code = config.pattern.split('').map(function(char) {
if (char === '#') {
return randomElem(chars);
return randomElem(config.charset);
} else {

@@ -45,8 +58,16 @@ return char;

}).join('');
return prefix + code + postfix;
return config.prefix + code + config.postfix;
}
function isFeasible(charset, pattern, count) {
return Math.pow(charset.length, pattern.match(/#/g).length) >= count;
}
function generate(config) {
config = config || {};
var count = config.count || 1;
config = new Config(config);
var count = config.count;
if (!isFeasible(config.charset, config.pattern, config.count)) {
throw new Error("Not possible to generate requested number of codes.");
}
var codes = {};

@@ -53,0 +74,0 @@ while (count > 0) {