Skip to content

Commit

Permalink
Merge pull request #14 from yusufshakeel/dev
Browse files Browse the repository at this point in the history
v0.8.1
  • Loading branch information
yusufshakeel committed Sep 28, 2020
2 parents da4bb0c + 55fc1a2 commit 3ac7b52
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 16 deletions.
25 changes: 24 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.7.3-blue.svg)](https://www.npmjs.com/package/couponjs)
[![npm version](https://img.shields.io/badge/npm-0.8.1-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)
[![Coverage Status](https://coveralls.io/repos/github/yusufshakeel/couponjs/badge.svg?branch=master)](https://coveralls.io/github/yusufshakeel/couponjs?branch=master)

Expand Down Expand Up @@ -186,6 +186,29 @@ Sample output:
['95TMY9JV', 'RZU6ZL0K', '1Q19N019']
```

## Omit characters

To omit characters from the generated coupon code we pass the following option.

```javascript
const myCoupons = coupon.generate({
omitCharacters: ['charToOmit']
});
```

Where, `omitCharacters` is the field that will help in omitting the characters.

`charToOmit` is a string of characters to omit from the generated coupon codes.

Example:

```javascript
const myCoupons = coupon.generate({
omitCharacters: ['ABC', 'XYZ']
});
```

The above code will generate coupons that will omit characters `A`, `B`, `C`, `X`, `Y` and `Z`.

## License
It's free :smiley:
Expand Down
9 changes: 7 additions & 2 deletions app/character-set-builder.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
const {DEFAULT_OMIT_CHARACTERS} = require('./constants.js');
const characterSet = require('./character-set.js');

/**
* This will generate a string of unique characters based on the options provided.
* @param {object} characterSetOptions The options to build the character set.
* @param {string[]} omitCharacters The array of characters that will be omitted.
* @returns {string} The set of characters based on the options provided.
*/
function characterSetBuilder(characterSetOptions) {
function characterSetBuilder(characterSetOptions, omitCharacters = DEFAULT_OMIT_CHARACTERS) {
const {builtIn = [], custom = []} = characterSetOptions;
return [...new Set([...builtIn.map(charSetName => characterSet(charSetName)), ...custom].join(''))].join('');
const charactersToOmit = [...new Set(omitCharacters.join(''))];
const charactersFromCharacterSetOptions = [...new Set([...builtIn.map(characterSetName => characterSet(characterSetName)), ...custom].join(''))];
const charactersToUse = charactersFromCharacterSetOptions.filter(character => !charactersToOmit.includes(character));
return charactersToUse.join('');
}

module.exports = characterSetBuilder;
6 changes: 4 additions & 2 deletions app/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ const CHARSET_BINARY = 'CHARSET_BINARY';
const CHARSET_OCTAL = 'CHARSET_OCTAL';
const CHARSET_HEX = 'CHARSET_HEX';
const CHARSET_HEX_LOWER = 'CHARSET_HEX_LOWER';
const DEFAULT_OMIT_CHARACTERS = [];

/**
* @type {{MIN_NUMBER_OF_COUPONS_TO_GENERATE: number, MAX_NUMBER_OF_COUPONS_TO_GENERATE: number, DEFAULT_NUMBER_OF_COUPONS_TO_GENERATE: number, MAX_LENGTH: number, MIN_LENGTH: number, DEFAULT_LENGTH: number, DEFAULT_PREFIX: string, DEFAULT_SUFFIX: string, ALPHABET_UPPERCASE: string, ALPHABET_LOWERCASE: string, DIGIT: string, BINARY: string, OCTAL: string, HEX: string, HEX_LOWER: string, CHARSET_ALPHA: string, CHARSET_ALPHA_LOWER: string, CHARSET_DIGIT: string, CHARSET_ALNUM: string, CHARSET_BINARY: string, CHARSET_OCTAL: string, CHARSET_HEX: string, CHARSET_HEX_LOWER: string}}
* @type {{DEFAULT_LENGTH: number, CHARSET_OCTAL: string, CHARSET_HEX_LOWER: string, CHARSET_ALPHA_LOWER: string, BINARY: string, CHARSET_BINARY: string, DIGIT: string, MAX_LENGTH: number, ALPHABET_LOWERCASE: string, CHARSET_ALNUM: string, MAX_NUMBER_OF_COUPONS_TO_GENERATE: number, CHARSET_ALPHA: string, OCTAL: string, MIN_LENGTH: number, ALPHABET_UPPERCASE: string, HEX_LOWER: string, CHARSET_DIGIT: string, DEFAULT_SUFFIX: string, MIN_NUMBER_OF_COUPONS_TO_GENERATE: number, HEX: string, CHARSET_HEX: string, DEFAULT_OMIT_CHARACTERS: [], DEFAULT_PREFIX: string, DEFAULT_NUMBER_OF_COUPONS_TO_GENERATE: number}}
*/
module.exports = {
MIN_NUMBER_OF_COUPONS_TO_GENERATE,
Expand All @@ -48,5 +49,6 @@ module.exports = {
CHARSET_BINARY,
CHARSET_OCTAL,
CHARSET_HEX,
CHARSET_HEX_LOWER
CHARSET_HEX_LOWER,
DEFAULT_OMIT_CHARACTERS
};
10 changes: 6 additions & 4 deletions app/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const {
DEFAULT_SUFFIX,
MIN_NUMBER_OF_COUPONS_TO_GENERATE,
MAX_NUMBER_OF_COUPONS_TO_GENERATE,
DEFAULT_NUMBER_OF_COUPONS_TO_GENERATE
DEFAULT_NUMBER_OF_COUPONS_TO_GENERATE,
DEFAULT_OMIT_CHARACTERS
} = require('./constants.js');
const characterSetBuilder = require('./character-set-builder.js');

Expand All @@ -18,11 +19,12 @@ const characterSetBuilder = require('./character-set-builder.js');
* @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.
* @param {number} numberOfCoupons Total number of coupons to generate.
* @param {string[]} omitCharacters This is the array of characters that will be ignored.
* @constructor
*/
const Engine = function (characterSetOption, randomInteger, length = DEFAULT_LENGTH, prefix = DEFAULT_PREFIX, suffix = DEFAULT_SUFFIX, numberOfCoupons = DEFAULT_NUMBER_OF_COUPONS_TO_GENERATE) {
const Engine = function (characterSetOption, randomInteger, length = DEFAULT_LENGTH, prefix = DEFAULT_PREFIX, suffix = DEFAULT_SUFFIX, numberOfCoupons = DEFAULT_NUMBER_OF_COUPONS_TO_GENERATE, omitCharacters = DEFAULT_OMIT_CHARACTERS) {

const characters = characterSetBuilder(characterSetOption).split('');
const characters = characterSetBuilder(characterSetOption, omitCharacters).split('');
const charactersLength = characters.length;
const totalNumberOfPossibleCoupons = Math.pow(charactersLength, length);

Expand Down Expand Up @@ -84,4 +86,4 @@ const Engine = function (characterSetOption, randomInteger, length = DEFAULT_LEN
};
};

module.exports = Engine;
module.exports = Engine;
6 changes: 4 additions & 2 deletions app/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const {
DEFAULT_PREFIX,
DEFAULT_SUFFIX,
CHARSET_ALPHA,
DEFAULT_NUMBER_OF_COUPONS_TO_GENERATE
DEFAULT_NUMBER_OF_COUPONS_TO_GENERATE,
DEFAULT_OMIT_CHARACTERS
} = require('./constants.js');

module.exports = {
Expand All @@ -13,5 +14,6 @@ module.exports = {
characterSet: {
builtIn: [CHARSET_ALPHA]
},
numberOfCoupons: DEFAULT_NUMBER_OF_COUPONS_TO_GENERATE
numberOfCoupons: DEFAULT_NUMBER_OF_COUPONS_TO_GENERATE,
omitCharacters: DEFAULT_OMIT_CHARACTERS
};
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const Coupon = function () {
* @returns {string}
*/
this.generate = function (option) {
const {numberOfCoupons, length, characterSet, prefix, suffix} = Object.assign({}, defaultOptions, option);
const engine = new Engine(characterSet, randomInteger, length, prefix, suffix, numberOfCoupons);
const {numberOfCoupons, length, characterSet, prefix, suffix, omitCharacters} = Object.assign({}, defaultOptions, option);
const engine = new Engine(characterSet, randomInteger, length, prefix, suffix, numberOfCoupons, omitCharacters);
return engine.run();
};
};
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "couponjs",
"version": "0.7.3",
"version": "0.8.1",
"description": "This is a simple coupon creation project using NodeJS.",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions tests/app/character-set-builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,7 @@ test('Should return hex characters when using builtIn "CHARSET_HEX" option', ()
test('Should return hex lowercase characters when using builtIn "CHARSET_HEX_LOWER" option', () => {
expect(characterSetBuilder({builtIn: [CHARSET_HEX_LOWER]})).toBe(HEX_LOWER);
});

test('Should be able to omit characters', () => {
expect(characterSetBuilder({custom: ['ABCDEF', '123456']}, ['AB', '12'])).toBe('CDEF3456');
});
14 changes: 13 additions & 1 deletion tests/app/engine.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,16 @@ test('Should return 2 unique coupon when character set is "abc", length is 3 and
};
const engine = new Engine(characterSet, randomInteger, 3, defaultOption.prefix, defaultOption.suffix, 2);
expect(engine.run().length).toBe(2);
});
});

test('Should return 2 unique coupon when character set is "abcdef" "123456" and omit "abc" "123", length is 3 and numberOfCoupons is 2', () => {
const characterSet = {
custom: ['abcdef', '123456']
};
const engine = new Engine(characterSet, randomInteger, 6, defaultOption.prefix, defaultOption.suffix, 2, ['abc', '123']);
const coupons = engine.run();
expect(coupons.length).toBe(2);
coupons.forEach(coupon => {
expect(/^[def456]{6}/.test(coupon)).toBeTruthy();
});
});
6 changes: 6 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,9 @@ test('Should generate 100 unique coupons of length 128 using builtIn "CHARSET_AL
}
expect(unqiueCoupons.length).toBe(myCoupons.length);
});

test('Should generate coupon code using uppercase alphabet A-Z of length 6 and omit "ABCDEFGH".', () => {
const coupon = new Coupon();
const result = coupon.generate({ omitCharacters: ['ABC', 'XYZ'] });
expect(/^[D-W]{6}$/.test(result)).toBeTruthy();
});

0 comments on commit 3ac7b52

Please sign in to comment.