File tree Expand file tree Collapse file tree 4 files changed +71
-3
lines changed Expand file tree Collapse file tree 4 files changed +71
-3
lines changed Original file line number Diff line number Diff line change @@ -5,9 +5,15 @@ import { HttpReasonPhrases } from '../HttpReasonPhrases';
5
5
6
6
describe ( 'getReasonPhrase' , function ( ) {
7
7
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
+ } ) ;
11
17
} ) ;
12
18
it ( '# Returns undefined for non-existent status code' , function ( ) {
13
19
expect ( getReasonPhrase ( 579 ) ) . to . equal ( undefined ) ;
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change @@ -3,5 +3,6 @@ export * from './isSuccessStatus';
3
3
export * from './isRedirectionStatus' ;
4
4
export * from './isClientErrorStatus' ;
5
5
export * from './isServerErrorStatus' ;
6
+ export { default as getStatusCode } from './getStatusCode' ;
6
7
export { default as getReasonPhrase } from './getReasonPhrase' ;
7
8
export { default as getCompositeStatus } from './getCompositeStatus' ;
You can’t perform that action at this time.
0 commit comments