Skip to content

Commit ddc6b24

Browse files
committed
Added findHttpStatus
1 parent b673c1f commit ddc6b24

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/find-http-status.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import HTTP_STATUSES from './constants/http-statuses';
2+
3+
function findHttpStatus(value, field = 'code') {
4+
return HTTP_STATUSES.find(httpStatus => httpStatus[field] === value);
5+
}
6+
7+
export default findHttpStatus;

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { default as isCreated } from './is-created';
22
export { default as isOk } from './is-ok';
3+
export { default as findHttpStatus } from './find-http-status';

tests/find-http-status.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { findHttpStatus } from '../src';
2+
3+
describe('findHttpStatus', () => {
4+
test('it should find http status by code', () => {
5+
const found = findHttpStatus(200);
6+
const mockResponse = {
7+
code: 200,
8+
key: 'OK',
9+
message: 'OK',
10+
category: 'SUCCESS'
11+
};
12+
13+
expect(found).toEqual(mockResponse);
14+
});
15+
16+
test('it should find http status by key', () => {
17+
const found = findHttpStatus('TOO_MANY_REQUESTS', 'key');
18+
const mockResponse = {
19+
code: 429,
20+
key: 'TOO_MANY_REQUESTS',
21+
message: 'Too Many Requests',
22+
category: 'CLIENT_ERROR'
23+
};
24+
25+
expect(found).toEqual(mockResponse);
26+
});
27+
});

0 commit comments

Comments
 (0)