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

✨ Add missing methods documentation #2

Merged
merged 3 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
257 changes: 251 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This library provides a set of functions for working with arrays in TypeScript.
- `average`: returns the average of all elements in the array, or 0 if the array is empty
- `distinct`: returns a new array with only distinct elements
- `shuffle`: returns a new array with the same elements, but in a shuffled order
- `flattenArray`: Flattens an array of nested arrays into a single array.
- `flatten`: Flattens an array of nested arrays into a single array.
- `uniqueElements`: Returns an array with only the unique elements from the input array.
- `groupBy`: Groups the elements of an array based on the result of a callback function.
- `partition`: Divides an array into two arrays based on the result of a callback function.
Expand All @@ -24,6 +24,25 @@ This library provides a set of functions for working with arrays in TypeScript.
- `mode`: Returns the mode of an array of numbers.
- `variance`: Returns the variance of an array of numbers.
- `standardDeviation`: Returns the standard deviation of an array of numbers.
- `countOccurrences`: Returns the number of occurrences of a given element in an array.
- `zip`: Returns an array of arrays, where each array contains elements of the input arrays at corresponding indices.
- `unzip`: Returns an array of arrays, where each array contains elements of the input array of arrays at corresponding indices.
- `rotate`: Rotates the elements of an array by a given number of positions.
- `flip`: Reverses the order of elements in an array.
- `mirror`: Returns a new array which is the concatenation of the input array and its reversed version.
- `isSorted`: Returns whether an array or object is sorted based on a provided comparison function, direction, and key.
- `isEqual`: Returns whether two arrays are equal in terms of elements and order.
- `swap`: Swaps the position of two elements in an array.
- `move`: Moves an element from one position to another in an array.
- `fill`: Fills an array with a specified value.
- `reverseFill`: Fills an array in reverse order with a specified value.
- `findFirst`: Returns the first element that satisfies a provided callback function.
- `findLast`: Returns the last element that satisfies a provided callback function.
- `remove`: Removes elements from an array at a given index and count.
- `insert`: Inserts elements into an array at a given index.
- `merge`: Merges two arrays into one.
- `pad`: Pads an array with a specified value and repeat times.
repeat: Returns an array with a repeated element.
- `range`: Returns an array with the minimum and maximum elements in an array of numbers.
- `countBy`: Groups the elements of an array and counts the number of elements in each group.
- `deepSearch`: Finds patterns, in strings, arrays, objects or nested combinations and returns the index of the match."
Expand Down Expand Up @@ -239,19 +258,19 @@ const shuffledNestedMixedArray = shuffle(nestedMixedArray);
console.log(shuffledNestedMixedArray); // [['a', 'b'], [{ key: 1 }, { key: 2 }], [1, 2]]
```

# `flattenArray`
# `flatten`

The `flattenArray` method flattens an array of arbitrarily nested arrays of any type into a flat array.
The `flatten` method flattens an array of arbitrarily nested arrays of any type into a flat array.

```
import { flattenArray } from 'ts-array-utilities';
import { flatten } from 'ts-array-utilities';

const nestedArray = [1, [2, [3, [4]]]];
const flatArray = flattenArray(nestedArray);
const flatArray = flatten(nestedArray);
// flatArray: [1, 2, 3, 4]

const moreNestedArray = [1, 2, [3, [4, 5], 6, [[[7, 8]]]], 9];
const moreFlatArray = flattenArray(moreNestedArray);
const moreFlatArray = flatten(moreNestedArray);
// moreFlatArray: [1, 2, 3, 4, 5, 6, 7, 8, 9]
```

Expand Down Expand Up @@ -432,6 +451,232 @@ const standardDeviationValue = standardDeviation(array);
// 1.5811388300841898
```

# `countOccurrences`

The `countOccurrences` method returns the number of occurrences of a given element in an array.

import { countOccurrences } from 'ts-array-utilities';

const array = [1, 2, 3, 2, 4, 2, 5];
const count = countOccurrences(array, 2);
console.log(count); // 3

# `zip`

The `zip` method returns an array of arrays, where each array contains elements of the input arrays at corresponding indices.

import { zip } from 'ts-array-utilities';

const array1 = [1, 2, 3];
const array2 = ['a', 'b', 'c'];
const array3 = [true, false, true];
const zipped = zip(array1, array2, array3);
console.log(zipped); // [[1, 'a', true], [2, 'b', false], [3, 'c', true]]

# `unzip`

The `unzip` method returns an array of arrays, where each array contains elements of the input array of arrays at corresponding indices.

import { unzip } from 'ts-array-utilities';

const array = [[1, 'a', true], [2, 'b', false], [3, 'c', true]];
const unzipped = unzip(array);
console.log(unzipped); // [[1, 2, 3], ['a', 'b', 'c'], [true, false, true]]

# `rotate`

The `rotate` method rotates the elements of an array by a given number of positions.

import { rotate } from 'ts-array-utilities';

const array = [1, 2, 3, 4, 5];
const rotated = rotate(array, 2);
console.log(rotated); // [4, 5, 1, 2, 3]

# `flip`

The `flip` method reverses the order of elements in an array.

import { flip } from 'ts-array-utilities';

const array = [1, 2, 3, 4, 5];
const flipped = flip(array);
console.log(flipped); // [5, 4, 3, 2, 1]

# `mirror`

The `mirror` method returns a new array which is the concatenation of the input array and its reversed version.

```
import { mirror } from 'ts-array-utilities';

const array = [1, 2, 3, 4, 5];
const mirrored = mirror(array);
console.log(mirrored); // [1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
```

# `isSorted`

The `isSorted` function is a utility function that can be used to determine if an array or object's values are sorted in a specified order.

```
import { isSorted } from 'isSorted';

const numbers = [1, 2, 3, 4, 5];
console.log(isSorted(numbers, (a, b) => a - b)); //true

const names = ['John', 'Mike', 'Sara', 'Zack'];
console.log(isSorted(names, (a, b) => a.localeCompare(b), 'desc')); // false

const obj = {a: {x:1}, b: {x:2}, c: {x:3}};
console.log(isSorted(obj, (a, b) => a - b, 'asc', 'x')); // true
```

# `isEqual`

The `isEqual` method returns whether two arrays are equal interns of elements and order.

```
import { isEqual } from 'ts-array-utilities';

const array1 = [1, 2, 3];
const array2 = [1, 2, 3];
const equal = isEqual(array1, array2);
console.log(equal); // true
```

# `swap`

The `swap` method swaps the position of two elements in an array.

```
import { swap } from 'ts-array-utilities';

const array = [1, 2, 3, 4, 5];
swap(array, 1, 3);
console.log(array); // [1, 4, 3, 2, 5]
```

# `move`

The `move` method moves an element from one position to another in an array.

```
import { move } from 'ts-array-utilities';

const array = [1, 2, 3, 4, 5];
move(array, 1, 3);
console.log(array); // [1, 3, 4, 2, 5]
```

# `fill`

The `fill` method fills an array with a specified value.

```
import { fill } from 'ts-array-utilities';

const array = [1, 2, 3, 4, 5];
fill(array, 0);
console.log(array); // [0, 0, 0, 0, 0]
```

# `reverseFill`

The `reverseFill` method fills an array in reverse order with a specified value.

```
import { reverseFill } from 'ts-array-utilities';

const array = [1, 2, 3, 4, 5];
reverseFill(array, 0);
console.log(array); // [0, 0, 0, 0, 0]
```

# `findFirst`

The `findFirst` method returns the first element that satisfies a provided callback function.

```
import { findFirst } from 'ts-array-utilities';

const array = [1, 2, 3, 4, 5];
const firstEven = findFirst(array, (element) => element % 2 === 0);
console.log(firstEven); // 2
```

# `findLast`

The `findLast` method returns the last element that satisfies a provided callback function.

```
import { findLast } from 'ts-array-utilities';

const array = [1, 2, 3, 4, 5];
const lastEven = findLast(array, (element) => element % 2 === 0);
console.log(lastEven); // 4
```

# `remove`

The `remove` method removes elements from an array at a given index and count.

```
import { remove } from 'ts-array-utilities';

const array = [1, 2, 3, 4, 5];
remove(array, 1, 2);
console.log(array); // [1, 4, 5]
```

# `insert`

The `insert` method inserts elements into an array at a given index.

```
import { insert } from 'ts-array-utilities';

const array = [1, 2, 3, 4, 5];
insert(array, 1, 6, 7);
console.log(array); // [1, 6, 7, 2, 3, 4, 5]
```

# `merge`

The `merge` method merges two arrays into one.

```
import { merge } from 'ts-array-utilities';

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const merged = merge(array1, array2);
console.log(merged); // [1, 2, 3, 4, 5, 6]
```

# `pad`

The `pad` method pads an array with a specified value and repeat times.

```
import { pad } from 'ts-array-utilities';

const array = [1, 2, 3];
const padded = pad(array, 0, 2);
console.log(padded); // [0, 0, 1, 2, 3, 0, 0]
```

# `repeat`

The `repeat` method returns an array with a repeated element.

```
import { repeat } from 'ts-array-utilities';

const repeated = repeat(1, 5);
console.log(repeated); // [1, 1, 1, 1, 1]
```

# `range`

The `range` method calculates the minimum and maximum values in the array.
Expand Down
42 changes: 32 additions & 10 deletions dist/helpers.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
export declare function sortNumberArray(array: number[], direction: string): number[];
export declare function sortStringArray(array: string[], direction: string): string[];
export declare function sortObjectArray(array: object[], direction: string, sortBy: string): object[];
export declare function deepEqual(a: any, b: any): boolean;
export declare function isNumber(value: any): value is number;
export declare function isObject(value: any): value is object;
export declare function isArrayOfNumbers(arr: any[]): boolean;
export declare function isArrayOfObjects(arr: any[]): boolean;
export declare function isArrayOfStrings(arr: any[]): boolean;
export declare function isArrayOfArrays(arr: any[]): boolean;
export declare function sortNumberArray(
array: number[],
direction: string
): number[]
export declare function sortStringArray(
array: string[],
direction: string
): string[]
export declare function sortObjectArray(
array: object[],
direction: string,
sortBy: string
): object[]
export declare function deepEqual(a: any, b: any): boolean
export declare function isNumber(value: any): value is number
export declare function isObject(value: any): value is object
export declare function isArrayOfNumbers(arr: any[]): boolean
export declare function isArrayOfObjects(arr: any[]): boolean
export declare function isArrayOfStrings(arr: any[]): boolean
export declare function isArrayOfArrays(arr: any[]): boolean
export declare function removeArray(
array: any[],
index: number | ((x: number) => boolean),
count?: number
): any[]
export declare function removeObject(
object: Record<string, any>,
index:
| number
| ((x: any, key: string, obj: Record<string, any>) => boolean)
| string
): Record<string, any>
Loading