Skip to content

Commit

Permalink
Merge pull request #20 from yusufshakeel/dev
Browse files Browse the repository at this point in the history
v0.8.7
  • Loading branch information
yusufshakeel committed Oct 5, 2020
2 parents 0f99db8 + 0ec6c27 commit 4a9e48a
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 57 deletions.
29 changes: 28 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.8.6-blue.svg)](https://www.npmjs.com/package/couponjs)
[![npm version](https://img.shields.io/badge/npm-0.8.7-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 @@ -41,6 +41,7 @@ const CouponJS = require('couponjs');
* [Format coupon with prefix and suffix](#format-coupon-with-prefix-and-suffix)
* [String format rule and prefix-suffix combo](#string-format-rule-and-prefix-suffix-combo)
* [Object format rule and prefix-suffix combo](#object-format-rule-and-prefix-suffix-combo)
* [Possible number of coupons](#possible-number-of-coupons)
* [License](#license)
* [Back this project](#back-this-project)
* [Donate](#donate)
Expand Down Expand Up @@ -277,6 +278,8 @@ const myCoupon = coupon.generate({
});
```

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

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

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

## Omit characters

To omit characters from the generated coupon code we pass the following option.
Expand Down Expand Up @@ -528,6 +533,28 @@ On executing this we will get coupon code like
QW-ERTOWL~ZJHZXC-VB
```

## Possible number of coupons

This section estimates the possible number of coupons that can be generated using different combinations of characters.

|Character Set |Characters |Total Characters |Coupon Length |Total possibility |Coupon Length |Total possibility |Coupon Length |Total possibility |
|--------------------|--------------|--------------------|--------------|-----------------:|--------------|-----------------:|--------------|-----------------:|
|CHARSET_ALPHA |A-Z |26 |6 |308915776 |8 |208827064576 |12 |9.5428957e+16 |
|CHARSET_ALPHA_LOWER |a-z |26 |6 |308915776 |8 |208827064576 |12 |9.5428957e+16 |
|CHARSET_DIGIT |0-9 |10 |6 |1000000 |8 |100000000 |12 |1e+12 |
|CHARSET_ALNUM |A-Z, a-z, 0-9 |62 |6 |56800235584 |8 |2.1834011e+14 |12 |3.2262668e+21 |
|CHARSET_BINARY |0-1 |2 |6 |64 |8 |256 |12 |4096 |
|CHARSET_OCTAL |0-7 |8 |6 |262144 |8 |16777216 |12 |68719476736 |
|CHARSET_HEX |0-9, A-F |16 |6 |16777216 |8 |4294967296 |12 |2.8147498e+14 |
|CHARSET_HEX_LOWER |0-9, a-f |16 |6 |16777216 |8 |4294967296 |12 |2.8147498e+14 |

Points to note!
* Repetition of characters considered in the above computation.
* Prefix and Suffix not considered in the above computaion.
* Omit characters is not set in the above computation.
* Different mix of character sets can be used to change the total number of possible coupons that can be generated.
* Speed and generation of coupons will also depend on the hardware used.

## License
It's free :smiley:

Expand Down
4 changes: 2 additions & 2 deletions app/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const throwValidationError = ({ message, field }) => {
* @param {string|object} format This is the format rule that will be applied to the coupon.
* @constructor
*/
const Engine = function (
const Engine = function ({
characterSetOption,
randomInteger,
length = DEFAULT_LENGTH,
Expand All @@ -52,7 +52,7 @@ const Engine = function (
numberOfCoupons = DEFAULT_NUMBER_OF_COUPONS_TO_GENERATE,
omitCharacters = DEFAULT_OMIT_CHARACTERS,
format = UNDEFINED
) {
}) {
const formatter = format !== UNDEFINED ? new Formatter(format) : { format: coupon => coupon };

const characters = characterSetBuilder(characterSetOption, omitCharacters).split('');
Expand Down
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ const Coupon = function (config) {
const {
numberOfCoupons,
length,
characterSet,
prefix,
suffix,
omitCharacters,
format
format,
characterSet: characterSetOption
} = Object.assign({}, defaultCouponGenerationOption, option);
try {
const engine = new Engine(
characterSet,
const engine = new Engine({
randomInteger,
characterSetOption,
length,
prefix,
suffix,
numberOfCoupons,
omitCharacters,
format
);
});
const generatedCoupons = engine.run();
performance.stopTimer();
const performanceStats = logPerformance ? { performance: performance.stats() } : {};
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.6",
"version": "0.8.7",
"description": "This is a simple coupon creation project using NodeJS.",
"main": "index.js",
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const {
CHARSET_OCTAL,
CHARSET_HEX,
CHARSET_HEX_LOWER
} = require('../../app/constants.js');
const characterSetBuilder = require('../../app/character-set-builder.js');
const { defaultCouponGenerationOption } = require('../../app/option.js');
} = require('../../../app/constants.js');
const characterSetBuilder = require('../../../app/character-set-builder.js');
const { defaultCouponGenerationOption } = require('../../../app/option.js');

test('Should throw error if invalid builtIn option provided', () => {
expect(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const characterSet = require('../../app/character-set.js');
const characterSet = require('../../../app/character-set.js');
const {
ALPHABET_UPPERCASE,
ALPHABET_LOWERCASE,
Expand All @@ -17,7 +17,7 @@ const {
CHARSET_OCTAL,
CHARSET_HEX,
CHARSET_HEX_LOWER
} = require('../../app/constants.js');
} = require('../../../app/constants.js');

test('Should throw error if invalid charSetName provided', () => {
expect.assertions(3);
Expand Down
Loading

0 comments on commit 4a9e48a

Please sign in to comment.