diff --git a/README.md b/README.md index 555d06b..9010cd8 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,31 @@ voucher_codes.generate({ Result: `["odghy", "kZEYc", "eOTCl", "wVCzD"]` +#### Prefix and Postfix + +You can optionally surround each generated code with a prefix and/or postfix. + +For instance: +``` +voucher_codes.generate({ + prefix: "promo-", + postfix: "-2015" +}); +``` + +Result: `["promo-WZ4x1t3U-2015"]` + +#### Config reference + +| attribute | default value | description | +|------------------|:--------------:|-------------------------------------------------------------------------| +| `length` | `8` | Number of characters in a generated code (excluding prefix and postfix) | +| `count` | `1` | Number of codes generated. | +| `charset` | `alphanumeric` | Characters that can appear in the code. | +| `prefix` | `""` | A text appended before the code. | +| `postfix` | `""` | A text appended after the code. | + + ### Testing Install dependencies: diff --git a/package.json b/package.json index 95706c7..52844dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "voucher-code-generator", - "version": "0.1.0", + "version": "0.2.0", "homepage": "http://www.voucherify.io/", "description": "Voucher Code Generator", diff --git a/test/voucher_codes.spec.js b/test/voucher_codes.spec.js index 21b9426..5b1fb1a 100644 --- a/test/voucher_codes.spec.js +++ b/test/voucher_codes.spec.js @@ -59,4 +59,29 @@ describe('voucher_codes', function(){ }); }); + it('should generate code with prefix', function(){ + var code = voucher_codes.generate({ + prefix: "promo-" + })[0]; + + expect(code).toMatch(/^promo-/); + }); + + it('should generate code with postfix', function(){ + var code = voucher_codes.generate({ + postfix: "-extra" + })[0]; + + expect(code).toMatch(/-extra$/); + }); + + it('should generate code with prefix', function(){ + var code = voucher_codes.generate({ + prefix: "promo-", + postfix: "-extra" + })[0]; + + expect(code).toMatch(/^promo-.*-extra$/); + }); + }); \ No newline at end of file diff --git a/voucher_codes.js b/voucher_codes.js index 08af16c..bb31202 100644 --- a/voucher_codes.js +++ b/voucher_codes.js @@ -23,11 +23,13 @@ function generateOne(config) { var length = config.length || 8; var chars = config.charset || charset("alphanumeric"); + var prefix = config.prefix || ""; + var postfix = config.postfix || ""; var code = ""; for (var i = 0; i < length; i++) { code += randomElem(chars); } - return code; + return prefix + code + postfix; } function generate(config) {