mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-27 14:57:44 +03:00
style(*): Apply formatting in markdown files (#76)
This commit is contained in:
parent
3cc1a03519
commit
cfc47e4b83
@ -5,7 +5,7 @@
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function uniqBy<T, U>(arr: T[], mapper: (item: T) => U): T[]
|
||||
function uniqBy<T, U>(arr: T[], mapper: (item: T) => U): T[];
|
||||
```
|
||||
|
||||
### 파라미터
|
||||
|
@ -5,7 +5,7 @@
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function uniqWith<T>(arr: T[], areItemsEqual: (item1: T, item2: T) => boolean): T[]
|
||||
function uniqWith<T>(arr: T[], areItemsEqual: (item1: T, item2: T) => boolean): T[];
|
||||
```
|
||||
|
||||
### 파라미터
|
||||
|
@ -7,7 +7,7 @@
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function maxBy<T>(elements: T[], selector: (element: T) => number): T
|
||||
function maxBy<T>(elements: T[], selector: (element: T) => number): T;
|
||||
```
|
||||
|
||||
### 파라미터
|
||||
@ -22,6 +22,6 @@ function maxBy<T>(elements: T[], selector: (element: T) => number): T
|
||||
### 예시
|
||||
|
||||
```typescript
|
||||
maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a) // 결과: { a: 3 }
|
||||
maxBy([], x => x.a) // 결과: undefined
|
||||
maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // 결과: { a: 3 }
|
||||
maxBy([], x => x.a); // 결과: undefined
|
||||
```
|
||||
|
@ -7,7 +7,7 @@
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function minBy<T>(elements: T[], selector: (element: T) => number): T
|
||||
function minBy<T>(elements: T[], selector: (element: T) => number): T;
|
||||
```
|
||||
|
||||
### 파라미터
|
||||
@ -22,6 +22,6 @@ function minBy<T>(elements: T[], selector: (element: T) => number): T
|
||||
### 예시
|
||||
|
||||
```typescript
|
||||
maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a) // 결과: { a: 3 }
|
||||
maxBy([], x => x.a) // 결과: undefined
|
||||
maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // 결과: { a: 3 }
|
||||
maxBy([], x => x.a); // 결과: undefined
|
||||
```
|
||||
|
@ -7,7 +7,10 @@
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function omitBy<T extends Record<string, any>>(obj: T, shouldOmit: (value: T[keyof T], key: keyof T) => boolean): Partial<T>;
|
||||
function omitBy<T extends Record<string, any>>(
|
||||
obj: T,
|
||||
shouldOmit: (value: T[keyof T], key: keyof T) => boolean
|
||||
): Partial<T>;
|
||||
```
|
||||
|
||||
### 파라미터
|
||||
|
@ -5,7 +5,7 @@ Returns a new array containing only the unique elements from the original array,
|
||||
## Signature
|
||||
|
||||
```typescript
|
||||
function uniqBy<T, U>(arr: T[], mapper: (item: T) => U): T[]
|
||||
function uniqBy<T, U>(arr: T[], mapper: (item: T) => U): T[];
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
@ -5,7 +5,7 @@ Returns a new array containing only the unique elements from the original array,
|
||||
## Signature
|
||||
|
||||
```typescript
|
||||
function uniqWith<T>(arr: T[], areItemsEqual: (item1: T, item2: T) => boolean): T[]
|
||||
function uniqWith<T>(arr: T[], areItemsEqual: (item1: T, item2: T) => boolean): T[];
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
@ -7,7 +7,7 @@ If the list is empty, returns `undefined`.
|
||||
## Signature
|
||||
|
||||
```typescript
|
||||
function maxBy<T>(elements: T[], selector: (element: T) => number): T
|
||||
function maxBy<T>(elements: T[], selector: (element: T) => number): T;
|
||||
```
|
||||
|
||||
### Parameters
|
||||
@ -22,6 +22,6 @@ The first element of the list that has the maximum value of the function. If the
|
||||
### Example
|
||||
|
||||
```typescript
|
||||
maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a) // Returns: { a: 3 }
|
||||
maxBy([], x => x.a) // Returns: undefined
|
||||
maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 3 }
|
||||
maxBy([], x => x.a); // Returns: undefined
|
||||
```
|
||||
|
@ -7,7 +7,7 @@ If the list is empty, returns `undefined`.
|
||||
## Signature
|
||||
|
||||
```typescript
|
||||
function minBy<T>(elements: T[], selector: (element: T) => number): T
|
||||
function minBy<T>(elements: T[], selector: (element: T) => number): T;
|
||||
```
|
||||
|
||||
### Parameters
|
||||
@ -22,6 +22,6 @@ The first element of the list that has the minimum value of the function. If the
|
||||
### Example
|
||||
|
||||
```typescript
|
||||
minBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a) // Returns: { a: 1 }
|
||||
minBy([], x => x.a) // Returns: undefined
|
||||
minBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 1 }
|
||||
minBy([], x => x.a); // Returns: undefined
|
||||
```
|
||||
|
@ -8,7 +8,10 @@ includes only the properties for which the predicate function returns false.
|
||||
## Signature
|
||||
|
||||
```typescript
|
||||
function omitBy<T extends Record<string, any>>(obj: T, shouldOmit: (value: T[keyof T], key: keyof T) => boolean): Partial<T>;
|
||||
function omitBy<T extends Record<string, any>>(
|
||||
obj: T,
|
||||
shouldOmit: (value: T[keyof T], key: keyof T) => boolean
|
||||
): Partial<T>;
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
@ -19,7 +19,11 @@
|
||||
* const result = differenceWith(array1, array2, areItemsEqual);
|
||||
* // result will be [{ id: 1 }, { id: 3 }] since the elements with id 2 are considered equal and are excluded from the result.
|
||||
*/
|
||||
export function differenceWith<T>(firstArr: readonly T[], secondArr: readonly T[], areItemsEqual: (x: T, y: T) => boolean): T[] {
|
||||
export function differenceWith<T>(
|
||||
firstArr: readonly T[],
|
||||
secondArr: readonly T[],
|
||||
areItemsEqual: (x: T, y: T) => boolean
|
||||
): T[] {
|
||||
return firstArr.filter(firstItem => {
|
||||
return secondArr.every(secondItem => {
|
||||
return !areItemsEqual(firstItem, secondItem);
|
||||
|
@ -20,6 +20,6 @@ export function dropWhile<T>(arr: readonly T[], canContinueDropping: (item: T) =
|
||||
if (dropEndIndex === -1) {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
return arr.slice(dropEndIndex);
|
||||
}
|
||||
|
@ -19,7 +19,11 @@
|
||||
* const result = intersectionWith(array1, array2, areItemsEqual);
|
||||
* // result will be [{ id: 2 }] since this element has a matching id in both arrays.
|
||||
*/
|
||||
export function intersectionWith<T>(firstArr: readonly T[], secondArr: readonly T[], areItemsEqual: (x: T, y: T) => boolean): T[] {
|
||||
export function intersectionWith<T>(
|
||||
firstArr: readonly T[],
|
||||
secondArr: readonly T[],
|
||||
areItemsEqual: (x: T, y: T) => boolean
|
||||
): T[] {
|
||||
return firstArr.filter(firstItem => {
|
||||
return secondArr.some(secondItem => {
|
||||
return areItemsEqual(firstItem, secondItem);
|
||||
|
@ -19,6 +19,10 @@ import { uniqWith } from './uniqWith';
|
||||
* const result = unionWith(array1, array2, areItemsEqual);
|
||||
* // result will be [{ id: 1 }, { id: 2 }, { id: 3 }] since { id: 2 } is considered equal in both arrays
|
||||
*/
|
||||
export function unionWith<T>(arr1: readonly T[], arr2: readonly T[], areItemsEqual: (item1: T, item2: T) => boolean): T[] {
|
||||
export function unionWith<T>(
|
||||
arr1: readonly T[],
|
||||
arr2: readonly T[],
|
||||
areItemsEqual: (item1: T, item2: T) => boolean
|
||||
): T[] {
|
||||
return uniqWith(arr1.concat(arr2), areItemsEqual);
|
||||
}
|
||||
|
@ -20,7 +20,11 @@ import { unionWith } from './unionWith';
|
||||
* xorWith([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], areObjectsEqual);
|
||||
* // Returns [{ id: 1 }, { id: 3 }]
|
||||
*/
|
||||
export function xorWith<T>(arr1: readonly T[], arr2: readonly T[], areElementsEqual: (item1: T, item2: T) => boolean): T[] {
|
||||
export function xorWith<T>(
|
||||
arr1: readonly T[],
|
||||
arr2: readonly T[],
|
||||
areElementsEqual: (item1: T, item2: T) => boolean
|
||||
): T[] {
|
||||
const union = unionWith(arr1, arr2, areElementsEqual);
|
||||
const intersection = intersectionWith(arr1, arr2, areElementsEqual);
|
||||
|
||||
|
@ -22,7 +22,12 @@
|
||||
export function zip<T>(arr1: readonly T[]): Array<[T]>;
|
||||
export function zip<T, U>(arr1: readonly T[], arr2: readonly U[]): Array<[T, U]>;
|
||||
export function zip<T, U, V>(arr1: readonly T[], arr2: readonly U[], arr3: readonly V[]): Array<[T, U, V]>;
|
||||
export function zip<T, U, V, W>(arr1: readonly T[], arr2: readonly U[], arr3: readonly V[], arr4: readonly W[]): Array<[T, U, V, W]>;
|
||||
export function zip<T, U, V, W>(
|
||||
arr1: readonly T[],
|
||||
arr2: readonly U[],
|
||||
arr3: readonly V[],
|
||||
arr4: readonly W[]
|
||||
): Array<[T, U, V, W]>;
|
||||
export function zip<T>(...arrs: Array<readonly T[]>): T[][] {
|
||||
const result: T[][] = [];
|
||||
|
||||
|
@ -28,7 +28,12 @@
|
||||
*/
|
||||
export function zipWith<T, R>(arr1: readonly T[], combine: (item: T) => R): R[];
|
||||
export function zipWith<T, U, R>(arr1: readonly T[], arr2: readonly U[], combine: (item1: T, item2: U) => R): R[];
|
||||
export function zipWith<T, U, V, R>(arr1: readonly T[], arr2: readonly U[], arr3: readonly V[], combine: (item1: T, item2: U, item3: V) => R): R[];
|
||||
export function zipWith<T, U, V, R>(
|
||||
arr1: readonly T[],
|
||||
arr2: readonly U[],
|
||||
arr3: readonly V[],
|
||||
combine: (item1: T, item2: U, item3: V) => R
|
||||
): R[];
|
||||
export function zipWith<T, U, V, W, R>(
|
||||
arr1: readonly T[],
|
||||
arr2: readonly U[],
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { maxBy } from './maxBy';
|
||||
|
||||
describe('maxBy', () => {
|
||||
@ -23,15 +23,13 @@ describe('maxBy', () => {
|
||||
});
|
||||
|
||||
it('if array is single-element, return unique element of array', () => {
|
||||
const people = [
|
||||
{ name: 'Mark', age: 25 },
|
||||
];
|
||||
const people = [{ name: 'Mark', age: 25 }];
|
||||
const result = maxBy(people, person => person.age);
|
||||
expect(result).toEqual({ name: 'Mark', age: 25 });
|
||||
});
|
||||
|
||||
it('if array is empty, return undefined', () => {
|
||||
type Person = { name: string, age: number };
|
||||
type Person = { name: string; age: number };
|
||||
const people: Person[] = [];
|
||||
const result = maxBy(people, person => person.age);
|
||||
expect(result).toBeUndefined();
|
||||
|
@ -1,39 +1,37 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { minBy } from './minBy';
|
||||
|
||||
describe('minBy', () => {
|
||||
it('minBy selects one max value in array', () => {
|
||||
const people = [
|
||||
{ name: 'Mark', age: 30 },
|
||||
{ name: 'Nunu', age: 20 },
|
||||
{ name: 'Overmars', age: 35 },
|
||||
];
|
||||
const result = minBy(people, person => person.age);
|
||||
expect(result).toEqual({ name: 'Nunu', age: 20 });
|
||||
});
|
||||
it('minBy selects one max value in array', () => {
|
||||
const people = [
|
||||
{ name: 'Mark', age: 30 },
|
||||
{ name: 'Nunu', age: 20 },
|
||||
{ name: 'Overmars', age: 35 },
|
||||
];
|
||||
const result = minBy(people, person => person.age);
|
||||
expect(result).toEqual({ name: 'Nunu', age: 20 });
|
||||
});
|
||||
|
||||
it('if there are two max values, first one is selected', () => {
|
||||
const people = [
|
||||
{ name: 'Mark', age: 30 },
|
||||
{ name: 'Nunu', age: 20 },
|
||||
{ name: 'Overmars', age: 20 },
|
||||
];
|
||||
const result = minBy(people, person => person.age);
|
||||
expect(result).toEqual({ name: 'Nunu', age: 20 });
|
||||
});
|
||||
it('if there are two max values, first one is selected', () => {
|
||||
const people = [
|
||||
{ name: 'Mark', age: 30 },
|
||||
{ name: 'Nunu', age: 20 },
|
||||
{ name: 'Overmars', age: 20 },
|
||||
];
|
||||
const result = minBy(people, person => person.age);
|
||||
expect(result).toEqual({ name: 'Nunu', age: 20 });
|
||||
});
|
||||
|
||||
it('if array is single-element, return unique element of array', () => {
|
||||
const people = [
|
||||
{ name: 'Mark', age: 25 },
|
||||
];
|
||||
const result = minBy(people, person => person.age);
|
||||
expect(result).toEqual({ name: 'Mark', age: 25 });
|
||||
});
|
||||
it('if array is single-element, return unique element of array', () => {
|
||||
const people = [{ name: 'Mark', age: 25 }];
|
||||
const result = minBy(people, person => person.age);
|
||||
expect(result).toEqual({ name: 'Mark', age: 25 });
|
||||
});
|
||||
|
||||
it('if array is empty, return undefined', () => {
|
||||
type Person = { name: string, age: number };
|
||||
const people: Person[] = [];
|
||||
const result = minBy(people, person => person.age);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
it('if array is empty, return undefined', () => {
|
||||
type Person = { name: string; age: number };
|
||||
const people: Person[] = [];
|
||||
const result = minBy(people, person => person.age);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
@ -9,7 +9,6 @@
|
||||
* @param selector A function that selects a number from an element.
|
||||
*/
|
||||
export function minBy<T>(elements: T[], selector: (element: T) => number): T {
|
||||
|
||||
let minElement = elements[0];
|
||||
let min = Infinity;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user