-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): countOccurrencesInArray 유틸 함수 추가 (#159)
* feat(utils): countOccurrencesInArray 유틸 함수 추가 * docs: 문서 수정
- Loading branch information
Showing
9 changed files
with
134 additions
and
7 deletions.
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': minor | ||
--- | ||
|
||
feat(utils): countOccurrencesInArray 유틸 함수 추가 - @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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# countOccurrencesInArray | ||
|
||
입력한 배열에서 배열의 각 요소들이 등장한 횟수를 카운팅 해주는 유틸 함수입니다. | ||
|
||
💡 단, `Object`, `Array`, `Set`, `Map`과 같은 객체 타입은 카운팅에서 제외되며, `null`, `NaN`, `undefined`는 카운팅에 포함됩니다. | ||
|
||
<br /> | ||
|
||
## Code | ||
[🔗 실제 구현 코드 확인](https://github.com/modern-agile-team/modern-kit/blob/main/packages/utils/src/array/countOccurrencesInArray/index.ts) | ||
|
||
## Interface | ||
```ts title="typescript" | ||
const countOccurrencesInArray: <T extends readonly any[]>( | ||
arr: T | ||
) => Record<Exclude<T[number], object>, number>; | ||
``` | ||
|
||
## Usage | ||
```ts title="typescript" | ||
import { countOccurrencesInArray } from '@modern-kit/utils'; | ||
|
||
const arr = [ | ||
'foo', | ||
'foo', | ||
'foo', | ||
1, | ||
1, | ||
[1], // exclude | ||
{ a: 1 }, // exclude | ||
]; | ||
|
||
const countingObj = countOccurrencesInArray(arr); // { foo: 3, 1: 2 } | ||
``` |
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
66 changes: 66 additions & 0 deletions
66
packages/utils/src/array/countOccurrencesInArray/countOccurrencesInArray.spec.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,66 @@ | ||
import { countOccurrencesInArray } from '.'; | ||
|
||
describe('countOccurrencesInArray', () => { | ||
it('should count occurrences of each value correctly', () => { | ||
const testArray1 = [ | ||
'foo', | ||
'foo', | ||
'foo', | ||
1, | ||
1, | ||
false, | ||
false, | ||
null, | ||
null, | ||
undefined, | ||
NaN, | ||
[1], // exclude | ||
{ a: 1 }, // exclude | ||
new Set(), // exclude | ||
new Map(), // exclude | ||
]; | ||
|
||
expect(countOccurrencesInArray(testArray1)).toEqual({ | ||
foo: 3, | ||
1: 2, | ||
false: 2, | ||
null: 2, | ||
undefined: 1, | ||
NaN: 1, | ||
}); | ||
|
||
const testArray2: string[] = []; | ||
|
||
expect(countOccurrencesInArray(testArray2)).toEqual({}); | ||
}); | ||
|
||
it('should correctly type the result object based on input array types', () => { | ||
const readonlyTestArray = [ | ||
'foo', | ||
'foo', | ||
'bar', | ||
1, | ||
2, | ||
[1], // exclude | ||
{ a: 1 }, // exclude | ||
] as const; | ||
|
||
expectTypeOf(countOccurrencesInArray(readonlyTestArray)).toEqualTypeOf< | ||
Record<'foo' | 1 | 2 | 'bar', number> | ||
>(); | ||
|
||
const defaultTestArray = [ | ||
'foo', | ||
'foo', | ||
'bar', | ||
1, | ||
2, | ||
[1], // exclude | ||
{ a: 1 }, // exclude | ||
]; | ||
|
||
expectTypeOf(countOccurrencesInArray(defaultTestArray)).toEqualTypeOf< | ||
Record<string | number, number> | ||
>(); | ||
}); | ||
}); |
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 @@ | ||
export const countOccurrencesInArray = <T extends readonly any[]>( | ||
arr: T | ||
): Record<Exclude<T[number], object>, number> => { | ||
return arr.reduce((acc, cur) => { | ||
if (typeof cur === 'object' && cur != null) { | ||
return acc; | ||
} | ||
|
||
acc[cur] = (acc[cur] || 0) + 1; | ||
|
||
return acc; | ||
}, {}); | ||
}; |
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 @@ | ||
export * from './countOccurrencesInArray'; |
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,3 +1,4 @@ | ||
export * from './array'; | ||
export * from './clipboard'; | ||
export * from './common'; | ||
export * from './device'; | ||
|
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,3 +1,5 @@ | ||
export const isArray = <T>(value: unknown): value is Array<T> => { | ||
export const isArray = <T extends readonly any[]>( | ||
value: unknown | ||
): value is T => { | ||
return Array.isArray(value); | ||
}; |
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