https://github.com/smoren/array-view-ts
Create array views for easy data manipulation, select elements using Python-like slice notation, enable efficient selection of elements using index lists and boolean masks.
https://github.com/smoren/array-view-ts
array array-view array-viewer collection mask python-like selector slice typescript view
Last synced: 8 months ago
JSON representation
Create array views for easy data manipulation, select elements using Python-like slice notation, enable efficient selection of elements using index lists and boolean masks.
- Host: GitHub
- URL: https://github.com/smoren/array-view-ts
- Owner: Smoren
- License: mit
- Created: 2024-03-06T08:07:28.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-03-24T16:21:56.000Z (over 1 year ago)
- Last Synced: 2025-04-12T14:40:23.636Z (8 months ago)
- Topics: array, array-view, array-viewer, collection, mask, python-like, selector, slice, typescript, view
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/array-view
- Size: 534 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Array View for TypeScript and JavaScript
[](https://www.npmjs.com/package/array-view)
[](https://www.npmjs.com/package/array-view)
[](https://coveralls.io/github/Smoren/array-view-ts?branch=master)

[](https://bundlephobia.com/result?p=array-view)
[](https://opensource.org/licenses/MIT)

**Array View** is a TypeScript library that provides a powerful set of utilities for working with arrays in
a versatile and efficient manner. These classes enable developers to create views of arrays, manipulate data with ease,
and select specific elements using index lists, masks, and slice parameters.
Array View offers a Python-like slicing experience for efficient data manipulation and selection of array elements.
## Features
- Create array views for easy data manipulation.
- Select elements using [Python-like slice notation](https://www.geeksforgeeks.org/python-list-slicing/).
- Handle array slicing operations with ease.
- Enable efficient selection of elements using index lists and boolean masks.
## Installation
```bash
npm install array-view
```
## Quick examples
### Slicing
```typescript
import { view } from "array-view";
const originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const originalView = view(originalArray);
originalView.loc['1:7:2']; // [2, 4, 6]
originalView.loc[':3']; // [1, 2, 3]
originalView.loc['::-1']; // [9, 8, 7, 6, 5, 4, 3, 2, 1]
originalView.loc[2]; // 3
originalView.loc[4]; // 5
originalView.loc[-1]; // 9
originalView.loc[-2]; // 8
originalView.loc['1:7:2'] = [22, 44, 66];
originalArray; // [1, 22, 3, 44, 5, 66, 7, 8, 9]
```
### Subviews
```typescript
import { view, mask, select, slice } from "array-view";
const originalArray = [1, 2, 3, 4, 5];
const originalView = view(originalArray);
originalView.subview(mask([true, false, true, false, true])).toArray(); // [1, 3, 5]
originalView.subview(select([1, 2, 4])).toArray(); // [2, 3, 5]
originalView.subview(slice('::-1')).toArray(); // [5, 4, 3, 2, 1]
originalView.subview(slice([,,-1])).toArray(); // [5, 4, 3, 2, 1]
originalView.subview(mask([true, false, true, false, true])).apply((x: number) => x * 10);
originalArray; // [10, 2, 30, 4, 50]
```
### Combining subviews
```typescript
import { view, mask, select, slice } from "array-view";
const originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const subview = view(originalArray)
.subview('::2') // [1, 3, 5, 7, 9]
.subview(mask([true, false, true, true, true])) // [1, 5, 7, 9]
.subview(select([0, 1, 2])) // [1, 5, 7]
.subview('1:') // [5, 7]
subview.loc[':'] = [55, 77];
originalArray; // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10]
```
### Mask example
```typescript
import { view, mask } from "array-view";
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const array2 = [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10];
const mask = view(array1).is((x: number) => x % 3 === 0);
// MaskSelector([false, false, true, false, false, true, false, false, true, false])
view(array2)
.subview(mask)
.applyWith(
view(array1).subview(mask),
(lhs: number, rhs: number) => lhs + rhs,
)
.toArray();
// [-1, -2, 0, -4, -5, 0, -7, -8, 0, -10]);
```
## Contributing
Contributions are welcome! Feel free to open an issue or submit a pull request on the [GitHub repository](https://github.com/Smoren/array-view-ts).
## License
Array View TS is released under the MIT License.