From 6696ce2aecf54cbd77b6f5b965a47571e197798f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wiktor?= Date: Thu, 3 Dec 2015 11:39:36 +0100 Subject: [PATCH 1/4] added prefix and postfix --- test/voucher_codes.spec.js | 25 +++++++++++++++++++++++++ voucher_codes.js | 4 +++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/test/voucher_codes.spec.js b/test/voucher_codes.spec.js index 21b9426..8baa70e 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.length).toMatch(/^promo-/); + }); + + it('should generate code with postfix', function(){ + var code = voucher_codes.generate({ + postfix: "-extra" + })[0]; + + expect(code.length).toMatch(/-extra$/); + }); + + it('should generate code with prefix', function(){ + var code = voucher_codes.generate({ + prefix: "promo-", + postfix: "-extra" + })[0]; + + expect(code.length).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) { From 319d45cfaf36070033895e0a2fb6a2075ca59e72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wiktor?= Date: Thu, 3 Dec 2015 11:56:37 +0100 Subject: [PATCH 2/4] readme - prefix and postfix, config reference --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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: From 9c7f3d2d5abf2d7de28ddbb177edc8c3a5ece091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wiktor?= Date: Thu, 3 Dec 2015 12:01:00 +0100 Subject: [PATCH 3/4] version 0.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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", From 23e34c03e724f34be31b90b7d3707021a6250209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wiktor?= Date: Thu, 3 Dec 2015 13:37:14 +0100 Subject: [PATCH 4/4] fix tests --- test/voucher_codes.spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/voucher_codes.spec.js b/test/voucher_codes.spec.js index 8baa70e..5b1fb1a 100644 --- a/test/voucher_codes.spec.js +++ b/test/voucher_codes.spec.js @@ -64,7 +64,7 @@ describe('voucher_codes', function(){ prefix: "promo-" })[0]; - expect(code.length).toMatch(/^promo-/); + expect(code).toMatch(/^promo-/); }); it('should generate code with postfix', function(){ @@ -72,7 +72,7 @@ describe('voucher_codes', function(){ postfix: "-extra" })[0]; - expect(code.length).toMatch(/-extra$/); + expect(code).toMatch(/-extra$/); }); it('should generate code with prefix', function(){ @@ -81,7 +81,7 @@ describe('voucher_codes', function(){ postfix: "-extra" })[0]; - expect(code.length).toMatch(/^promo-.*-extra$/); + expect(code).toMatch(/^promo-.*-extra$/); }); }); \ No newline at end of file