-
Notifications
You must be signed in to change notification settings - Fork 9
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
Feat(utils): isSubset #309
Merged
Merged
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
5bfe36c
feat(utils): isSubset 생성
beberiche 8accd69
text(utils): isSubset 함수 테스트 파일 생성
beberiche cf901f7
feat(utils): isSubset 의 배열 요소가 참조형인 경우를 위한 deepEqual 적용
beberiche 65e0fef
test(utils): isSubset deepEqual 적용에 대한 테스트 재조정
beberiche 5e3f0b3
feat(utils): validator 폴더의 index.ts에 isSubset export 항목 추가
beberiche d630eef
docs(utils): isSubset.md 생성
beberiche 1731597
fix(utils): isSubset 함수 매개변수 타입 에러 개선
beberiche 53144df
feat(utils): deepEqual 코드 삭제. (부분집합의 정의를 사용자에게 위임)
beberiche 84c855b
test(utils): isSubset 함수 수정에 대한 test 케이스 변경
beberiche ac53f8e
docs(utils): isSubset 함수 코드 수정사항에 맞게 docs 내용 변경
beberiche 67f83fb
test(utils): 테스트 실패요소 개선
beberiche a9439fb
refactor(utils): isSubset 함수 재구현 (utils/array/difference 재사용)
beberiche 7de0806
test(utils): isSubset 함수 재구현에 따른 test 내용 수정
beberiche 6a62323
docs(utils): isSubset 함수 재구현에 따른 설명 내용 수정
beberiche c473786
Update packages/utils/src/validator/isSubset/index.ts
ssi02014 f31e24d
Update docs/docs/utils/validator/isSubset.md
ssi02014 a4c479f
Update docs/docs/utils/validator/isSubset.md
ssi02014 0e4f0da
Update docs/docs/utils/validator/isSubset.md
ssi02014 52047c7
Update docs/docs/utils/validator/isSubset.md
ssi02014 9308730
Create tidy-guests-clean.md
ssi02014 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,90 @@ | ||
# isSubset | ||
|
||
두번째 인자로 주어지는 배열의 모든 요소를 첫번째 인자의 배열이 완전히 포함하는지(부분집합) 에 대한 여부 `boolean` 를 반환합니다. | ||
|
||
배열 요소의 타입이 참조형인 경우, 깊은 비교를 진행하며, `iteratee` 함수 인자를 정의하여 비교항목을 설정하는 것이 가능합니다. | ||
|
||
<br /> | ||
|
||
|
||
|
||
## Code | ||
[🔗 실제 구현 코드 확인](https://github.com/modern-agile-team/modern-kit/blob/main/packages/utils/src/validator/isSubset/index.ts) | ||
|
||
## Interface | ||
```ts title="typescript" | ||
const isSubset = <T, U>( | ||
parentArray: readonly T[], | ||
childArray: readonly T[], | ||
iteratee?: (item: T) => U | ||
) => boolean; | ||
``` | ||
|
||
## Usage | ||
```ts title="typescript" | ||
import { isSubset } from '@modern-kit/utils'; | ||
|
||
const parentArray = [1, 2, 3, 4]; | ||
const childArray1 = [1, 3]; | ||
const childArray2 = [1, 5]; | ||
|
||
console.log(isSubset(parentArray, childArray1)); // true | ||
console.log(isSubset(parentArray, childArray2)); // false | ||
``` | ||
|
||
```ts title="typescript" | ||
import { isSubset } from '@modern-kit/utils'; | ||
|
||
const parentArray = ['1', 2, 3, 4]; | ||
const childArray1 = ['1', 2, 3]; | ||
const childArray2 = [1, '2', 3]; | ||
|
||
console.log(isSubset(parentArray, childArray1)); // true | ||
console.log(isSubset(parentArray, childArray2)); // false | ||
``` | ||
|
||
```ts title="typescript" | ||
// 요소 타입이 배열인 경우 | ||
import { isSubset } from '@modern-kit/utils'; | ||
|
||
const parentArray = [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]; | ||
const childArray1 = [[0, 1, 2, 3, 4]]; | ||
const childArray2 = [[0, 1, 7, 4, 9]]; | ||
|
||
isSubset(parentArray, childArray1); // false, 요소가 참조형(배열)이므로, 주소값이 달라 false를 반환한다. | ||
isSubset(parentArray, childArray2, (obj) => obj[2]); // true ([2,7], [7]) | ||
isSubset(parentArray, childArray2, (obj) => obj[3]); // false ([3,8], [4]) | ||
``` | ||
|
||
```ts title="typescript | ||
// 깊이가 2 이상의 오브젝트 | ||
import { isSubset } from '@modern-kit/utils'; | ||
const parentArray = [ | ||
{ | ||
name: 'Peter', | ||
age: 13, | ||
}, | ||
{ | ||
name: 'Aimee', | ||
age: 25, | ||
}, | ||
]; | ||
|
||
const childArray1 = [ | ||
{ | ||
name: 'Aimee', | ||
age: 25, | ||
}, | ||
]; | ||
|
||
const childArray2 = [ | ||
{ | ||
name: 'Peter', | ||
age: 15, | ||
}, | ||
]; | ||
|
||
isSubset(parentArray, childArray1); // false, 요소가 참조형(객체)이므로, 주소값이 달라 false를 반환한다 | ||
isSubset(parentArray, childArray1, (obj) => JSON.stringify(obj)); // true | ||
isSubset(parentArray, childArray2, (obj) => JSON.stringify(obj)); // false | ||
isSubset(parentArray, childArray2, (obj) => obj.name); // true |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const isSubset = <T, U>( | ||
parentArray: readonly T[], | ||
childArray: readonly T[], | ||
iteratee?: (item: T) => U | ||
) => { | ||
const baseArray = iteratee ? parentArray.map(iteratee) : parentArray; | ||
const cmpArray = iteratee ? childArray.map(iteratee) : childArray; | ||
|
||
const baseSet = new Set<T | U>(baseArray); | ||
|
||
return (cmpArray as any).every((el: T | U) => baseSet.has(el)); | ||
}; |
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,70 @@ | ||
import { isSubset } from '.'; | ||
|
||
describe('isSubset', () => { | ||
it('if the childArray is a subset of the parentArray', () => { | ||
const parentArray = [1, 2, 3, 4]; | ||
const childArray1 = [1, 3]; | ||
const childArray2 = [1, 5]; | ||
|
||
expect(isSubset(parentArray, childArray1)).toBeTruthy(); | ||
expect(isSubset(parentArray, childArray2)).toBeFalsy(); | ||
}); | ||
|
||
it('if the type is dfferent between comparison elements', () => { | ||
const parentArray = ['1', 2, 3, 4]; | ||
const childArray1 = ['1', 2, 3]; | ||
const childArray2 = [1, '2', 3]; | ||
|
||
expect(isSubset(parentArray, childArray1)).toBeTruthy(); | ||
expect(isSubset(parentArray, childArray2)).toBeFalsy(); | ||
expect(isSubset(parentArray, childArray2, (el) => Number(el))).toBeTruthy(); | ||
}); | ||
|
||
it('if elements type is array', () => { | ||
const parentArray = [ | ||
[0, 1, 2, 3, 4], | ||
[5, 6, 7, 8, 9], | ||
]; | ||
const childArray = [[0, 1, 7, 4, 9]]; | ||
|
||
expect(isSubset(parentArray, childArray)).toBeFalsy(); | ||
expect(isSubset(parentArray, childArray, (obj) => obj[2])).toBeTruthy(); // [2,7], [7]; | ||
expect(isSubset(parentArray, childArray, (obj) => obj[3])).toBeFalsy(); // [3,8], [4] | ||
}); | ||
|
||
it('if elements type is reference', () => { | ||
const parentArray = [ | ||
{ | ||
name: 'Peter', | ||
age: 13, | ||
}, | ||
{ | ||
name: 'Aimee', | ||
age: 25, | ||
}, | ||
]; | ||
|
||
const childArray1 = [ | ||
{ | ||
name: 'Aimee', | ||
age: 25, | ||
}, | ||
]; | ||
|
||
const childArray2 = [ | ||
{ | ||
name: 'Peter', | ||
age: 15, | ||
}, | ||
]; | ||
|
||
expect(isSubset(parentArray, childArray1)).toBeFalsy(); | ||
expect( | ||
isSubset(parentArray, childArray1, (obj) => JSON.stringify(obj)) | ||
).toBeTruthy(); | ||
expect( | ||
isSubset(parentArray, childArray2, (obj) => JSON.stringify(obj)) | ||
).toBeFalsy(); | ||
expect(isSubset(parentArray, childArray2, (obj) => obj.name)).toBeTruthy(); | ||
}); | ||
}); | ||
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.
it()
함수에 맞게 변경해주시면 감사드립니다.it
과test
는 동일한 기능을 하지만 이름이 서로 다르기 때문에 테스트 설명이 다르게 작성되어야 합니다.it()
에 들어갈 테스트 설명 작성이 어렵다면ChatGPT
를 활용하시는 것을 추천드립니다 🤗