Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
114 changes: 114 additions & 0 deletions test/integration/isAlpha.test.js
Original file line number Diff line number Diff line change
@@ -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.');
});
});
46 changes: 46 additions & 0 deletions test/unit/isAlpha.test.js
Original file line number Diff line number Diff line change
@@ -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
});
});
21 changes: 21 additions & 0 deletions validationFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down