From 2d5e9c1d88932b221fbacac7f3be219942f7bc06 Mon Sep 17 00:00:00 2001 From: Yusuf Shakeel Date: Tue, 10 Mar 2020 23:07:14 +0530 Subject: [PATCH 1/2] create coupon of length N --- README.md | 13 ++++++++++++- app/constants.js | 4 ++-- app/engine.js | 8 +++++--- app/option.js | 6 ++++++ index.js | 9 ++++++--- tests/app/engine.test.js | 9 +++++++++ tests/index.test.js | 6 ++++++ 7 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 app/option.js diff --git a/README.md b/README.md index 857ae17..6671d94 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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(); ``` @@ -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: diff --git a/app/constants.js b/app/constants.js index 3a84781..14bbeb7 100644 --- a/app/constants.js +++ b/app/constants.js @@ -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' }; \ No newline at end of file diff --git a/app/engine.js b/app/engine.js index 8c234ab..8e7d941 100644 --- a/app/engine.js +++ b/app/engine.js @@ -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(''); @@ -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(''); diff --git a/app/option.js b/app/option.js new file mode 100644 index 0000000..9f0fc5b --- /dev/null +++ b/app/option.js @@ -0,0 +1,6 @@ +const { DEFAULT_LENGTH, ALPHABET_UPPERCASE } = require('./constants.js'); + +module.exports = { + length: DEFAULT_LENGTH, + characters: ALPHABET_UPPERCASE +}; \ No newline at end of file diff --git a/index.js b/index.js index 4dea7e3..9514a19 100644 --- a/index.js +++ b/index.js @@ -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'); /** @@ -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(); }; }; diff --git a/tests/app/engine.test.js b/tests/app/engine.test.js index d9d41fa..26d75c2 100644 --- a/tests/app/engine.test.js +++ b/tests/app/engine.test.js @@ -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'); }); \ No newline at end of file diff --git a/tests/index.test.js b/tests/index.test.js index 869e20b..6186b90 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -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(); +}); From 6b50167a781caeae5049d70b9932a10924e8544a Mon Sep 17 00:00:00 2001 From: Yusuf Shakeel Date: Tue, 10 Mar 2020 23:07:44 +0530 Subject: [PATCH 2/2] 0.2.0 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 85f2c27..47528eb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "couponjs", - "version": "0.1.0", + "version": "0.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 2a2bd6e..1dec177 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "couponjs", - "version": "0.1.0", + "version": "0.2.0", "description": "This is a simple coupon creation project using NodeJS.", "main": "index.js", "scripts": {