Skip to content

Commit e1cd649

Browse files
authored
Merge pull request #95 from js-thing/issue_94
added new helper function: getStatusCode
2 parents 3c6c04f + 1f21f24 commit e1cd649

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

src/helpers/getReasonPhrase.spec.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ import { HttpReasonPhrases } from '../HttpReasonPhrases';
55

66
describe('getReasonPhrase', function () {
77
it('# Returns the corresponding reason phrase for a valid defined status code', function () {
8-
expect(getReasonPhrase(HttpStatusCodes.Ok)).to.equal(
9-
HttpReasonPhrases.Ok
10-
);
8+
Object.entries(HttpStatusCodes).forEach((statusCodeKeyVal) => {
9+
if (typeof statusCodeKeyVal[1] === 'number') {
10+
expect(getReasonPhrase(statusCodeKeyVal[1])).to.equal(
11+
(HttpReasonPhrases as { [key: string]: string })[
12+
statusCodeKeyVal[0]
13+
]
14+
);
15+
}
16+
});
1117
});
1218
it('# Returns undefined for non-existent status code', function () {
1319
expect(getReasonPhrase(579)).to.equal(undefined);

src/helpers/getStatusCode.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { expect } from 'chai';
2+
import getStatusCode from './getStatusCode';
3+
import { HttpStatusCodes } from '../HttpStatusCodes';
4+
import { HttpReasonPhrases } from '../HttpReasonPhrases';
5+
6+
describe('getStatusCode', function () {
7+
it('# Returns the corresponding status code for a valid defined reason phrase', function () {
8+
Object.entries(HttpReasonPhrases).forEach((reasonPhraseKeyVal) => {
9+
expect(getStatusCode(reasonPhraseKeyVal[1])).to.equal(
10+
(HttpStatusCodes as { [key: string]: number | string })[
11+
reasonPhraseKeyVal[0]
12+
]
13+
);
14+
expect(
15+
getStatusCode(reasonPhraseKeyVal[1].toUpperCase(), true)
16+
).to.equal(
17+
(HttpStatusCodes as { [key: string]: number | string })[
18+
reasonPhraseKeyVal[0]
19+
]
20+
);
21+
});
22+
});
23+
it('# Returns undefined for non-existent reason phrase', function () {
24+
expect(getStatusCode('')).to.equal(undefined);
25+
expect(getStatusCode('hello')).to.equal(undefined);
26+
});
27+
});

src/helpers/getStatusCode.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { HttpStatusCodes } from '../HttpStatusCodes';
2+
import { HttpReasonPhrases } from '../HttpReasonPhrases';
3+
4+
/**
5+
* Get the status code for a given reason phrase.
6+
* Optionally pass a second parameter to ignore case.
7+
*
8+
* @param reasonPhrase - e.g. OK
9+
* @param ignoreCase - ignore case while comparing, default: false
10+
* @returns - The corresponding status code as `number` for the given input `undefined` otherwise
11+
*/
12+
export default (
13+
reasonPhrase: string,
14+
ignoreCase = false
15+
): number | undefined => {
16+
if (!reasonPhrase) {
17+
return undefined;
18+
}
19+
const foundReasonKeyVal = Object.entries(
20+
HttpReasonPhrases as { [key: string]: string }
21+
).find((reasonKeyVal) => {
22+
if (ignoreCase) {
23+
return reasonKeyVal[1].toLowerCase() === reasonPhrase.toLowerCase();
24+
}
25+
return reasonKeyVal[1] === reasonPhrase;
26+
});
27+
if (!foundReasonKeyVal) {
28+
return undefined;
29+
}
30+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
31+
return ((<any>HttpStatusCodes) as { [key: string]: number })[
32+
foundReasonKeyVal[0]
33+
];
34+
};

src/helpers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export * from './isSuccessStatus';
33
export * from './isRedirectionStatus';
44
export * from './isClientErrorStatus';
55
export * from './isServerErrorStatus';
6+
export { default as getStatusCode } from './getStatusCode';
67
export { default as getReasonPhrase } from './getReasonPhrase';
78
export { default as getCompositeStatus } from './getCompositeStatus';

0 commit comments

Comments
 (0)