Skip to content

Commit

Permalink
Merge pull request #6 from rspective/prefix-postfix
Browse files Browse the repository at this point in the history
Prefix and postfix
  • Loading branch information
tpindel committed Dec 3, 2015
2 parents 16b006a + 23e34c0 commit ae71a8a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
25 changes: 25 additions & 0 deletions test/voucher_codes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/);
});

});
4 changes: 3 additions & 1 deletion voucher_codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ae71a8a

Please sign in to comment.