Skip to content

Commit

Permalink
Merge pull request #5 from yusufshakeel/dev
Browse files Browse the repository at this point in the history
v0.5.0
  • Loading branch information
yusufshakeel committed Mar 14, 2020
2 parents 6c4e5ba + 806c655 commit 4d270a6
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 27 deletions.
3 changes: 2 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.4.0-blue.svg)](https://www.npmjs.com/package/couponjs)
[![npm version](https://img.shields.io/badge/npm-0.5.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
Expand Down Expand Up @@ -102,6 +102,7 @@ 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
- `CHARSET_ALNUM` -- which consists of uppercase alphabet A-Z, lowercase alphabet a-z and digit 0-9

Example: If we want uppercase and digit we can pass the following.
```javascript
Expand Down
42 changes: 24 additions & 18 deletions app/character-set-builder.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
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}"]`);
}
}
const {CHARSET_ALPHA, CHARSET_ALPHA_LOWER, CHARSET_DIGIT, CHARSET_ALNUM, ALPHABET_UPPERCASE, ALPHABET_LOWERCASE, DIGIT} = require('./constants.js');

/**
* This will generate a string of unique characters based on the options provided.
Expand All @@ -21,14 +7,34 @@ function characters (charSet) {
*/
function characterSetBuilder(characterSetOptions) {

const { builtIn = [], custom = [] } = characterSetOptions;
/**
* 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;
case CHARSET_ALNUM:
return `${ALPHABET_UPPERCASE}${ALPHABET_LOWERCASE}${DIGIT}`;
default:
throw new Error(`Invalid builtIn characterSet specified. Allowed values ["${CHARSET_ALPHA}", "${CHARSET_ALPHA_LOWER}", "${CHARSET_DIGIT}"]`);
}
}

const {builtIn = [], custom = []} = characterSetOptions;

const builtInCharacters = builtIn.reduce((chars, charSet) => {
return `${chars}${characters(charSet)}`
return `${chars}${characters(charSet)}`;
}, '');

const customCharacters = custom.reduce((chars, charSet) => {
return `${chars}${charSet}`
return `${chars}${charSet}`;
}, '');

const uniqueCharacters = `${builtInCharacters}${customCharacters}`.split('').reduce((characters, character) => {
Expand Down
5 changes: 3 additions & 2 deletions app/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @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}}
* @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, CHARSET_ALNUM: string}}
*/
module.exports = {
MAX_LENGTH: 128,
Expand All @@ -12,5 +12,6 @@ module.exports = {
DIGIT: '0123456789',
CHARSET_ALPHA: 'CHARSET_ALPHA',
CHARSET_ALPHA_LOWER: 'CHARSET_ALPHA_LOWER',
CHARSET_DIGIT: 'CHARSET_DIGIT'
CHARSET_DIGIT: 'CHARSET_DIGIT',
CHARSET_ALNUM: 'CHARSET_ALNUM'
};
8 changes: 4 additions & 4 deletions 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.4.0",
"version": "0.5.0",
"description": "This is a simple coupon creation project using NodeJS.",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 5 additions & 1 deletion tests/app/character-set-builder.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {ALPHABET_UPPERCASE, ALPHABET_LOWERCASE, DIGIT, CHARSET_DIGIT, CHARSET_ALPHA_LOWER, CHARSET_ALPHA} = require('../../app/constants.js');
const {ALPHABET_UPPERCASE, ALPHABET_LOWERCASE, DIGIT, CHARSET_DIGIT, CHARSET_ALPHA_LOWER, CHARSET_ALPHA, CHARSET_ALNUM} = require('../../app/constants.js');
const characterSetBuilder = require('../../app/character-set-builder.js');
const defaultOptions = require('../../app/option.js');

Expand Down Expand Up @@ -64,3 +64,7 @@ test('Should return unique characters when option has duplicate characters', ()
};
expect(characterSetBuilder(option)).toBe(`${ALPHABET_UPPERCASE}${ALPHABET_LOWERCASE}${DIGIT}`);
});

test('Should return uppercase, lowercase alphabet and digit when using builtIn "CHARSET_ALNUM" option', () => {
expect(characterSetBuilder({builtIn: [CHARSET_ALNUM]})).toBe(`${ALPHABET_UPPERCASE}${ALPHABET_LOWERCASE}${DIGIT}`);
});
10 changes: 10 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,13 @@ test('Should generate coupon code of length 8 with prefix and suffix and using c
});
expect(/^SUPER[A-Za-z0-9@$&]{8}AWESOME$/.test(myCoupon)).toBeTruthy();
});

test('Should generate coupon code using characterSet builtIn CHARSET_ALNUM option', () => {
const coupon = new Coupon();
const myCoupon = coupon.generate({
characterSet: {
builtIn: ['CHARSET_ALNUM']
}
});
expect(/^[A-Za-z0-9]{6}$/.test(myCoupon)).toBeTruthy();
});

0 comments on commit 4d270a6

Please sign in to comment.