Skip to content
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 20 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
5bfe36c
feat(utils): isSubset 생성
beberiche Jul 6, 2024
8accd69
text(utils): isSubset 함수 테스트 파일 생성
beberiche Jul 6, 2024
cf901f7
feat(utils): isSubset 의 배열 요소가 참조형인 경우를 위한 deepEqual 적용
beberiche Jul 6, 2024
65e0fef
test(utils): isSubset deepEqual 적용에 대한 테스트 재조정
beberiche Jul 6, 2024
5e3f0b3
feat(utils): validator 폴더의 index.ts에 isSubset export 항목 추가
beberiche Jul 6, 2024
d630eef
docs(utils): isSubset.md 생성
beberiche Jul 6, 2024
1731597
fix(utils): isSubset 함수 매개변수 타입 에러 개선
beberiche Jul 6, 2024
53144df
feat(utils): deepEqual 코드 삭제. (부분집합의 정의를 사용자에게 위임)
beberiche Jul 7, 2024
84c855b
test(utils): isSubset 함수 수정에 대한 test 케이스 변경
beberiche Jul 7, 2024
ac53f8e
docs(utils): isSubset 함수 코드 수정사항에 맞게 docs 내용 변경
beberiche Jul 7, 2024
67f83fb
test(utils): 테스트 실패요소 개선
beberiche Jul 7, 2024
a9439fb
refactor(utils): isSubset 함수 재구현 (utils/array/difference 재사용)
beberiche Jul 8, 2024
7de0806
test(utils): isSubset 함수 재구현에 따른 test 내용 수정
beberiche Jul 8, 2024
6a62323
docs(utils): isSubset 함수 재구현에 따른 설명 내용 수정
beberiche Jul 8, 2024
c473786
Update packages/utils/src/validator/isSubset/index.ts
ssi02014 Jul 8, 2024
f31e24d
Update docs/docs/utils/validator/isSubset.md
ssi02014 Jul 8, 2024
a4c479f
Update docs/docs/utils/validator/isSubset.md
ssi02014 Jul 8, 2024
0e4f0da
Update docs/docs/utils/validator/isSubset.md
ssi02014 Jul 8, 2024
52047c7
Update docs/docs/utils/validator/isSubset.md
ssi02014 Jul 8, 2024
9308730
Create tidy-guests-clean.md
ssi02014 Jul 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions docs/docs/utils/validator/isSubset.md
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
1 change: 1 addition & 0 deletions packages/utils/src/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * from './isString';
export * from './isValidEmail';
export * from './isValidPassword';
export * from './isValidPhoneNumberFormat';
export * from './isSubset';
12 changes: 12 additions & 0 deletions packages/utils/src/validator/isSubset/index.ts
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));
};
70 changes: 70 additions & 0 deletions packages/utils/src/validator/isSubset/isSubset.spec.ts
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();
});
});
Copy link
Contributor

@ssi02014 ssi02014 Jul 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import { isSubset } from '.';

describe('isSubset', () => {
  it('should correctly determine if the subset arrays are subsets of the superset array', () => {
    // 네이밍 변경
    const superset = [1, 2, 3, 4];
    const subset1 = [1, 3];
    const subset2 = [1, 5];
    
    // ...
  });

  it('should return the correct result if the types are different between comparison elements', () => {
    // ...
  });

  it('should handle elements of type array correctly', () => {
    expect(isSubset(superset, subset1, (arr) => arr[2])).toBeTruthy(); // (*) iteratee인자를 arr로 변경해주세요.
    // ...
  });

  it('should handle elements of type reference correctly', () => {
    // ...
  });
});

Loading