-
Notifications
You must be signed in to change notification settings - Fork 5
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
[실습] 연습문제 week1/URL/addQuery 제출합니다 #16
Open
minsoo-web
wants to merge
4
commits into
main
Choose a base branch
from
practice/1week-url-addQuery-minsoo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+166
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
packages/example/src/1week/URL/addQuery/__submit__/minsoo-web.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { addQuery, getHashPart, replaceKeyValue } from './minsoo-web' | ||
|
||
describe('getHashPart', () => { | ||
it('case 1', () => { | ||
expect(getHashPart('minsoo#foo')).toEqual({ | ||
nonHash: 'minsoo', | ||
hash: '#foo', | ||
}) | ||
}) | ||
|
||
it('case 2', () => { | ||
expect(getHashPart('http://www.minsoo#foo')).toEqual({ | ||
nonHash: 'http://www.minsoo', | ||
hash: '#foo', | ||
}) | ||
}) | ||
|
||
it('case 3', () => { | ||
expect(getHashPart('http://www.minsoo')).toEqual({ | ||
nonHash: 'http://www.minsoo', | ||
hash: '', | ||
}) | ||
}) | ||
|
||
it('case 4: 일반적이진 않지만, 의도치 않게 해시가 여러개 들어가있는 경우', () => { | ||
expect(getHashPart('http://www.minsoo#foo#minsoo')).toEqual({ | ||
nonHash: 'http://www.minsoo', | ||
hash: '#foo#minsoo', | ||
}) | ||
}) | ||
}) | ||
|
||
describe('replaceKeyValue', () => { | ||
it('case 1', () => { | ||
expect(replaceKeyValue('minsoo?key=foo', 'key', 'bar')).toBe( | ||
'minsoo?key=bar', | ||
) | ||
}) | ||
|
||
it('case 2', () => { | ||
expect(replaceKeyValue('minsoo?foo=foo', 'key', 'bar')).toBe( | ||
'minsoo?foo=foo', | ||
) | ||
}) | ||
|
||
it('case 3', () => { | ||
expect(replaceKeyValue('minsoo?key=foo&key=baz', 'key', 'bar')).toBe( | ||
'minsoo?key=bar&key=bar', | ||
) | ||
}) | ||
}) | ||
|
||
describe('addQuery', () => { | ||
test('case 0: URL 형식이 굳이 아니여도 동작', () => { | ||
expect(addQuery('www.linkedin.com/?name=elon#top', 'name', 'musk')).toBe( | ||
'www.linkedin.com/?name=musk#top', | ||
) | ||
}) | ||
|
||
test('case 1', () => { | ||
expect(addQuery('https://www.linkedin.com/', 'key', 'value')).toBe( | ||
'https://www.linkedin.com/?key=value', | ||
) | ||
}) | ||
|
||
test('case 2: 기존 URL에 이미 쿼리가 있는 경우', () => { | ||
expect( | ||
addQuery('https://www.linkedin.com/?name=elon', 'key', 'value'), | ||
).toBe('https://www.linkedin.com/?name=elon&key=value') | ||
}) | ||
|
||
test('case 3: 기존 URL에 hash가 있는 경우', () => { | ||
expect( | ||
addQuery('https://www.linkedin.com/?name=elon#top', 'key', 'value'), | ||
).toBe('https://www.linkedin.com/?name=elon&key=value#top') | ||
}) | ||
|
||
test('case 4: 기존 쿼리와 추가하려는 쿼리의 key가 동일한 경우', () => { | ||
const URL = 'https://www.linkedin.com/?name=elon#top' | ||
|
||
expect(addQuery(URL, 'name', 'musk')).toBe( | ||
'https://www.linkedin.com/?name=musk#top', | ||
) | ||
|
||
expect(addQuery(URL, 'name', 'musk')).toBe( | ||
'https://www.linkedin.com/?name=musk#top', | ||
) | ||
}) | ||
|
||
test('case 5: 기존 쿼리와 추가하려는 쿼리의 key가 동일한 경우', () => { | ||
const URL = 'https://www.linkedin.com/?name=elon&name=minsoo#top' | ||
|
||
expect(addQuery(URL, 'name', 'musk')).toBe( | ||
'https://www.linkedin.com/?name=musk&name=musk#top', | ||
) | ||
|
||
expect(addQuery(URL, 'name', 'musk')).toBe( | ||
'https://www.linkedin.com/?name=musk&name=musk#top', | ||
) | ||
}) | ||
}) |
65 changes: 65 additions & 0 deletions
65
packages/example/src/1week/URL/addQuery/__submit__/minsoo-web.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const hasIncludes = (url: string, value: string): 'YES' | 'NO' => | ||
url.includes(value) ? 'YES' : 'NO' | ||
|
||
export const getHashPart = ( | ||
text: string, | ||
): { nonHash: string; hash: string } => { | ||
const indexOfHash = text.indexOf('#') | ||
|
||
if (indexOfHash >= 0) { | ||
return { | ||
nonHash: text.slice(0, indexOfHash), | ||
hash: text.slice(indexOfHash), | ||
} | ||
} | ||
|
||
return { | ||
nonHash: text, | ||
hash: '', | ||
} | ||
} | ||
|
||
export const replaceKeyValue = ( | ||
url: string, | ||
key: string, | ||
newValue: string | number, | ||
) => { | ||
const regExp = new RegExp(`${key}=([^&]*)`, 'g') | ||
if (url.match(regExp)) { | ||
return url.replace(regExp, `${key}=${newValue}`) | ||
} | ||
|
||
return url | ||
} | ||
|
||
/** | ||
* 출제자: 예진님 (yejineee) | ||
* URL의 쿼리스트링 관련 유틸 함수를 계산 을 최대한 이용하여 만들기 | ||
* | ||
* - 해쉬가 있다면, 유지해야 함 | ||
* - 기존 쿼리가 있다면, 유지해야 함 | ||
* - 기존 쿼리와 추가하려는 쿼리의 key가 동일하다면, 기존 쿼리가 대체됨 | ||
*/ | ||
|
||
export const addQuery = ( | ||
originURL: string, | ||
key: string, | ||
value: string | number, | ||
) => { | ||
const { nonHash, hash } = getHashPart(originURL) | ||
|
||
let returnUrl = nonHash | ||
let connector = '?' | ||
|
||
if (hasIncludes(originURL, '?') === 'YES') { | ||
connector = '&' | ||
} | ||
|
||
returnUrl += `${connector}${key}=${value}` | ||
|
||
if (hasIncludes(originURL, `?${key}`) === 'YES') { | ||
returnUrl = replaceKeyValue(nonHash, key, value) | ||
} | ||
|
||
return returnUrl + hash | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
위와 같은 요구사항이라면 set이라는 이름을 쓰면 어떨까요? 뭔가 set이 더 어울리는 것 같아요
addQuery라면 queryParam을 배열로 사용하는 경우로 오해받을 수도 있을 거 같아보여서요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오,, 저두 이거 약간 헷갈리는 것 같긴했는데 출제자인 예진님이 함수 이름을 정해주신거라! 예진님 의견도 궁금합니다!