Skip to content

Commit

Permalink
refactor: helper function for transform url
Browse files Browse the repository at this point in the history
  • Loading branch information
sahsisunny committed Dec 20, 2023
1 parent 057525b commit 4c79659
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
17 changes: 0 additions & 17 deletions __tests__/Unit/utils/transformGitHubApiUrl.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
import extractRepoName from '../../../src/utils/extractRepoName';
import {
extractRepoName,
transformGitHubApiUrl,
} from '../../../src/utils/urlTransform';

describe('Unit | Util | Transform GitHub Api Url', () => {
test('returns proper value with proper input', () => {
expect(
transformGitHubApiUrl(
'https://api.github.com/repos/Real-Dev-Squad/todo-action-items/issues/196'
)
).toEqual(
'https://github.com/Real-Dev-Squad/todo-action-items/issues/196'
);
});

test('returns # with improper input', () => {
expect(transformGitHubApiUrl('')).toEqual('#');
});
});

describe('Unit | Util | Extract Repo Name', () => {
test('returns proper value with proper input', () => {
Expand Down
3 changes: 1 addition & 2 deletions src/components/taskDetails/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import React, { FC } from 'react';
import setColor from './taskPriorityColors';
import classNames from './task-details.module.scss';
import { TaskDetailsProps } from '@/interfaces/taskDetails.type';
import extractRepoName from '@/utils/extractRepoName';
import transformGitHubApiUrl from '@/utils/transformGitHubApiUrl';
import { transformGitHubApiUrl, extractRepoName } from '@/utils/urlTransform';

const Details: FC<TaskDetailsProps> = ({ detailType, value }) => {
const color = value ? setColor?.[value] : undefined;
Expand Down
11 changes: 0 additions & 11 deletions src/utils/transformGitHubApiUrl.ts

This file was deleted.

12 changes: 11 additions & 1 deletion src/utils/extractRepoName.ts → src/utils/urlTransform.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
function transformGitHubApiUrl(apiUrl?: string) {
if (!apiUrl) return '#';
const transformedUrl = apiUrl.replace(
/https:\/\/api\.github\.com\/repos\//,
'https://github.com/'
);

return transformedUrl;
}

function extractRepoName(issueUrl: string) {
const match = issueUrl.match(
/(?:https:\/\/github\.com\/([^/]+)\/([^/]+)\/issues\/(\d+))|(?:https:\/\/api\.github\.com\/repos\/([^/]+)\/([^/]+)\/issues\/(\d+))/
Expand All @@ -12,4 +22,4 @@ function extractRepoName(issueUrl: string) {
return null;
}

export default extractRepoName;
export { transformGitHubApiUrl, extractRepoName };

0 comments on commit 4c79659

Please sign in to comment.