diff --git a/README.md b/README.md index 675f038..617ad4f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/app/character-set-builder.js b/app/character-set-builder.js index 4cfd65f..6ce7844 100644 --- a/app/character-set-builder.js +++ b/app/character-set-builder.js @@ -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. @@ -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) => { diff --git a/app/constants.js b/app/constants.js index e1b23ce..7b85416 100644 --- a/app/constants.js +++ b/app/constants.js @@ -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, @@ -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' }; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 570ddda..6c839c8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "couponjs", - "version": "0.4.0", + "version": "0.5.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -581,9 +581,9 @@ }, "dependencies": { "acorn": { - "version": "6.4.0", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-6.4.0.tgz", - "integrity": "sha512-gac8OEcQ2Li1dxIEWGZzsp2BitJxwkwcOm0zHAJLcPJaVvm58FRnk6RkuLRpU1EujipU2ZFODv2P9DLMfnV8mw==", + "version": "6.4.1", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-6.4.1.tgz", + "integrity": "sha512-ZVA9k326Nwrj3Cj9jlh3wGFutC2ZornPNARZwsNYqQYgN0EsV2d53w5RN/co65Ohn4sUAUtb1rSUAOD6XN9idA==", "dev": true } } diff --git a/package.json b/package.json index 8c93885..f8bef2b 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/tests/app/character-set-builder.test.js b/tests/app/character-set-builder.test.js index a3f022a..e12dbdc 100644 --- a/tests/app/character-set-builder.test.js +++ b/tests/app/character-set-builder.test.js @@ -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'); @@ -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}`); +}); diff --git a/tests/index.test.js b/tests/index.test.js index 3edc665..0334eff 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -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(); +});