Last active
May 12, 2021 16:29
-
-
Save kiranparajuli589/ba01cf78e012b089a31a5e6a493e3346 to your computer and use it in GitHub Desktop.
Name in a box
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
class NameBox { | |
#nameToPrint | |
#spaceBeforeName | |
#numberOfLines | |
#specialCharacter | |
/** | |
* Name box initializer | |
* | |
* @param {String} name Name to wrap | |
* @param {Number} spaceBeforeName Number of spaces to print before name | |
* @param {Number} numberOfLines Number of lines or height of the box | |
* @param {character} character Special character for box | |
*/ | |
constructor(name, spaceBeforeName = 3, numberOfLines = 3, character = '*') { | |
this.#nameToPrint = this.validateName(name) | |
this.#spaceBeforeName = this.validateSpaceBeforeName(spaceBeforeName) | |
this.#numberOfLines = this.validateNumberOfLines(numberOfLines) | |
this.#specialCharacter = this.validateSpecialCharacter(character) | |
} | |
/** | |
* validates if input is a single character | |
* | |
* @param {String} value string to validate | |
* @param {String} field field name | |
* | |
* @returns {String} validated string | |
* @throws Error | |
*/ | |
validateSingleCharacter(value, field) { | |
const lengthOfValue = value.length | |
if (lengthOfValue !== 1) { | |
throw new Error(`${field}: expected single character but got: ${lengthOfValue} characters`) | |
} | |
} | |
/** | |
* validates if input is a non empty string | |
* | |
* @param {String} value string to validate | |
* @param {String} field field name | |
* | |
* @returns {String} validated string | |
* @throws Error | |
*/ | |
validateNonEmptyString(value, field) { | |
if (!value.length > 0) { | |
throw new Error(`${field}: expected non empty string but found empty`) | |
} | |
} | |
/** | |
* validates if number is an odd digit | |
* | |
* @param {Number} value number to validate | |
* @param {String} field field name | |
* | |
* @returns {Number} validated number | |
* @throws Error | |
*/ | |
validateOddDigit(value, field) { | |
if (value % 2 === 0) { | |
throw new Error(`${field}: number of lines can't be an even digit`) | |
} | |
} | |
/** | |
* validates if number is non-negative | |
* | |
* @param {Number} value number to validate | |
* @param {String} field field name | |
* | |
* @returns {Number} validated number | |
* @throws Error | |
*/ | |
validateNonNegativeInteger(value, field) { | |
if (value < 0) { | |
throw new Error(`${field}: number of lines can't be a negative digit`) | |
} | |
return value | |
} | |
/** | |
* validates type of value with expected type | |
* | |
* @param {*} value variable to validate | |
* @param {String} expectedType expected type | |
* @param {String} field field name | |
* | |
* @returns {*} validated data | |
* @throws Error | |
*/ | |
validateType(value, expectedType, field) { | |
const actualType = typeof value | |
if (actualType !== expectedType) { | |
throw new Error(`${field}: expected type '${expectedType}', got '${actualType}'`) | |
} | |
return value | |
} | |
/** | |
* validates special character field | |
* | |
* @param {*} value | |
* | |
* @returns {String} | |
*/ | |
validateSpecialCharacter(value) { | |
this.validateType(value, 'string', 'character') | |
this.validateSingleCharacter(value, 'character') | |
return value | |
} | |
/** | |
* validates name field | |
* | |
* @param {*} value | |
* | |
* @returns {String} | |
*/ | |
validateName(value) { | |
return this.validateType(value, 'string', 'name') | |
} | |
/** | |
* validates spaces before name field | |
* | |
* @param {*} value | |
* | |
* @returns {Number} | |
*/ | |
validateSpaceBeforeName(value) { | |
this.validateNonNegativeInteger(value, 'spaceBeforeName') | |
return value | |
} | |
/** | |
* validates number of lines files | |
* | |
* @param {*} value | |
* | |
* @returns {Number} | |
*/ | |
validateNumberOfLines(value) { | |
this.validateType(value, 'number', 'numberOfLines') | |
this.validateNonNegativeInteger(value, 'numberOfLines') | |
this.validateOddDigit(value, 'numberOfLines') | |
return value | |
} | |
/** | |
* returns length of line | |
* | |
* @returns {Number} | |
*/ | |
getLineLength() { | |
return this.#spaceBeforeName*2 + this.#nameToPrint.length + 2 | |
} | |
/** | |
* line for top and bottom part of the name box | |
* Example: ************* | |
* | |
* @returns {String} | |
*/ | |
getBaseLine() { | |
let string = '' | |
for (let i = 0; i < this.getLineLength(); i++) { | |
string += this.#specialCharacter | |
} | |
return string | |
} | |
/** | |
* line used for except base line and name line | |
* Example: * * | |
* | |
* @returns {String} | |
*/ | |
returnNonNameLine() { | |
let string = '' | |
for (let i = 0; i < this.getLineLength() - 2; i++) { | |
string += ' ' | |
} | |
return `${this.#specialCharacter}${string}${this.#specialCharacter}` | |
} | |
/** | |
* line containing name | |
* Example: * Kiran * | |
* | |
* @returns {String} | |
*/ | |
getNameLine() { | |
let temp = this.#nameToPrint | |
let string | |
for (let i = 0; i < this.#spaceBeforeName; i++) { | |
string = ` ${temp} ` | |
temp = string | |
} | |
return `${this.#specialCharacter}${temp}${this.#specialCharacter}` | |
} | |
/** | |
* prints name inisde the box | |
* | |
* magic happens here | |
* | |
* @returns {String} | |
*/ | |
printNameBox() { | |
let string = '' | |
const newline = '\n' | |
string += this.getBaseLine() + newline | |
for (let i = 0; i < (this.#numberOfLines - 1) / 2; i++) { | |
string += this.returnNonNameLine() + newline | |
} | |
string += this.getNameLine() + newline | |
for (let i = 0; i < (this.#numberOfLines - 1) / 2; i++) { | |
string += this.returnNonNameLine() + newline | |
} | |
string += this.getBaseLine() | |
console.log(string) | |
} | |
} | |
function main() { | |
obj0 = new NameBox('Kiran'); | |
obj0.printNameBox() | |
obj1 = new NameBox('Chyawanprash Harilal', 10, 11, '*') | |
obj1.printNameBox() | |
obj1 = new NameBox('Kileshowr Baba', 40, 19, '!') | |
obj1.printNameBox() | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment