Skip to content

Commit

Permalink
create coupon of length N
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufshakeel committed Mar 10, 2020
1 parent 6604d83 commit 2d5e9c1
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 9 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
This is a simple coupon creation project using NodeJS.

[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/yusufshakeel/couponjs)
[![npm version](https://img.shields.io/badge/npm-0.1.0-blue.svg)](https://www.npmjs.com/package/couponjs)
[![npm version](https://img.shields.io/badge/npm-0.2.0-blue.svg)](https://www.npmjs.com/package/couponjs)
[![Build Status](https://travis-ci.com/yusufshakeel/couponjs.svg?branch=master)](https://travis-ci.com/yusufshakeel/couponjs)

# Getting Started
Expand All @@ -20,6 +20,8 @@ Using the following for testing:
## Generate coupon
Create an object of Coupon.
```javascript
const Coupon = require('couponjs');

const coupon = new Coupon();
```

Expand All @@ -30,6 +32,15 @@ const myCoupon = coupon.generate();

By default, `generate` will return coupon code of length 6 using uppercase alphabet.

### Coupon of length N
To generate coupon of a given length we pass the following option to the `generate` method.
```javascript
const myCoupon = coupon.generate({
length: 8
});
```
Where, 8 in the above code represent the total number of characters that will be present in the coupon.


## License
It's free :smiley:
Expand Down
4 changes: 2 additions & 2 deletions app/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Constants
* @type {{ALPHABET_UPPERCASE: string, ALPHABET_LOWERCASE: string, DIGIT: string}}
* @type {{DEFAULT_LENGTH: number, ALPHABET_UPPERCASE: string}}
*/
module.exports = {
DEFAULT_LENGTH: 6,
ALPHABET_UPPERCASE: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
};
8 changes: 5 additions & 3 deletions app/engine.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const { DEFAULT_LENGTH } = require('./constants.js');
/**
* Engine to produce coupon.
* @param {string} characters This is the set of characters used to generate coupon.
* @param {function} randomInteger This is the function that will generate random integer value.
* @param {number} length This is the length of the coupon.
* @constructor
*/
const Engine = function (characters, randomInteger) {
const Engine = function (characters, randomInteger, length = DEFAULT_LENGTH) {

function characterSet() {
return characters.split('');
Expand All @@ -13,9 +15,9 @@ const Engine = function (characters, randomInteger) {
function generateCoupon() {
const generatedCouponCharacters = [];
const charSet = characterSet();
for(let i = 0; i < 6; i++) {
for(let i = 0; i < length; i++) {
generatedCouponCharacters.push(
charSet[randomInteger(0, 6)]
charSet[randomInteger(0, length - 1)]
);
}
return generatedCouponCharacters.join('');
Expand Down
6 changes: 6 additions & 0 deletions app/option.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { DEFAULT_LENGTH, ALPHABET_UPPERCASE } = require('./constants.js');

module.exports = {
length: DEFAULT_LENGTH,
characters: ALPHABET_UPPERCASE
};
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Engine = require('./app/engine.js');
const { ALPHABET_UPPERCASE } = require('./app/constants.js');
const defaultOptions = require('./app/option.js');
const randomInteger = require('./app/random-integer.js');

/**
Expand All @@ -9,10 +9,13 @@ const randomInteger = require('./app/random-integer.js');
const Coupon = function () {
/**
* This will generate coupons.
*
* @param {object} option This is the configuration option.
* @returns {string}
*/
this.generate = function () {
const engine = new Engine(ALPHABET_UPPERCASE, randomInteger);
this.generate = function (option) {
const { length, characters } = Object.assign(defaultOptions, option);
const engine = new Engine(characters, randomInteger, length);
return engine.run();
};
};
Expand Down
9 changes: 9 additions & 0 deletions tests/app/engine.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ test('Should return zzzzzz as coupon when character set is "z" and randomInteger
});
const engine = new Engine(characters, mockRandomInteger);
expect(engine.run()).toBe('zzzzzz');
});

test('Should return aaa as coupon when character set is "a" and rendomInteger generates always 0 and length is 3', () => {
const characters = "a";
const mockRandomInteger = jest.fn((min, max) => {
return 0;
});
const engine = new Engine(characters, mockRandomInteger, 3);
expect(engine.run()).toBe('aaa');
});
6 changes: 6 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ test('Should generate coupon code using uppercase alphabet A-Z of length 6.', ()
const result = coupon.generate();
expect(/^[A-Z]{6}$/.test(result)).toBeTruthy();
});

test('Should generate coupon code using uppercase alphabet A-Z of length 8.', () => {
const coupon = new Coupon();
const result = coupon.generate({ length: 8 });
expect(/^[A-Z]{8}$/.test(result)).toBeTruthy();
});

0 comments on commit 2d5e9c1

Please sign in to comment.