https://github.com/cdaein/array
https://github.com/cdaein/array
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/cdaein/array
- Owner: cdaein
- License: mit
- Created: 2022-10-23T15:46:08.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-25T03:34:52.000Z (about 2 years ago)
- Last Synced: 2025-03-08T23:03:20.902Z (2 months ago)
- Language: TypeScript
- Size: 903 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @daeinc/array
Array utilities.
## Installation
```sh
npm i @daeinc/array
```then,
```js
import { fillAndMap, ... } from "@daeinc/array";
```## Functions
### accumulate
```ts
const accumulate: (arr: number[], precision?: number) => number[];
```Takes in a number array and returns a new array with values accumulated. For example, the input array `[1, 2, 3, 4]` will return `[1, 3, 6, 10]`. It also takes an optional parameter `precision`(default=4) to compensate for float rounding error. The original values are used while summing, but the return values will be rounded.
### addToArray
```ts
const addToArray: (
arr: T[],
entry: T,
newArrayLen: number,
mode?: "first" | "last"
) => T[];
```Adds a new element to array in-place while limiting how many to keep history. The mode `first` will insert at the beginning of the array.
### fillAndMap
```ts
const fillAndMap: (n: number, fn: (el: null, idx: number) => T) => T[];
```Creates a new array with given length and maps values
### getNonZeroIndices
```ts
const getNonZeroIndices: (arr: number[]) => number[];
```Check for elements with non-zero values and return indices.
### interpolateArray
```ts
const interpolateArray: (
arrStart: number[],
arrTarget: number[],
t: number
) => number[];
```Interpolates between two 1-dimensional arrays of same size.
### isAllOne
```ts
const isAllOne: (arr: number[]) => boolean;
```Returns `true` if all elements of input array is `1`.
### isAllZero
```ts
const isAllZero: (arr: number[]) => boolean;
```Returns `true` if all elements of input array is `0`.
### isAnyOne
```ts
const isAnyOne: (arr: number[]) => boolean;
```Returns `true` if any element of input array is `1`.
### isAnyZero
```ts
const isAnyZero: (arr: number[]) => boolean;
```Returns `true` if any element of input array is `0`.
### objectToArray
```ts
const objectToArray: (
obj: {
[key: string]: T;
},
keys: string[]
) => T[];
```Convert object key-value pairs into simple array of values. Only included keys will be converted. The order is preserved.
This function can be useful when converting objects with `{x, y}` to `[x, y]`, for example.
### unwrapArrayOfObjects
```ts
const unwrapArrayOfObjects: (
arr: {
[key: string]: T;
}[],
objKey: string
) => T[];
```A helper function to get object values inside an array. All objects must have same keys present. For example, when the input array is `[ {name: val1}, {name: val2} ]`, calling `unwrapArrayOfObjects(arr, "name")` will return `[val1, val2]`.
## To dos
- test
## License
MIT