Skip to content

Commit

Permalink
Merge pull request #21 from yusufshakeel/dev
Browse files Browse the repository at this point in the history
v0.8.8
  • Loading branch information
yusufshakeel authored Oct 5, 2020
2 parents 4a9e48a + 1d66fb2 commit bc66eb9
Show file tree
Hide file tree
Showing 19 changed files with 249 additions and 38 deletions.
29 changes: 26 additions & 3 deletions 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.8.7-blue.svg)](https://www.npmjs.com/package/couponjs)
[![npm version](https://img.shields.io/badge/npm-0.8.8-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 All @@ -24,6 +24,7 @@ const CouponJS = require('couponjs');
* [Coupon engine configuration](#coupon-engine-configuration)
* [Verbose](#verbose)
* [Log Performance](#log-performance)
* [Maximum number of coupons to generate](#maximum-number-of-coupons-to-generate)
* [Generate coupon](#generate-coupon)
* [Configuration to generate coupons](#configuration-to-generate-coupons)
* [Coupon of length N](#coupon-of-length-n)
Expand Down Expand Up @@ -159,6 +160,25 @@ And error response like the following.
}
```

### Maximum number of coupons to generate

By default, we can generate `100000` coupons.

If we want to increase or decrease the number of coupons that can be generated then we set the `maxNumberOfCouponsToGenerate`
field.

It takes an integer value greater than 0.

Default value: `100000`

Syntax:

```javascript
const coupon = new CouponJS({
maxNumberOfCouponsToGenerate: 100
});
```

## Generate coupon
Create an object.
```javascript
Expand Down Expand Up @@ -278,7 +298,8 @@ const myCoupon = coupon.generate({
});
```

Reference: [Possible number of coupons](#possible-number-of-coupons)
Reference:
* [Possible number of coupons](#possible-number-of-coupons)

## Coupon with custom characterSet
To use custom characters to generate coupons pass the following option.
Expand Down Expand Up @@ -344,7 +365,9 @@ Sample output:
['95TMY9JV', 'RZU6ZL0K', '1Q19N019']
```

Reference: [Possible number of coupons](#possible-number-of-coupons)
Reference:
* [Maximum number of coupons to generate](#maximum-number-of-coupons-to-generate)
* [Possible number of coupons](#possible-number-of-coupons)

## Omit characters

Expand Down
13 changes: 9 additions & 4 deletions app/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const DEFAULT_COUPON_FORMAT = UNDEFINED;
const ERROR_TYPE_VALIDATION_ERROR = `${COUPONJS}_VALIDATION_ERROR`;
const ERROR_TYPE_CHARACTER_SET_ERROR = `${COUPONJS}_CHARACTER_SET_ERROR`;
const ERROR_TYPE_FORMAT_ERROR = `${COUPONJS}_FORMAT_ERROR`;
const ERROR_TYPE_CONFIGURATION_ERROR = `${COUPONJS}_CONFIGURATION_ERROR`;
const ERROR_TYPE_GENERATE_COUPON_CONFIGURATION_ERROR = `${COUPONJS}_GENERATE_COUPON_CONFIGURATION_ERROR`;
const ERROR_TYPE_COUPON_ENGINE_CONFIGURATION_ERROR = `${COUPONJS}_COUPON_ENGINE_CONFIGURATION_ERROR`;
const ERROR_CONSTANTS = {
[ERROR_TYPE_VALIDATION_ERROR]: {
type: ERROR_TYPE_VALIDATION_ERROR,
Expand All @@ -45,9 +46,13 @@ const ERROR_CONSTANTS = {
type: ERROR_TYPE_FORMAT_ERROR,
message: 'Format error'
},
[ERROR_TYPE_CONFIGURATION_ERROR]: {
type: ERROR_TYPE_CONFIGURATION_ERROR,
message: 'Configuration error'
[ERROR_TYPE_GENERATE_COUPON_CONFIGURATION_ERROR]: {
type: ERROR_TYPE_GENERATE_COUPON_CONFIGURATION_ERROR,
message: 'Generate coupon configuration error'
},
[ERROR_TYPE_COUPON_ENGINE_CONFIGURATION_ERROR]: {
type: ERROR_TYPE_COUPON_ENGINE_CONFIGURATION_ERROR,
message: 'Coupon engine configuration error'
}
};

Expand Down
10 changes: 6 additions & 4 deletions app/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const throwValidationError = ({ message, field }) => {
{
message,
field,
type: ERROR_CONSTANTS.COUPONJS_CONFIGURATION_ERROR.type
type: ERROR_CONSTANTS.COUPONJS_GENERATE_COUPON_CONFIGURATION_ERROR.type
}
]
});
Expand All @@ -41,6 +41,7 @@ const throwValidationError = ({ message, field }) => {
* @param {number} numberOfCoupons Total number of coupons to generate.
* @param {string[]} omitCharacters This is the array of characters that will be ignored.
* @param {string|object} format This is the format rule that will be applied to the coupon.
* @param {number} maxNumberOfCouponsToGenerate This is the maximum number of coupons that can be generated.
* @constructor
*/
const Engine = function ({
Expand All @@ -51,7 +52,8 @@ const Engine = function ({
suffix = DEFAULT_SUFFIX,
numberOfCoupons = DEFAULT_NUMBER_OF_COUPONS_TO_GENERATE,
omitCharacters = DEFAULT_OMIT_CHARACTERS,
format = UNDEFINED
format = UNDEFINED,
maxNumberOfCouponsToGenerate = MAX_NUMBER_OF_COUPONS_TO_GENERATE
}) {
const formatter = format !== UNDEFINED ? new Formatter(format) : { format: coupon => coupon };

Expand All @@ -71,9 +73,9 @@ const Engine = function ({
field: 'numberOfCoupons'
});
}
if (numberOfCoupons > MAX_NUMBER_OF_COUPONS_TO_GENERATE) {
if (numberOfCoupons > maxNumberOfCouponsToGenerate) {
throwValidationError({
message: `Maximum value for numberOfCoupons is ${MAX_NUMBER_OF_COUPONS_TO_GENERATE}.`,
message: `Maximum value for numberOfCoupons is ${maxNumberOfCouponsToGenerate}.`,
field: 'numberOfCoupons'
});
}
Expand Down
6 changes: 4 additions & 2 deletions app/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ const {
CHARSET_ALPHA,
DEFAULT_NUMBER_OF_COUPONS_TO_GENERATE,
DEFAULT_OMIT_CHARACTERS,
DEFAULT_COUPON_FORMAT
DEFAULT_COUPON_FORMAT,
MAX_NUMBER_OF_COUPONS_TO_GENERATE
} = require('./constants.js');

module.exports = {
defaultCouponEngineOption: {
verbose: false,
logPerformance: false
logPerformance: false,
maxNumberOfCouponsToGenerate: MAX_NUMBER_OF_COUPONS_TO_GENERATE
},
defaultCouponGenerationOption: {
length: DEFAULT_LENGTH,
Expand Down
64 changes: 64 additions & 0 deletions app/validator/coupon-config-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';

const { ERROR_CONSTANTS } = require('../constants.js');
const ValidationError = require('../error/validation-error.js');

const isOfType = (variable, type) => typeof variable === type;

function couponConfigValidator(config) {
const { verbose, logPerformance, maxNumberOfCouponsToGenerate } = config;

if (verbose && !isOfType(verbose, 'boolean')) {
throw new ValidationError({
message: `Coupon engine configuration field 'verbose' must be of type boolean.`,
errors: [
{
type: ERROR_CONSTANTS.COUPONJS_COUPON_ENGINE_CONFIGURATION_ERROR.type,
field: 'verbose',
message: `Coupon engine configuration field 'verbose' must be of type boolean.`
}
]
});
}

if (logPerformance && !isOfType(logPerformance, 'boolean')) {
throw new ValidationError({
message: `Coupon engine configuration field 'logPerformance' must be of type boolean.`,
errors: [
{
type: ERROR_CONSTANTS.COUPONJS_COUPON_ENGINE_CONFIGURATION_ERROR.type,
field: 'logPerformance',
message: `Coupon engine configuration field 'logPerformance' must be of type boolean.`
}
]
});
}

if (maxNumberOfCouponsToGenerate && !Number.isInteger(maxNumberOfCouponsToGenerate)) {
throw new ValidationError({
message: `Coupon engine configuration field 'maxNumberOfCouponsToGenerate' must be of type integer.`,
errors: [
{
type: ERROR_CONSTANTS.COUPONJS_COUPON_ENGINE_CONFIGURATION_ERROR.type,
field: 'maxNumberOfCouponsToGenerate',
message: `Coupon engine configuration field 'maxNumberOfCouponsToGenerate' must be of type integer.`
}
]
});
}

if (maxNumberOfCouponsToGenerate < 1) {
throw new ValidationError({
message: `Coupon engine configuration field 'maxNumberOfCouponsToGenerate' must be greater than 0.`,
errors: [
{
type: ERROR_CONSTANTS.COUPONJS_COUPON_ENGINE_CONFIGURATION_ERROR.type,
field: 'maxNumberOfCouponsToGenerate',
message: `Coupon engine configuration field 'maxNumberOfCouponsToGenerate' must be greater than 0.`
}
]
});
}
}

module.exports = { couponConfigValidator };
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Engine = require('./app/engine.js');
const { defaultCouponGenerationOption, defaultCouponEngineOption } = require('./app/option.js');
const randomInteger = require('./app/random-integer.js');
const Performance = require('./app/performance.js');
const { couponConfigValidator } = require('./app/validator/coupon-config-validator.js');

/**
* The Coupon constructor.
Expand All @@ -12,7 +13,12 @@ const Performance = require('./app/performance.js');
*/
const Coupon = function (config) {
const performance = new Performance();
const { verbose, logPerformance } = Object.assign({}, defaultCouponEngineOption, config);
const { verbose, logPerformance, maxNumberOfCouponsToGenerate } = Object.assign(
{},
defaultCouponEngineOption,
config
);
couponConfigValidator({ verbose, logPerformance, maxNumberOfCouponsToGenerate });

/**
* This will generate coupons.
Expand Down Expand Up @@ -40,7 +46,8 @@ const Coupon = function (config) {
suffix,
numberOfCoupons,
omitCharacters,
format
format,
maxNumberOfCouponsToGenerate
});
const generatedCoupons = engine.run();
performance.stopTimer();
Expand Down
5 changes: 4 additions & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"testEnvironment": "node",
"verbose": true,
"bail": 1,
"bail": true,
"testRegex": "(unit).test.js$",
"collectCoverage": true,
"collectCoverageFrom": [
"**/*.{js,jsx}",
"!**/coverage/**",
"!**/docs/**",
"!**/node_modules/**",
"!**/performance/**",
"!**/tests/**"
],
"coverageThreshold": {
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.8.7",
"version": "0.8.8",
"description": "This is a simple coupon creation project using NodeJS.",
"main": "index.js",
"scripts": {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test('Should throw error if numberOfCoupons is less than 1', () => {
{
field: 'numberOfCoupons',
message: 'Minimum value for numberOfCoupons is 1.',
type: 'COUPONJS_CONFIGURATION_ERROR'
type: 'COUPONJS_GENERATE_COUPON_CONFIGURATION_ERROR'
}
]);
}
Expand Down Expand Up @@ -60,7 +60,7 @@ test('Should throw error if numberOfCoupons is greater than 100000', () => {
{
field: 'numberOfCoupons',
message: 'Maximum value for numberOfCoupons is 100000.',
type: 'COUPONJS_CONFIGURATION_ERROR'
type: 'COUPONJS_GENERATE_COUPON_CONFIGURATION_ERROR'
}
]);
}
Expand Down Expand Up @@ -93,7 +93,7 @@ test('Should throw error if length is 3 and numberOfCoupons is greater than tota
{
field: 'numberOfCoupons',
message: 'Total number of possible coupons that can be generated is 27.',
type: 'COUPONJS_CONFIGURATION_ERROR'
type: 'COUPONJS_GENERATE_COUPON_CONFIGURATION_ERROR'
}
]);
}
Expand Down Expand Up @@ -126,7 +126,7 @@ test('Should throw error if length is 3 and numberOfCoupons is greater than tota
{
field: 'numberOfCoupons',
message: 'Total number of possible coupons that can be generated is 17576.',
type: 'COUPONJS_CONFIGURATION_ERROR'
type: 'COUPONJS_GENERATE_COUPON_CONFIGURATION_ERROR'
}
]);
}
Expand Down Expand Up @@ -159,7 +159,7 @@ test('Should throw error if length is 3 and numberOfCoupons is greater than tota
{
field: 'numberOfCoupons',
message: 'Total number of possible coupons that can be generated is 17576.',
type: 'COUPONJS_CONFIGURATION_ERROR'
type: 'COUPONJS_GENERATE_COUPON_CONFIGURATION_ERROR'
}
]);
}
Expand Down Expand Up @@ -192,7 +192,7 @@ test('Should throw error if length is 3 and numberOfCoupons is greater than tota
{
field: 'numberOfCoupons',
message: 'Total number of possible coupons that can be generated is 1000.',
type: 'COUPONJS_CONFIGURATION_ERROR'
type: 'COUPONJS_GENERATE_COUPON_CONFIGURATION_ERROR'
}
]);
}
Expand Down Expand Up @@ -225,7 +225,7 @@ test('Should throw error if length is 3 and numberOfCoupons is greater than tota
{
field: 'numberOfCoupons',
message: 'Total number of possible coupons that can be generated is 8.',
type: 'COUPONJS_CONFIGURATION_ERROR'
type: 'COUPONJS_GENERATE_COUPON_CONFIGURATION_ERROR'
}
]);
}
Expand Down Expand Up @@ -258,7 +258,7 @@ test('Should throw error if length is 3 and numberOfCoupons is greater than tota
{
field: 'numberOfCoupons',
message: 'Total number of possible coupons that can be generated is 512.',
type: 'COUPONJS_CONFIGURATION_ERROR'
type: 'COUPONJS_GENERATE_COUPON_CONFIGURATION_ERROR'
}
]);
}
Expand Down Expand Up @@ -291,7 +291,7 @@ test('Should throw error if length is 3 and numberOfCoupons is greater than tota
{
field: 'numberOfCoupons',
message: 'Total number of possible coupons that can be generated is 4096.',
type: 'COUPONJS_CONFIGURATION_ERROR'
type: 'COUPONJS_GENERATE_COUPON_CONFIGURATION_ERROR'
}
]);
}
Expand Down Expand Up @@ -324,7 +324,7 @@ test('Should throw error if length is 2 and numberOfCoupons is greater than tota
{
field: 'numberOfCoupons',
message: 'Total number of possible coupons that can be generated is 3844.',
type: 'COUPONJS_CONFIGURATION_ERROR'
type: 'COUPONJS_GENERATE_COUPON_CONFIGURATION_ERROR'
}
]);
}
Expand All @@ -350,7 +350,7 @@ test('Should throw error if length is less than 1', () => {
{
field: 'length',
message: 'Minimum value for length is 1.',
type: 'COUPONJS_CONFIGURATION_ERROR'
type: 'COUPONJS_GENERATE_COUPON_CONFIGURATION_ERROR'
}
]);
}
Expand All @@ -376,7 +376,7 @@ test('Should throw error if length is greater than 128', () => {
{
field: 'length',
message: 'Maximum value for length is 128.',
type: 'COUPONJS_CONFIGURATION_ERROR'
type: 'COUPONJS_GENERATE_COUPON_CONFIGURATION_ERROR'
}
]);
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit bc66eb9

Please sign in to comment.