From 57a295d4b8dea0422f7748c3b8cbe0baf4b65f84 Mon Sep 17 00:00:00 2001 From: Yusuf Shakeel Date: Sat, 14 Mar 2020 14:10:22 +0530 Subject: [PATCH 1/9] character set builder + test --- app/character-set-builder.js | 41 +++++++++++++++ tests/app/character-set-builder.test.js | 66 +++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 app/character-set-builder.js create mode 100644 tests/app/character-set-builder.test.js diff --git a/app/character-set-builder.js b/app/character-set-builder.js new file mode 100644 index 0000000..4cfd65f --- /dev/null +++ b/app/character-set-builder.js @@ -0,0 +1,41 @@ +const { CHARSET_ALPHA, CHARSET_ALPHA_LOWER, CHARSET_DIGIT, ALPHABET_UPPERCASE, ALPHABET_LOWERCASE, DIGIT} = require('./constants.js'); + +/** + * This will return the characters based on the character set name. + * @param {string} charSet This is the name of the character set. + * @returns {string} String of characters. + */ +function characters (charSet) { + switch(charSet) { + case CHARSET_ALPHA: return ALPHABET_UPPERCASE; + case CHARSET_ALPHA_LOWER: return ALPHABET_LOWERCASE; + case CHARSET_DIGIT: return DIGIT; + default: throw new Error(`Invalid builtIn characterSet specified. Allowed values ["${CHARSET_ALPHA}", "${CHARSET_ALPHA_LOWER}", "${CHARSET_DIGIT}"]`); + } +} + +/** + * This will generate a string of unique characters based on the options provided. + * @param {object} characterSetOptions The options to build the character set. + * @returns {string} The set of characters based on the options provided. + */ +function characterSetBuilder(characterSetOptions) { + + const { builtIn = [], custom = [] } = characterSetOptions; + + const builtInCharacters = builtIn.reduce((chars, charSet) => { + return `${chars}${characters(charSet)}` + }, ''); + + const customCharacters = custom.reduce((chars, charSet) => { + return `${chars}${charSet}` + }, ''); + + const uniqueCharacters = `${builtInCharacters}${customCharacters}`.split('').reduce((characters, character) => { + return characters + (characters.indexOf(character) === -1 ? character : ''); + }, ''); + + return `${uniqueCharacters}`; +} + +module.exports = characterSetBuilder; diff --git a/tests/app/character-set-builder.test.js b/tests/app/character-set-builder.test.js new file mode 100644 index 0000000..a3f022a --- /dev/null +++ b/tests/app/character-set-builder.test.js @@ -0,0 +1,66 @@ +const {ALPHABET_UPPERCASE, ALPHABET_LOWERCASE, DIGIT, CHARSET_DIGIT, CHARSET_ALPHA_LOWER, CHARSET_ALPHA} = require('../../app/constants.js'); +const characterSetBuilder = require('../../app/character-set-builder.js'); +const defaultOptions = require('../../app/option.js'); + +test('Should throw error if invalid builtIn option provided', () => { + expect(() => { + const option = { + builtIn: ['UNKNOWN'] + }; + const chars = characterSetBuilder(option); + throw new Error('Should have failed.'); + }).toThrow('Invalid builtIn characterSet specified. Allowed values ["CHARSET_ALPHA", "CHARSET_ALPHA_LOWER", "CHARSET_DIGIT"]'); +}); + +test('Should return uppercase alphabet A-Z when using default options', () => { + expect(characterSetBuilder(defaultOptions.characterSet)).toBe(ALPHABET_UPPERCASE); +}); + +test('Should return uppercase alphabet A-Z when using builtIn "CHARSET_ALPHA" option', () => { + expect(characterSetBuilder({builtIn: [CHARSET_ALPHA]})).toBe(ALPHABET_UPPERCASE); +}); + +test('Should return lowercase alphabet a-z when using builtIn "CHARSET_ALPHA_LOWER" option', () => { + expect(characterSetBuilder({builtIn: [CHARSET_ALPHA_LOWER]})).toBe(ALPHABET_LOWERCASE); +}); + +test('Should return digits 0-9 when using builtIn "CHARSET_DIGIT" option', () => { + expect(characterSetBuilder({builtIn: [CHARSET_DIGIT]})).toBe(DIGIT); +}); + +test('Should return uppercase, lowercase alphabet and digits when using builtIn ["CHARSET_ALPHA", "CHARSET_ALPHA_LOWER", "CHARSET_DIGIT"] option', () => { + expect(characterSetBuilder({builtIn: [CHARSET_ALPHA, CHARSET_ALPHA_LOWER, CHARSET_DIGIT]})).toBe(`${ALPHABET_UPPERCASE}${ALPHABET_LOWERCASE}${DIGIT}`); +}); + +test('Should return alphabet ABC when using custom "ABC" option', () => { + expect(characterSetBuilder({custom: ['ABC']})).toBe('ABC'); +}); + +test('Should return alphabet abc when using custom "abc" option', () => { + expect(characterSetBuilder({custom: ['abc']})).toBe('abc'); +}); + +test('Should return digits 123 when using custom "123" option', () => { + expect(characterSetBuilder({custom: ['123']})).toBe('123'); +}); + +test('Should return alphabet uppercase, lowercase and digit "ABCabc123" when using custom ["ABC", "abc", "123"] option', () => { + expect(characterSetBuilder({custom: ['ABC', 'abc', '123']})).toBe('ABCabc123'); +}); + +test('Should return both builtIn and custom characters when using custom both options', () => { + const option = { + builtIn: [CHARSET_ALPHA, CHARSET_ALPHA_LOWER, CHARSET_DIGIT], + custom: ['@$%'] + }; + const expectedResult = `${ALPHABET_UPPERCASE}${ALPHABET_LOWERCASE}${DIGIT}@$%`; + expect(characterSetBuilder(option)).toBe(expectedResult); +}); + +test('Should return unique characters when option has duplicate characters', () => { + const option = { + builtIn: [CHARSET_ALPHA, CHARSET_ALPHA_LOWER, CHARSET_DIGIT], + custom: ['ABC', 'abc', 'BCD', 'xyz', '123'] + }; + expect(characterSetBuilder(option)).toBe(`${ALPHABET_UPPERCASE}${ALPHABET_LOWERCASE}${DIGIT}`); +}); From 3a7a8136ab409928d752df69701df544bbbebe00 Mon Sep 17 00:00:00 2001 From: Yusuf Shakeel Date: Sat, 14 Mar 2020 14:10:34 +0530 Subject: [PATCH 2/9] contants --- app/constants.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/constants.js b/app/constants.js index 767cfdf..e1b23ce 100644 --- a/app/constants.js +++ b/app/constants.js @@ -1,5 +1,5 @@ /** - * @type {{DEFAULT_LENGTH: number, DEFAULT_PREFIX: string, DEFAULT_SUFFIX: string, ALPHABET_UPPERCASE: string}} + * @type {{MAX_LENGTH: number, MIN_LENGTH: number, DEFAULT_LENGTH: number, DEFAULT_PREFIX: string, DEFAULT_SUFFIX: string, ALPHABET_UPPERCASE: string, ALPHABET_LOWERCASE: string, DIGIT: string, CHARSET_ALPHA: string, CHARSET_ALPHA_LOWER: string, CHARSET_DIGIT: string}} */ module.exports = { MAX_LENGTH: 128, @@ -7,5 +7,10 @@ module.exports = { DEFAULT_LENGTH: 6, DEFAULT_PREFIX: '', DEFAULT_SUFFIX: '', - ALPHABET_UPPERCASE: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + ALPHABET_UPPERCASE: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', + ALPHABET_LOWERCASE: 'abcdefghijklmnopqrstuvwxyz', + DIGIT: '0123456789', + CHARSET_ALPHA: 'CHARSET_ALPHA', + CHARSET_ALPHA_LOWER: 'CHARSET_ALPHA_LOWER', + CHARSET_DIGIT: 'CHARSET_DIGIT' }; \ No newline at end of file From 69199187fe71da0e3b9be7bbf2356202158788b5 Mon Sep 17 00:00:00 2001 From: Yusuf Shakeel Date: Sat, 14 Mar 2020 14:10:53 +0530 Subject: [PATCH 3/9] default option updated --- app/option.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/option.js b/app/option.js index 3c1ef88..0f7bf6e 100644 --- a/app/option.js +++ b/app/option.js @@ -1,8 +1,10 @@ -const {DEFAULT_LENGTH, DEFAULT_PREFIX, DEFAULT_SUFFIX, ALPHABET_UPPERCASE} = require('./constants.js'); +const {DEFAULT_LENGTH, DEFAULT_PREFIX, DEFAULT_SUFFIX, ALPHABET_UPPERCASE, CHARSET_ALPHA} = require('./constants.js'); module.exports = { length: DEFAULT_LENGTH, prefix: DEFAULT_PREFIX, suffix: DEFAULT_SUFFIX, - characters: ALPHABET_UPPERCASE + characterSet: { + builtIn: [CHARSET_ALPHA] + } }; \ No newline at end of file From abd8a09020c34ff382e8c922f533259a6d6fc744 Mon Sep 17 00:00:00 2001 From: Yusuf Shakeel Date: Sat, 14 Mar 2020 14:11:08 +0530 Subject: [PATCH 4/9] engine updated to handle characterSet --- app/engine.js | 19 +++++++---------- tests/app/engine.test.js | 45 ++++++++++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/app/engine.js b/app/engine.js index 21404c2..a801530 100644 --- a/app/engine.js +++ b/app/engine.js @@ -1,14 +1,16 @@ const {MAX_LENGTH, MIN_LENGTH, DEFAULT_LENGTH, DEFAULT_PREFIX, DEFAULT_SUFFIX} = require('./constants.js'); +const characterSetBuilder = require('./character-set-builder.js'); + /** * Engine to produce coupon. - * @param {string} characters This is the set of characters used to generate coupon. + * @param {object} characterSetOption This is the set of character set options 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 excluding prefix and suffix characters if any. * @param {string} prefix This is the set of characters that is added at the start of the coupon. * @param {string} suffix This is the set of characters that is added at the end of the coupon. * @constructor */ -const Engine = function (characters, randomInteger, length = DEFAULT_LENGTH, prefix = DEFAULT_PREFIX, suffix = DEFAULT_SUFFIX) { +const Engine = function (characterSetOption, randomInteger, length = DEFAULT_LENGTH, prefix = DEFAULT_PREFIX, suffix = DEFAULT_SUFFIX) { /** * This will validate options @@ -19,24 +21,17 @@ const Engine = function (characters, randomInteger, length = DEFAULT_LENGTH, pre if (length >= MAX_LENGTH) throw new Error(`Maximum value for "length" is ${MAX_LENGTH}.`); }; - /** - * This will return array of characters. - * @returns {string[]} - */ - function characterSet() { - return characters.split(''); - } - /** * This will generate the coupon. * @returns {string} */ function generateCoupon() { + const characters = characterSetBuilder(characterSetOption).split(''); + const charactersLength = characters.length; const generatedCouponCharacters = []; - const charSet = characterSet(); for (let i = 0; i < length; i++) { generatedCouponCharacters.push( - charSet[randomInteger(0, length - 1)] + characters[randomInteger(0, charactersLength - 1)] ); } return generatedCouponCharacters.join(''); diff --git a/tests/app/engine.test.js b/tests/app/engine.test.js index f6daa48..3911bc7 100644 --- a/tests/app/engine.test.js +++ b/tests/app/engine.test.js @@ -1,12 +1,13 @@ const Engine = require('../../app/engine.js'); +const defaultOption = require('../../app/option.js'); test('Should throw error if length is less than 1', () => { expect(() => { - const characters = 'A'; + const characterSet = defaultOption.characterSet; const mockRandomInteger = jest.fn((min, max) => { return 0; }); - const engine = new Engine(characters, mockRandomInteger, 0); + const engine = new Engine(characterSet, mockRandomInteger, 0); engine.run(); throw new Error('Should have failed.'); }).toThrow('Minimum value for "length" is 1.'); @@ -14,66 +15,78 @@ test('Should throw error if length is less than 1', () => { test('Should throw error if length is greater than 128', () => { expect(() => { - const characters = 'A'; + const characterSet = defaultOption.characterSet; const mockRandomInteger = jest.fn((min, max) => { return 0; }); - const engine = new Engine(characters, mockRandomInteger, 200); + const engine = new Engine(characterSet, mockRandomInteger, 200); engine.run(); throw new Error('Should have failed.'); }).toThrow('Maximum value for "length" is 128.'); }); test('Should return AAAAAA as coupon when character set is "A" and randomInteger generates always 0', () => { - const characters = 'A'; + const characterSet = { + custom: ['A'] + }; const mockRandomInteger = jest.fn((min, max) => { return 0; }); - const engine = new Engine(characters, mockRandomInteger); + const engine = new Engine(characterSet, mockRandomInteger); expect(engine.run()).toBe('AAAAAA'); }); test('Should return zzzzzz as coupon when character set is "z" and randomInteger generates always 0', () => { - const characters = 'z'; + const characterSet = { + custom: ['z'] + }; const mockRandomInteger = jest.fn((min, max) => { return 0; }); - const engine = new Engine(characters, mockRandomInteger); + const engine = new Engine(characterSet, 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 characterSet = { + custom: ['a'] + }; const mockRandomInteger = jest.fn((min, max) => { return 0; }); - const engine = new Engine(characters, mockRandomInteger, 3); + const engine = new Engine(characterSet, mockRandomInteger, 3); expect(engine.run()).toBe('aaa'); }); test('Should return PREFIXaaa as coupon when character set is "a" and rendomInteger generates always 0 and length is 3', () => { - const characters = 'a'; + const characterSet = { + custom: ['a'] + }; const mockRandomInteger = jest.fn((min, max) => { return 0; }); - const engine = new Engine(characters, mockRandomInteger, 3, 'PREFIX'); + const engine = new Engine(characterSet, mockRandomInteger, 3, 'PREFIX'); expect(engine.run()).toBe('PREFIXaaa'); }); test('Should return aaaSUFFIX as coupon when character set is "a" and rendomInteger generates always 0 and length is 3', () => { - const characters = 'a'; + const characterSet = { + custom: ['a'] + }; const mockRandomInteger = jest.fn((min, max) => { return 0; }); - const engine = new Engine(characters, mockRandomInteger, 3, '', 'SUFFIX'); + const engine = new Engine(characterSet, mockRandomInteger, 3, '', 'SUFFIX'); expect(engine.run()).toBe('aaaSUFFIX'); }); test('Should return PREFIXaaaSUFFIX as coupon when character set is "a" and rendomInteger generates always 0 and length is 3', () => { - const characters = 'a'; + const characterSet = { + custom: ['a'] + }; const mockRandomInteger = jest.fn((min, max) => { return 0; }); - const engine = new Engine(characters, mockRandomInteger, 3, 'PREFIX', 'SUFFIX'); + const engine = new Engine(characterSet, mockRandomInteger, 3, 'PREFIX', 'SUFFIX'); expect(engine.run()).toBe('PREFIXaaaSUFFIX'); }); \ No newline at end of file From c82d3da239b80f191105acce76b5fe6bd916a2f0 Mon Sep 17 00:00:00 2001 From: Yusuf Shakeel Date: Sat, 14 Mar 2020 14:11:24 +0530 Subject: [PATCH 5/9] test update --- index.js | 5 ++-- tests/index.test.js | 70 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 676ef27..97f6fdb 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,6 @@ const randomInteger = require('./app/random-integer.js'); * @constructor */ const Coupon = function () { - /** * This will generate coupons. * @@ -15,8 +14,8 @@ const Coupon = function () { * @returns {string} */ this.generate = function (option) { - const {length, characters, prefix, suffix} = Object.assign({}, defaultOptions, option); - const engine = new Engine(characters, randomInteger, length, prefix, suffix); + const {length, characterSet, prefix, suffix} = Object.assign({}, defaultOptions, option); + const engine = new Engine(characterSet, randomInteger, length, prefix, suffix); return engine.run(); }; }; diff --git a/tests/index.test.js b/tests/index.test.js index 36171a6..5fbc146 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -69,3 +69,73 @@ test('Should generate coupon code using uppercase alphabet A-Z of length 6,7,8,9 expect(/^[A-Z]{9}AWESOME$/.test(coupon.generate({length: 9, suffix: 'AWESOME'}))).toBeTruthy(); expect(/^[A-Z]{10}AWESOME$/.test(coupon.generate({length: 10, suffix: 'AWESOME'}))).toBeTruthy(); }); + +test('Should generate coupon code using characterSet builtIn uppercase alphabet option', () => { + const coupon = new Coupon(); + const myCoupon = coupon.generate({ + characterSet: { + builtIn: ['CHARSET_ALPHA'] + } + }); + expect(/^[A-Z]{6}$/.test(myCoupon)).toBeTruthy(); +}); + +test('Should generate coupon code using characterSet builtIn lowercase alphabet option', () => { + const coupon = new Coupon(); + const myCoupon = coupon.generate({ + characterSet: { + builtIn: ['CHARSET_ALPHA_LOWER'] + } + }); + expect(/^[a-z]{6}$/.test(myCoupon)).toBeTruthy(); +}); + +test('Should generate coupon code using characterSet builtIn digit option', () => { + const coupon = new Coupon(); + const myCoupon = coupon.generate({ + characterSet: { + builtIn: ['CHARSET_DIGIT'] + } + }); + expect(/^[0-9]{6}$/.test(myCoupon)).toBeTruthy(); +}); + +test('Should generate coupon code using characterSet builtIn alphabet uppercase, lowercase and digit option', () => { + const coupon = new Coupon(); + const myCoupon = coupon.generate({ + characterSet: { + builtIn: ['CHARSET_ALPHA', 'CHARSET_ALPHA_LOWER', 'CHARSET_DIGIT'] + } + }); + expect(/^[A-Za-z0-9]{6}$/.test(myCoupon)).toBeTruthy(); +}); + +test('Should generate coupon code using characterSet custom uppercase alphabet option', () => { + const coupon = new Coupon(); + const myCoupon = coupon.generate({ + characterSet: { + custom: ['ABC'] + } + }); + expect(/^[A-C]{6}$/.test(myCoupon)).toBeTruthy(); +}); + +test('Should generate coupon code using characterSet custom lowercase alphabet option', () => { + const coupon = new Coupon(); + const myCoupon = coupon.generate({ + characterSet: { + custom: ['xyz'] + } + }); + expect(/^[x-z]{6}$/.test(myCoupon)).toBeTruthy(); +}); + +test('Should generate coupon code using characterSet custom digit option', () => { + const coupon = new Coupon(); + const myCoupon = coupon.generate({ + characterSet: { + custom: ['123'] + } + }); + expect(/^[1-3]{6}$/.test(myCoupon)).toBeTruthy(); +}); From 8f96515984b7a12018bf5258a8fc45e7a2aa7d61 Mon Sep 17 00:00:00 2001 From: Yusuf Shakeel Date: Sat, 14 Mar 2020 14:15:49 +0530 Subject: [PATCH 6/9] update --- app/engine.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/engine.js b/app/engine.js index a801530..897ea2e 100644 --- a/app/engine.js +++ b/app/engine.js @@ -34,7 +34,7 @@ const Engine = function (characterSetOption, randomInteger, length = DEFAULT_LEN characters[randomInteger(0, charactersLength - 1)] ); } - return generatedCouponCharacters.join(''); + return `${prefix}${generatedCouponCharacters.join('')}${suffix}`; } /** @@ -43,7 +43,7 @@ const Engine = function (characterSetOption, randomInteger, length = DEFAULT_LEN */ this.run = function () { validate({length}); - return `${prefix}${generateCoupon()}${suffix}`; + return generateCoupon(); }; }; From a12a3630bef705bbeefef9bcef01183cd14bb1f1 Mon Sep 17 00:00:00 2001 From: Yusuf Shakeel Date: Sat, 14 Mar 2020 14:23:59 +0530 Subject: [PATCH 7/9] tests --- tests/index.test.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/index.test.js b/tests/index.test.js index 5fbc146..3edc665 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -139,3 +139,38 @@ test('Should generate coupon code using characterSet custom digit option', () => }); expect(/^[1-3]{6}$/.test(myCoupon)).toBeTruthy(); }); + +test('Should generate coupon code using characterSet custom uppercase, lowercase alphabet, digit and symbol option', () => { + const coupon = new Coupon(); + const myCoupon = coupon.generate({ + characterSet: { + custom: ['ABC', 'xyz', '123', '@$&'] + } + }); + expect(/^[A-Cx-z1-3@$&]{6}$/.test(myCoupon)).toBeTruthy(); +}); + +test('Should generate coupon code using characterSet builtIn and custom option', () => { + const coupon = new Coupon(); + const myCoupon = coupon.generate({ + characterSet: { + builtIn: ['CHARSET_ALPHA', 'CHARSET_ALPHA_LOWER', "CHARSET_DIGIT"], + custom: ['ABC', 'xyz', '123', '@$&'] + } + }); + expect(/^[A-Za-z0-9@$&]{6}$/.test(myCoupon)).toBeTruthy(); +}); + +test('Should generate coupon code of length 8 with prefix and suffix and using characterSet builtIn and custom option', () => { + const coupon = new Coupon(); + const myCoupon = coupon.generate({ + length: 8, + prefix: 'SUPER', + suffix: 'AWESOME', + characterSet: { + builtIn: ['CHARSET_ALPHA', 'CHARSET_ALPHA_LOWER', "CHARSET_DIGIT"], + custom: ['ABC', 'xyz', '123', '@$&'] + } + }); + expect(/^SUPER[A-Za-z0-9@$&]{8}AWESOME$/.test(myCoupon)).toBeTruthy(); +}); From 20969550da49681fe8cb34b2a13a89f570fae9d8 Mon Sep 17 00:00:00 2001 From: Yusuf Shakeel Date: Sat, 14 Mar 2020 14:57:00 +0530 Subject: [PATCH 8/9] readme updated --- README.md | 73 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index c8c8971..675f038 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.3.0-blue.svg)](https://www.npmjs.com/package/couponjs) +[![npm version](https://img.shields.io/badge/npm-0.4.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 @@ -11,12 +11,6 @@ Add this to your project using npm. > npm i couponjs ``` -## Tests -Test code of this project is inside the `tests` directory. - -Using the following for testing: -* Jest - ## Generate coupon Create an object of Coupon. ```javascript @@ -32,7 +26,7 @@ const myCoupon = coupon.generate(); By default, `generate` will return coupon code of length 6 using uppercase alphabet. -### Coupon of length N +## 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({ @@ -47,7 +41,7 @@ Range of `length`: If length is not passed then default value of 6 is considered. -### Coupon with prefix +## Coupon with prefix To generate a coupon with a given prefix we pass the following option to the `generate` method. ```javascript const myCoupon = coupon.generate({ @@ -69,7 +63,7 @@ const myCoupon = coupon.generate({ ``` We will get coupon like the following `SUPERAAA`. -### Coupon with suffix +## Coupon with suffix To create coupon with suffix pass the following option. ```javascript const myCoupon = coupon.generate({ @@ -79,9 +73,9 @@ const myCoupon = coupon.generate({ ``` The above code will generate coupon like the following `ZZZAWESOME`. -Note! Characters of the suffix is not counted. If length is note specified then default value of 6 is considered as the length. +Note! Characters of the suffix is not counted. If length is not specified then default value of 6 is considered as the length. -### Coupon with prefix and suffix +## Coupon with prefix and suffix To create coupon with prefix and suffix pass the following option. ```javascript const myCoupon = coupon.generate({ @@ -94,6 +88,61 @@ The above code will generate coupon like the following `SUPERZZZZZZAWESOME`. Note! The characters of the prefix and suffix is not considered. If length is not specified then default value of 6 is considered. +## Coupon with builtIn characterSet +To create coupon code with builtIn characterSet pass the following option. +```javascript +const myCoupon = coupon.generate({ + characterSet: { + builtIn: ['charSetName'] + } +}); +``` +Where, `charSetName` is any one of the following names. + +- `CHARSET_ALPHA` -- which consists of uppercase alphabet characters A-Z +- `CHARSET_ALPHA_LOWER` -- which consists of lowercase alphabet characters a-z +- `CHARSET_DIGIT` -- which consists of digits 0-9 + +Example: If we want uppercase and digit we can pass the following. +```javascript +const myCoupon = coupon.generate({ + characterSet: { + builtIn: ['CHARSET_ALPHA', 'CHARSET_DIGIT'] + } +}); +``` + +## Coupon with custom characterSet +To use custom characters to generate coupons pass the following option. +```javascript +const myCoupon = coupon.generate({ + characterSet: { + custom: ['customChar'] + } +}); +``` +Where, `customChar` is any custom characters that you wish to use. + +Example: To use `ABC`, `xyz` and `01234` in coupon pass the following. +```javascript +const myCoupon = coupon.generate({ + characterSet: { + custom: ['ABC', 'xyz', '01234'] + } +}); +``` + +## Coupons using builtIn and custom characterSet +Example: Following option will use digit `0-9` and alphabet `ABC`. +```javascript +const myCoupon = coupon.generate({ + characterSet: { + builtIn: ['CHARSET_DIGIT'], + custom: ['ABC'] + } +}); +``` + ## License It's free :smiley: From ba153fdc4b714aee4f04262fa88d03511082d372 Mon Sep 17 00:00:00 2001 From: Yusuf Shakeel Date: Sat, 14 Mar 2020 14:58:03 +0530 Subject: [PATCH 9/9] 0.4.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 94a56cd..570ddda 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "couponjs", - "version": "0.3.0", + "version": "0.4.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 34a62b6..8c93885 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "couponjs", - "version": "0.3.0", + "version": "0.4.0", "description": "This is a simple coupon creation project using NodeJS.", "main": "index.js", "scripts": {