diff --git a/server.js b/server.js index 137dd90..bae63b2 100644 --- a/server.js +++ b/server.js @@ -506,6 +506,37 @@ app.post('/api/onlyLetters', (req, res) => { res.json({ result }); }); +/** + * POST /api/isAlpha + * @summary Returns true if the input string contains only alphabetical characters (letters), otherwise false + * @description This endpoint checks whether the provided input string contains only letters (both uppercase and lowercase). It returns `true` if the string contains only alphabetical characters, and `false` otherwise. + * @param {BasicRequest} request.body.required - The input string to check + * @return {BasicResponse} 200 - Success response with a boolean result + * @return {BadRequestResponse} 400 - Bad request response when the input string is missing + * @example request - test + * { + * "inputString": "hello" + * } + * @example response - 200 - example payload + * { + * "result": true + * } + * @example response - 400 - example + * { + * "error": "Input string required as a parameter." + * } + */ +app.post('/api/isAlpha', (req, res) => { + const { inputString } = req.body; + + if (!inputString) { + return res.status(400).json({ error: requiredParameterResponse }); + } + + const result = ValidationFunctions.isAlpha(inputString); + res.json({ result }); +}); + /** * POST /api/excludeTheseCharacters * @summary Removes the specified characters from the input string diff --git a/test/integration/isAlpha.test.js b/test/integration/isAlpha.test.js new file mode 100644 index 0000000..9e69df5 --- /dev/null +++ b/test/integration/isAlpha.test.js @@ -0,0 +1,114 @@ +const request = require('supertest'); +const app = require('../../server.js'); + +describe('POST /api/isAlpha', () => { + it('should return true if the input string contains only letters', async () => { + const response = await request(app) + .post('/api/isAlpha') + .send({ + inputString: 'hello' + }) + .expect(200); + + expect(response.body).toHaveProperty('result', true); + }); + + it('should return true for mixed case letters', async () => { + const response = await request(app) + .post('/api/isAlpha') + .send({ + inputString: 'HelloWorld' + }) + .expect(200); + + expect(response.body).toHaveProperty('result', true); + }); + + it('should return true for uppercase letters only', async () => { + const response = await request(app) + .post('/api/isAlpha') + .send({ + inputString: 'ABC' + }) + .expect(200); + + expect(response.body).toHaveProperty('result', true); + }); + + it('should return false if the input string contains numbers', async () => { + const response = await request(app) + .post('/api/isAlpha') + .send({ + inputString: 'hello123' + }) + .expect(200); + + expect(response.body).toHaveProperty('result', false); + }); + + it('should return false if the input string contains spaces', async () => { + const response = await request(app) + .post('/api/isAlpha') + .send({ + inputString: 'hello world' + }) + .expect(200); + + expect(response.body).toHaveProperty('result', false); + }); + + it('should return false if the input string contains special characters', async () => { + const response = await request(app) + .post('/api/isAlpha') + .send({ + inputString: 'hello!world' + }) + .expect(200); + + expect(response.body).toHaveProperty('result', false); + }); + + it('should return 400 for empty strings', async () => { + const response = await request(app) + .post('/api/isAlpha') + .send({ inputString: '' }) + .expect(400); + + expect(response.body).toHaveProperty('error'); + expect(response.body.error).toBe('Input string required as a parameter.'); + }); + + it('should return 400 if inputString is missing', async () => { + const response = await request(app) + .post('/api/isAlpha') + .send({}) + .expect(400); + + expect(response.body).toHaveProperty('error'); + expect(response.body.error).toBe('Input string required as a parameter.'); + }); + + it('should return 400 if inputString is null', async () => { + const response = await request(app) + .post('/api/isAlpha') + .send({ + inputString: null + }) + .expect(400); + + expect(response.body).toHaveProperty('error'); + expect(response.body.error).toBe('Input string required as a parameter.'); + }); + + it('should return 400 if inputString is undefined', async () => { + const response = await request(app) + .post('/api/isAlpha') + .send({ + inputString: undefined + }) + .expect(400); + + expect(response.body).toHaveProperty('error'); + expect(response.body.error).toBe('Input string required as a parameter.'); + }); +}); diff --git a/test/unit/isAlpha.test.js b/test/unit/isAlpha.test.js new file mode 100644 index 0000000..9093b58 --- /dev/null +++ b/test/unit/isAlpha.test.js @@ -0,0 +1,46 @@ +const { isAlpha } = require('../../validationFunctions'); + +describe('isAlpha', () => { + it('should return true for strings containing only letters', () => { + expect(isAlpha('hello')).toBe(true); // Lowercase letters only + expect(isAlpha('HelloWorld')).toBe(true); // Mixed case letters + expect(isAlpha('ABC')).toBe(true); // Uppercase letters only + expect(isAlpha('a')).toBe(true); // Single letter + }); + + it('should return false for strings containing numbers', () => { + expect(isAlpha('hello123')).toBe(false); // Contains numbers + expect(isAlpha('123hello')).toBe(false); // Starts with numbers + expect(isAlpha('hello123world')).toBe(false); // Numbers in middle + }); + + it('should return false for strings containing spaces', () => { + expect(isAlpha('hello world')).toBe(false); // Contains space + expect(isAlpha(' hello')).toBe(false); // Leading space + expect(isAlpha('hello ')).toBe(false); // Trailing space + }); + + it('should return false for strings containing special characters', () => { + expect(isAlpha('hello!')).toBe(false); // Contains exclamation mark + expect(isAlpha('hello@world')).toBe(false); // Contains at symbol + expect(isAlpha('hello#world')).toBe(false); // Contains hash + expect(isAlpha('hello-world')).toBe(false); // Contains hyphen + expect(isAlpha('hello_world')).toBe(false); // Contains underscore + }); + + it('should return false for empty strings', () => { + expect(isAlpha('')).toBe(false); // Empty string + }); + + it('should return false for strings with only special characters', () => { + expect(isAlpha('!@#')).toBe(false); // Only special characters + expect(isAlpha('123')).toBe(false); // Only numbers + expect(isAlpha(' ')).toBe(false); // Only spaces + }); + + it('should return false for strings with mixed content', () => { + expect(isAlpha('hello123world')).toBe(false); // Letters and numbers + expect(isAlpha('hello world!')).toBe(false); // Letters, spaces, and special characters + expect(isAlpha('123hello@world')).toBe(false); // Numbers, letters, and special characters + }); +}); diff --git a/validationFunctions.js b/validationFunctions.js index 94157ad..5c2ee6a 100644 --- a/validationFunctions.js +++ b/validationFunctions.js @@ -35,6 +35,27 @@ module.exports = class ValidationFunctions { return str.replace(/[^a-zA-Z]/g, ''); } + /** + * Checks if the given string contains only alphabetical characters (letters). + * + * This function validates whether the input string contains only letters (both uppercase and lowercase). + * It will return `true` if the string contains only alphabetical characters, and `false` otherwise. + * + * @param {string} str - The string to be checked. + * @returns {boolean} - Returns `true` if the string contains only letters, `false` otherwise. + * + * @example + * isAlpha('hello'); // Returns true + * isAlpha('HelloWorld'); // Returns true + * isAlpha('hello123'); // Returns false (contains numbers) + * isAlpha('hello world'); // Returns false (contains space) + * isAlpha('hello!'); // Returns false (contains special character) + */ + static isAlpha(str) { + const alphaRegex = /^[a-zA-Z]+$/; + return alphaRegex.test(str); + } + /** * Removes all alphanumberic characters and spaces, leaving only special characters. *