-
Notifications
You must be signed in to change notification settings - Fork 11
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
fix(utils): isInRange, range 로직 개선 및 한글화 #554
Merged
Changes from all commits
Commits
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
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,5 @@ | ||
--- | ||
'@modern-kit/utils': patch | ||
--- | ||
|
||
fix(utils): isInRange, range 로직 개선 - @ssi02014 |
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
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
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
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 |
---|---|---|
@@ -1,62 +1,46 @@ | ||
import { isNil } from '../../validator'; | ||
|
||
interface IsInRangeProps { | ||
value: number; | ||
min: number; | ||
max: number; | ||
equalOptions?: { | ||
min?: boolean; | ||
max?: boolean; | ||
}; | ||
inclusiveMin?: boolean; | ||
inclusiveMax?: boolean; | ||
} | ||
|
||
/** | ||
* @description 값이 지정된 범위 내에 있는지 확인합니다. | ||
* @description 주어진 value가 `min`과 `max`로 지정된 범위 내에 있는지 여부를 판단합니다. | ||
* | ||
* 이 함수는 주어진 값이 `min`과 `max`로 지정된 범위 내에 있는지 여부를 판단합니다. | ||
* `min`과 `max`가 유효한 값이어야 하며, `min`이 `max`보다 클 수 없습니다. | ||
* `equalOptions`를 통해 경계 값을 포함할지 여부를 설정할 수 있습니다. | ||
* `inclusiveMin`/`inclusiveMax`를 통해 경계 값을 포함할지 여부를 설정할 수 있습니다. 기본적으로 최소값은 포함하며 최대값은 포함하지 않습니다. | ||
* | ||
* @param {{ | ||
* value: number; | ||
* min: number; | ||
* max: number; | ||
* equalOptions?: { | ||
* min?: boolean; | ||
* max?: boolean; | ||
* }; | ||
* }} | ||
* - `value`: 확인할 값. | ||
* - `min`: 값이 포함되어야 하는 최소값. | ||
* - `max`: 값이 포함되어야 하는 최대값. | ||
* - `equalOptions.min`: `true`일 경우 최소값을 포함합니다. | ||
* - `equalOptions.max`: `true`일 경우 최대값을 포함합니다. | ||
* @param {IsInRangeProps} - isInRange 함수의 옵션 객체입니다. | ||
* @property {number} value - 확인할 값. | ||
* @property {number} min - 값이 포함되어야 하는 최소값. | ||
* @property {number} max - 값이 포함되어야 하는 최대값. | ||
* @property {boolean} inclusiveMin - `true`일 경우 최소값을 포함합니다. 기본 값은 true 입니다. | ||
* @property {boolean} inclusiveMax - `true`일 경우 최대값을 포함합니다. 기본 값은 false 입니다. | ||
* @returns {boolean} - 값이 지정된 범위 내에 있으면 `true`, 그렇지 않으면 `false`를 반환합니다. | ||
* | ||
* @throws {Error} - `min` 또는 `max` 값이 유효하지 않거나, `min`이 `max`보다 큰 경우 오류가 발생합니다. | ||
* | ||
* @example | ||
* isInRange({ value: 5, min: 1, max: 10 }); // true | ||
* isInRange({ value: 10, min: 1, max: 10 }); // false | ||
* | ||
* @example | ||
* isInRange({ value: 10, min: 1, max: 10, inclusiveMax: true }); // true | ||
*/ | ||
export function isInRange({ | ||
value, | ||
min, | ||
max, | ||
equalOptions = {}, | ||
inclusiveMin = true, | ||
inclusiveMax = false, | ||
}: IsInRangeProps): boolean { | ||
if (isNil(min) || isNil(max)) { | ||
throw new Error('min and max values are invalid.'); | ||
if (min >= max) { | ||
throw new Error('최소값은 최대값보다 크거나 같은 수 없습니다.'); | ||
} | ||
|
||
if (min > max) { | ||
throw new Error('min value cannot be greater than the max value.'); | ||
} | ||
|
||
const { min: minEqual = true, max: maxEqual = false } = equalOptions; | ||
|
||
const isWithinMin = minEqual ? value >= min : value > min; | ||
const isWithinMax = maxEqual ? value <= max : value < max; | ||
const isInclusiveMin = inclusiveMin ? value >= min : value > min; | ||
const isInclusiveMax = inclusiveMax ? value <= max : value < max; | ||
|
||
return isWithinMin && isWithinMax; | ||
return isInclusiveMin && isInclusiveMax; | ||
} |
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,13 @@ | ||
import { bench, describe } from 'vitest'; | ||
import { inRange as inRangeLodash } from 'lodash-es'; | ||
import { isInRange } from '.'; | ||
|
||
describe('isInRange', () => { | ||
bench('@modern-kit/isInRange', () => { | ||
isInRange({ value: 10, min: 0, max: 10 }); | ||
}); | ||
|
||
bench('lodash/inRange', () => { | ||
inRangeLodash(10, 0, 10); | ||
}); | ||
}); |
Oops, something went wrong.
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.
타입스크립트로 작성되어 min, max 값을 검증하는 것은 불필요해 보여 제거합니다!