Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding validator function #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
16 changes: 15 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as HttpStatusV1 from '../test/v1';
import * as HttpStatusV2 from './index';
import {
getReasonPhrase, getStatusCode, ReasonPhrases, StatusCodes,
getReasonPhrase, getStatusCode, ReasonPhrases, StatusCodes, validateStatusCode,
} from './index';
import codes from '../codes.json';

Expand Down Expand Up @@ -87,3 +87,17 @@ describe('v2', () => {
expect(() => { getStatusCode('blah blah'); }).toThrowError(/Reason phrase does not exist: blah blah/);
});
});

describe('validateStatusCode', () => {
test('Valid status code', () => {
expect(validateStatusCode(200)).toBe(true);
expect(validateStatusCode(404)).toBe(true);
expect(validateStatusCode(500)).toBe(true);
});

test('Invalid status code', () => {
expect(validateStatusCode(0)).toBe(false);
expect(validateStatusCode(1000)).toBe(false);
expect(validateStatusCode(-200)).toBe(false);
});
});
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import legacyCodes from './legacy';
import {
getStatusCode,
getStatusText,
validateStatusCode,
} from './utils-functions';

export {
getStatusCode,
getReasonPhrase,
getStatusText,
validateStatusCode,
} from './utils-functions';

export {
Expand All @@ -25,4 +27,5 @@ export default {
...legacyCodes,
getStatusCode,
getStatusText,
validateStatusCode,
};
10 changes: 10 additions & 0 deletions src/utils-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ export function getStatusCode(reasonPhrase: string): (number) {
* @returns {string|undefined} The associated reason phrase (e.g. "Bad Request", "OK")
* */
export const getStatusText = getReasonPhrase;

/**
* Returns true if the given status code is a valid HTTP status code.
*
* @param {number} statusCode The HTTP status code
* @returns {boolean} True if the status code is valid, false otherwise
* */
export function validateStatusCode(statusCode: number): boolean {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a type guard and refine the type (statusCode is StatusCodes rather than boolean).

return Object.values(reasonPhraseToStatusCode).includes(statusCode);
}