Skip to content

Commit

Permalink
Merge pull request #7 from yusufshakeel/dev
Browse files Browse the repository at this point in the history
v0.6.1
  • Loading branch information
yusufshakeel committed Mar 15, 2020
2 parents 3c34447 + 4f24df9 commit 7b70c98
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 62 deletions.
2 changes: 1 addition & 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.6.0-blue.svg)](https://www.npmjs.com/package/couponjs)
[![npm version](https://img.shields.io/badge/npm-0.6.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)

# Getting Started
Expand Down
59 changes: 2 additions & 57 deletions app/character-set-builder.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,15 @@
const {
ALPHABET_UPPERCASE,
ALPHABET_LOWERCASE,
DIGIT,
BINARY,
OCTAL,
HEX,
HEX_LOWER,
CHARSET_ALPHA,
CHARSET_ALPHA_LOWER,
CHARSET_DIGIT,
CHARSET_ALNUM,
CHARSET_BINARY,
CHARSET_OCTAL,
CHARSET_HEX,
CHARSET_HEX_LOWER
} = 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.
* @returns {string} The set of characters based on the options provided.
*/
function characterSetBuilder(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}`;
case CHARSET_BINARY:
return `${BINARY}`;
case CHARSET_OCTAL:
return `${OCTAL}`;
case CHARSET_HEX:
return `${HEX}`;
case CHARSET_HEX_LOWER:
return `${HEX_LOWER}`;
default:
const validCharSets = [
CHARSET_ALPHA,
CHARSET_ALPHA_LOWER,
CHARSET_DIGIT,
CHARSET_ALNUM,
CHARSET_BINARY,
CHARSET_OCTAL,
CHARSET_HEX,
CHARSET_HEX_LOWER
];
throw new Error(`Invalid builtIn characterSet specified. Allowed values: ${validCharSets.join(', ')}`);
}
}

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

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

const customCharacters = custom.reduce((chars, charSet) => {
Expand Down
57 changes: 57 additions & 0 deletions app/character-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const {
ALPHABET_UPPERCASE,
ALPHABET_LOWERCASE,
DIGIT,
BINARY,
OCTAL,
HEX,
HEX_LOWER,
CHARSET_ALPHA,
CHARSET_ALPHA_LOWER,
CHARSET_DIGIT,
CHARSET_ALNUM,
CHARSET_BINARY,
CHARSET_OCTAL,
CHARSET_HEX,
CHARSET_HEX_LOWER
} = require('./constants.js');

/**
* This will return the characters based on the character set name.
* @param {string} charSetName This is the name of the character set.
* @returns {string} String of characters.
*/
function characterSet(charSetName) {
const validCharSets = [
CHARSET_ALPHA,
CHARSET_ALPHA_LOWER,
CHARSET_DIGIT,
CHARSET_ALNUM,
CHARSET_BINARY,
CHARSET_OCTAL,
CHARSET_HEX,
CHARSET_HEX_LOWER
];
switch (charSetName) {
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}`;
case CHARSET_BINARY:
return `${BINARY}`;
case CHARSET_OCTAL:
return `${OCTAL}`;
case CHARSET_HEX:
return `${HEX}`;
case CHARSET_HEX_LOWER:
return `${HEX_LOWER}`;
default:
throw new Error(`Invalid builtIn characterSet specified. Allowed values: ${validCharSets.join(', ')}`);
}
}

module.exports = characterSet;
2 changes: 1 addition & 1 deletion app/option.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {DEFAULT_LENGTH, DEFAULT_PREFIX, DEFAULT_SUFFIX, ALPHABET_UPPERCASE, CHARSET_ALPHA} = require('./constants.js');
const {DEFAULT_LENGTH, DEFAULT_PREFIX, DEFAULT_SUFFIX, CHARSET_ALPHA} = require('./constants.js');

module.exports = {
length: DEFAULT_LENGTH,
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.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "couponjs",
"version": "0.6.0",
"version": "0.6.1",
"description": "This is a simple coupon creation project using NodeJS.",
"main": "index.js",
"scripts": {
"test": "jest -c jest.config.json",
"generate-docs": "npx jsdoc -c jsdocs.config.json",
"precommit": "npm run test && npm run generate-docs"
"lint": "npx eslint -c .eslintrc.json ./index.js ./app",
"precommit": "npm run lint && npm run test && npm run generate-docs"
},
"repository": {
"type": "git",
Expand Down
57 changes: 57 additions & 0 deletions tests/app/character-set.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const characterSet = require('../../app/character-set.js');
const {
ALPHABET_UPPERCASE,
ALPHABET_LOWERCASE,
DIGIT,
BINARY,
OCTAL,
HEX,
HEX_LOWER,
CHARSET_ALPHA,
CHARSET_ALPHA_LOWER,
CHARSET_DIGIT,
CHARSET_ALNUM,
CHARSET_BINARY,
CHARSET_OCTAL,
CHARSET_HEX,
CHARSET_HEX_LOWER
} = require('../../app/constants.js');

test('Should throw error if invalid charSetName provided', () => {
expect(() => {
const chars = characterSet('UNKNOWN');
throw new Error('Should have failed.');
}).toThrow('Invalid builtIn characterSet specified. Allowed values: CHARSET_ALPHA, CHARSET_ALPHA_LOWER, CHARSET_DIGIT, CHARSET_ALNUM, CHARSET_BINARY, CHARSET_OCTAL, CHARSET_HEX, CHARSET_HEX_LOWER');
});

test('Should return uppercase alphabet A-Z when using charSetName "CHARSET_ALPHA"', () => {
expect(characterSet(CHARSET_ALPHA)).toBe(ALPHABET_UPPERCASE);
});

test('Should return lowercase alphabet a-z when using charSetName "CHARSET_ALPHA_LOWER"', () => {
expect(characterSet(CHARSET_ALPHA_LOWER)).toBe(ALPHABET_LOWERCASE);
});

test('Should return digits 0-9 when using charSetName "CHARSET_DIGIT"', () => {
expect(characterSet(CHARSET_DIGIT)).toBe(DIGIT);
});

test('Should return uppercase, lowercase alphabet and digit when using charSetName "CHARSET_ALNUM"', () => {
expect(characterSet(CHARSET_ALNUM)).toBe(`${ALPHABET_UPPERCASE}${ALPHABET_LOWERCASE}${DIGIT}`);
});

test('Should return binary characters when using charSetName "CHARSET_BINARY"', () => {
expect(characterSet(CHARSET_BINARY)).toBe(BINARY);
});

test('Should return octal characters when using charSetName "CHARSET_OCTAL"', () => {
expect(characterSet(CHARSET_OCTAL)).toBe(OCTAL);
});

test('Should characterSet hex characters when using charSetName "CHARSET_HEX"', () => {
expect(characterSet(CHARSET_HEX)).toBe(HEX);
});

test('Should return hex lowercase characters when using charSetName "CHARSET_HEX_LOWER"', () => {
expect(characterSet(CHARSET_HEX_LOWER)).toBe(HEX_LOWER);
});

0 comments on commit 7b70c98

Please sign in to comment.