-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from yusufshakeel/dev
v0.4.0
- Loading branch information
Showing
11 changed files
with
326 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
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}"]`); | ||
} | ||
} | ||
|
||
/** | ||
* 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) { | ||
|
||
const { builtIn = [], custom = [] } = characterSetOptions; | ||
|
||
const builtInCharacters = builtIn.reduce((chars, charSet) => { | ||
return `${chars}${characters(charSet)}` | ||
}, ''); | ||
|
||
const customCharacters = custom.reduce((chars, charSet) => { | ||
return `${chars}${charSet}` | ||
}, ''); | ||
|
||
const uniqueCharacters = `${builtInCharacters}${customCharacters}`.split('').reduce((characters, character) => { | ||
return characters + (characters.indexOf(character) === -1 ? character : ''); | ||
}, ''); | ||
|
||
return `${uniqueCharacters}`; | ||
} | ||
|
||
module.exports = characterSetBuilder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
/** | ||
* @type {{DEFAULT_LENGTH: number, DEFAULT_PREFIX: string, DEFAULT_SUFFIX: string, ALPHABET_UPPERCASE: 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}} | ||
*/ | ||
module.exports = { | ||
MAX_LENGTH: 128, | ||
MIN_LENGTH: 1, | ||
DEFAULT_LENGTH: 6, | ||
DEFAULT_PREFIX: '', | ||
DEFAULT_SUFFIX: '', | ||
ALPHABET_UPPERCASE: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
ALPHABET_UPPERCASE: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', | ||
ALPHABET_LOWERCASE: 'abcdefghijklmnopqrstuvwxyz', | ||
DIGIT: '0123456789', | ||
CHARSET_ALPHA: 'CHARSET_ALPHA', | ||
CHARSET_ALPHA_LOWER: 'CHARSET_ALPHA_LOWER', | ||
CHARSET_DIGIT: 'CHARSET_DIGIT' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
const {DEFAULT_LENGTH, DEFAULT_PREFIX, DEFAULT_SUFFIX, ALPHABET_UPPERCASE} = require('./constants.js'); | ||
const {DEFAULT_LENGTH, DEFAULT_PREFIX, DEFAULT_SUFFIX, ALPHABET_UPPERCASE, CHARSET_ALPHA} = require('./constants.js'); | ||
|
||
module.exports = { | ||
length: DEFAULT_LENGTH, | ||
prefix: DEFAULT_PREFIX, | ||
suffix: DEFAULT_SUFFIX, | ||
characters: ALPHABET_UPPERCASE | ||
characterSet: { | ||
builtIn: [CHARSET_ALPHA] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const {ALPHABET_UPPERCASE, ALPHABET_LOWERCASE, DIGIT, CHARSET_DIGIT, CHARSET_ALPHA_LOWER, CHARSET_ALPHA} = require('../../app/constants.js'); | ||
const characterSetBuilder = require('../../app/character-set-builder.js'); | ||
const defaultOptions = require('../../app/option.js'); | ||
|
||
test('Should throw error if invalid builtIn option provided', () => { | ||
expect(() => { | ||
const option = { | ||
builtIn: ['UNKNOWN'] | ||
}; | ||
const chars = characterSetBuilder(option); | ||
throw new Error('Should have failed.'); | ||
}).toThrow('Invalid builtIn characterSet specified. Allowed values ["CHARSET_ALPHA", "CHARSET_ALPHA_LOWER", "CHARSET_DIGIT"]'); | ||
}); | ||
|
||
test('Should return uppercase alphabet A-Z when using default options', () => { | ||
expect(characterSetBuilder(defaultOptions.characterSet)).toBe(ALPHABET_UPPERCASE); | ||
}); | ||
|
||
test('Should return uppercase alphabet A-Z when using builtIn "CHARSET_ALPHA" option', () => { | ||
expect(characterSetBuilder({builtIn: [CHARSET_ALPHA]})).toBe(ALPHABET_UPPERCASE); | ||
}); | ||
|
||
test('Should return lowercase alphabet a-z when using builtIn "CHARSET_ALPHA_LOWER" option', () => { | ||
expect(characterSetBuilder({builtIn: [CHARSET_ALPHA_LOWER]})).toBe(ALPHABET_LOWERCASE); | ||
}); | ||
|
||
test('Should return digits 0-9 when using builtIn "CHARSET_DIGIT" option', () => { | ||
expect(characterSetBuilder({builtIn: [CHARSET_DIGIT]})).toBe(DIGIT); | ||
}); | ||
|
||
test('Should return uppercase, lowercase alphabet and digits when using builtIn ["CHARSET_ALPHA", "CHARSET_ALPHA_LOWER", "CHARSET_DIGIT"] option', () => { | ||
expect(characterSetBuilder({builtIn: [CHARSET_ALPHA, CHARSET_ALPHA_LOWER, CHARSET_DIGIT]})).toBe(`${ALPHABET_UPPERCASE}${ALPHABET_LOWERCASE}${DIGIT}`); | ||
}); | ||
|
||
test('Should return alphabet ABC when using custom "ABC" option', () => { | ||
expect(characterSetBuilder({custom: ['ABC']})).toBe('ABC'); | ||
}); | ||
|
||
test('Should return alphabet abc when using custom "abc" option', () => { | ||
expect(characterSetBuilder({custom: ['abc']})).toBe('abc'); | ||
}); | ||
|
||
test('Should return digits 123 when using custom "123" option', () => { | ||
expect(characterSetBuilder({custom: ['123']})).toBe('123'); | ||
}); | ||
|
||
test('Should return alphabet uppercase, lowercase and digit "ABCabc123" when using custom ["ABC", "abc", "123"] option', () => { | ||
expect(characterSetBuilder({custom: ['ABC', 'abc', '123']})).toBe('ABCabc123'); | ||
}); | ||
|
||
test('Should return both builtIn and custom characters when using custom both options', () => { | ||
const option = { | ||
builtIn: [CHARSET_ALPHA, CHARSET_ALPHA_LOWER, CHARSET_DIGIT], | ||
custom: ['@$%'] | ||
}; | ||
const expectedResult = `${ALPHABET_UPPERCASE}${ALPHABET_LOWERCASE}${DIGIT}@$%`; | ||
expect(characterSetBuilder(option)).toBe(expectedResult); | ||
}); | ||
|
||
test('Should return unique characters when option has duplicate characters', () => { | ||
const option = { | ||
builtIn: [CHARSET_ALPHA, CHARSET_ALPHA_LOWER, CHARSET_DIGIT], | ||
custom: ['ABC', 'abc', 'BCD', 'xyz', '123'] | ||
}; | ||
expect(characterSetBuilder(option)).toBe(`${ALPHABET_UPPERCASE}${ALPHABET_LOWERCASE}${DIGIT}`); | ||
}); |
Oops, something went wrong.