Skip to content

Commit

Permalink
Merge pull request #17 from yusufshakeel/dev
Browse files Browse the repository at this point in the history
v0.8.4
  • Loading branch information
yusufshakeel committed Oct 3, 2020
2 parents 0943994 + 8183564 commit b3c1303
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 60 deletions.
62 changes: 57 additions & 5 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.3-blue.svg)](https://www.npmjs.com/package/couponjs)
[![npm version](https://img.shields.io/badge/npm-0.8.4-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 @@ -13,15 +13,18 @@ Add this to your project using npm.
```

Next, require `couponjs`.

```javascript
const Coupon = require('couponjs');
const CouponJS = require('couponjs');
```

## Table of Contents

* [Getting Started](#getting-started)
* [Coupon engine configuration](#coupon-engine-configuration)
* [Verbose](#verbose)
* [Generate coupon](#generate-coupon)
* [Configurations](#configurations)
* [Configuration to generate coupons](#configuration-to-generate-coupons)
* [Coupon of length N](#coupon-of-length-n)
* [Coupon with prefix](#coupon-with-prefix)
* [Coupon with suffix](#coupon-with-suffix)
Expand All @@ -41,11 +44,60 @@ const Coupon = require('couponjs');
* [Back this project](#back-this-project)
* [Donate](#donate)

## Coupon engine configuration

We can configure the CouponJS engine to get reponse in a different format.

The syntax looks like the following.

```javascript
const coupon = new CouponJS({ key: value });
```

### Verbose

If we want verbose response (success/error) then we can set the field `verbose` to true.

Default value: `false`

```javascript
const coupon = new CouponJS({ verbose: true });
```

If set to `true` then success response will look like the following.

```
{
status: 'success',
numberOfCoupons: 1,
coupons: 'IRKLLE'
}
```

And error response will look like the following.

```
{
status: 'error',
error: {
type: 'COUPONJS_VALIDATION_ERROR',
message: 'Invalid characters used in the format rule.',
errors: [
{
field: 'format',
type: 'COUPONJS_FORMAT_ERROR',
message: 'Invalid characters used in the format rule. Only x and - are allowed. And only one - inbetween like xxx-xxx.'
}
]
}
}
```


## Generate coupon
Create an object.
```javascript
const coupon = new Coupon();
const coupon = new CouponJS();
```

Now, call the `generate` method.
Expand All @@ -56,7 +108,7 @@ The above code will produce coupon like `ASDFGH`.

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

## Configurations
## Configuration to generate coupons

We can pass different options to configure the engine to generate coupons as per our settings.

Expand Down
21 changes: 13 additions & 8 deletions app/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ const {
} = require('./constants.js');

module.exports = {
length: DEFAULT_LENGTH,
prefix: DEFAULT_PREFIX,
suffix: DEFAULT_SUFFIX,
characterSet: {
builtIn: [CHARSET_ALPHA]
defaultCouponEngineOption: {
verbose: false
},
numberOfCoupons: DEFAULT_NUMBER_OF_COUPONS_TO_GENERATE,
omitCharacters: DEFAULT_OMIT_CHARACTERS,
format: DEFAULT_COUPON_FORMAT
defaultCouponGenerationOption: {
length: DEFAULT_LENGTH,
prefix: DEFAULT_PREFIX,
suffix: DEFAULT_SUFFIX,
characterSet: {
builtIn: [CHARSET_ALPHA]
},
numberOfCoupons: DEFAULT_NUMBER_OF_COUPONS_TO_GENERATE,
omitCharacters: DEFAULT_OMIT_CHARACTERS,
format: DEFAULT_COUPON_FORMAT
}
};
52 changes: 38 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use strict';

const Engine = require('./app/engine.js');
const defaultOptions = require('./app/option.js');
const { defaultCouponGenerationOption, defaultCouponEngineOption } = require('./app/option.js');
const randomInteger = require('./app/random-integer.js');

/**
* The Coupon constructor.
* @param {object} config This is to configure the coupon engine.
* @constructor
*/
const Coupon = function () {
const Coupon = function (config) {
const { verbose } = Object.assign({}, defaultCouponEngineOption, config);

/**
* This will generate coupons.
*
Expand All @@ -24,18 +27,39 @@ const Coupon = function () {
suffix,
omitCharacters,
format
} = Object.assign({}, defaultOptions, option);
const engine = new Engine(
characterSet,
randomInteger,
length,
prefix,
suffix,
numberOfCoupons,
omitCharacters,
format
);
return engine.run();
} = Object.assign({}, defaultCouponGenerationOption, option);
try {
const engine = new Engine(
characterSet,
randomInteger,
length,
prefix,
suffix,
numberOfCoupons,
omitCharacters,
format
);
const generatedCoupons = engine.run();
const verboseResult = {
numberOfCoupons,
status: 'success',
coupons: generatedCoupons
};
return verbose ? verboseResult : generatedCoupons;
} catch (e) {
if (verbose) {
return {
status: 'error',
error: {
message: e.message,
type: e.type,
errors: e.errors
}
};
} else {
throw e;
}
}
};
};

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.3",
"version": "0.8.4",
"description": "This is a simple coupon creation project using NodeJS.",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions tests/app/character-set-builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const {
CHARSET_HEX_LOWER
} = require('../../app/constants.js');
const characterSetBuilder = require('../../app/character-set-builder.js');
const defaultOptions = require('../../app/option.js');
const { defaultCouponGenerationOption } = require('../../app/option.js');

test('Should throw error if invalid builtIn option provided', () => {
expect(() => {
Expand All @@ -33,7 +33,7 @@ test('Should throw error if invalid builtIn option provided', () => {
});

test('Should return uppercase alphabet A-Z when using default options', () => {
expect(characterSetBuilder(defaultOptions.characterSet)).toBe(ALPHABET_UPPERCASE);
expect(characterSetBuilder(defaultCouponGenerationOption.characterSet)).toBe(ALPHABET_UPPERCASE);
});

test('Should return uppercase alphabet A-Z when using builtIn "CHARSET_ALPHA" option', () => {
Expand Down
Loading

0 comments on commit b3c1303

Please sign in to comment.