Skip to content

Commit 1c4b2f6

Browse files
committed
fix for types
1 parent 90dc202 commit 1c4b2f6

File tree

6 files changed

+40
-13
lines changed

6 files changed

+40
-13
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ originalView.loc['::-1']; // [9, 8, 7, 6, 5, 4, 3, 2, 1]
3838

3939
originalView.loc[2]; // 3
4040
originalView.loc[4]; // 5
41+
originalView.loc[-1]; // 9
42+
originalView.loc[-2]; // 8
4143

4244
originalView.loc['1:7:2'] = [22, 44, 66];
4345
originalArray; // [1, 22, 3, 44, 5, 66, 7, 8, 9]

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "array-view",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "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.",
55
"license": "MIT",
66
"repository": {
@@ -41,6 +41,7 @@
4141
"slice",
4242
"array",
4343
"array-view",
44+
"data-view",
4445
"selector",
4546
"collection",
4647
"python-like"

src/functions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ArrayView } from "./views";
22
import { Slice } from "./structs";
33
import { IndexListSelector, MaskSelector, SliceSelector } from "./selectors";
4+
import { ArrayViewInterface } from "./types";
45

56
/**
67
* Creates an ArrayView instance from the provided source array or ArrayView.
@@ -23,7 +24,7 @@ import { IndexListSelector, MaskSelector, SliceSelector } from "./selectors";
2324
* const filteredView = view(originalArrayView, false);
2425
* // Modify the filtered view affecting the original array
2526
*/
26-
export function view<T>(source: Array<T> | ArrayView<T>, readonly?: boolean): ArrayView<T> {
27+
export function view<T>(source: Array<T> | ArrayViewInterface<T>, readonly?: boolean): ArrayView<T> {
2728
return ArrayView.toView(source, readonly);
2829
}
2930

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export interface ArrayViewInterface<T> {
2727
* Flag indicating if the view is read-only.
2828
*/
2929
readonly readonly: boolean;
30+
/**
31+
* Base source array.
32+
*/
33+
readonly source: Array<T>;
3034

3135
/**
3236
* Returns the array representation of the view.

src/views.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ export class ArrayView<T> implements ArrayViewInterface<T> {
2121
* Source array of the view.
2222
* @protected
2323
*/
24-
protected readonly source: Array<T>;
24+
public readonly source: Array<T>;
2525
/**
2626
* Parent view (exists if source is another array view).
2727
* @protected
2828
*/
29-
protected readonly parentView?: ArrayView<T>;
29+
protected readonly parentView?: ArrayViewInterface<T>;
3030

3131
/**
3232
* Creates an ArrayView instance from the given source array or ArrayView.
@@ -37,12 +37,12 @@ export class ArrayView<T> implements ArrayViewInterface<T> {
3737
*
3838
* @template T
3939
*
40-
* @param {Array<T> | ArrayView<T>} source - The source array or ArrayView to create a view from.
40+
* @param {Array<T> | ArrayViewInterface<T>} source - The source array or ArrayView to create a view from.
4141
* @param {boolean} [readonly] - Optional flag to indicate whether the view should be readonly.
4242
*
4343
* @returns {ArrayView<T>} An ArrayView instance based on the source array or ArrayView.
4444
*/
45-
public static toView<T>(source: Array<T> | ArrayView<T>, readonly?: boolean): ArrayView<T> {
45+
public static toView<T>(source: Array<T> | ArrayViewInterface<T>, readonly?: boolean): ArrayView<T> {
4646
if (!(source instanceof ArrayView)) {
4747
return new ArrayView(source, { readonly });
4848
}
@@ -64,12 +64,12 @@ export class ArrayView<T> implements ArrayViewInterface<T> {
6464
* @constructor
6565
*/
6666
constructor(
67-
source: Array<T> | ArrayView<T>,
67+
source: Array<T> | ArrayViewInterface<T>,
6868
{ readonly }: { readonly?: boolean } = {},
6969
) {
70-
const loc = (source instanceof ArrayView) ? source.loc : source;
71-
this.source = (source instanceof ArrayView) ? source.source : source;
72-
this.parentView = (source instanceof ArrayView) ? source : undefined;
70+
const loc = Array.isArray(source) ? source : source.loc;
71+
this.source = Array.isArray(source) ? source : source.source;
72+
this.parentView = Array.isArray(source) ? undefined : source;
7373
this.readonly = readonly ?? ((source instanceof ArrayView) ? (source as ArrayView<T>).readonly : false);
7474

7575
if ((source instanceof ArrayView) && source.readonly && !this.readonly) {

tests/examples/examples.test.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ it("Slicing example", () => {
1515

1616
expect(originalView.loc[2]).toEqual(3);
1717
expect(originalView.loc[4]).toEqual(5);
18+
expect(originalView.loc[-1]).toEqual(9);
19+
expect(originalView.loc[-2]).toEqual(8);
1820

1921
originalView.loc['1:7:2'] = [22, 44, 66];
2022
expect(originalArray).toEqual([1, 22, 3, 44, 5, 66, 7, 8, 9])
@@ -36,13 +38,30 @@ it("Combined example", () => {
3638
const originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3739

3840
const subview = view(originalArray)
39-
.subview('::2') // [1, 3, 5, 7, 9]
41+
.subview('::2') // [1, 3, 5, 7, 9]
4042
.subview(mask([true, false, true, true, true])) // [1, 5, 7, 9]
41-
.subview(select([0, 1, 2])) // [1, 5, 7]
42-
.subview('1:') // [5, 7]
43+
.subview(select([0, 1, 2])) // [1, 5, 7]
44+
.subview('1:') // [5, 7]
4345

4446
expect(subview.toArray()).toEqual([5, 7]);
4547

4648
subview.loc[':'] = [55, 77];
4749
expect(originalArray).toEqual([1, 2, 3, 4, 55, 6, 77, 8, 9, 10]);
4850
});
51+
52+
it("Another combined example", () => {
53+
const originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
54+
55+
const subview = view(originalArray)
56+
.subview('::2') // [1, 3, 5, 7, 9]
57+
.subview(mask([true, false, true, true, true])); // [1, 5, 7, 9]
58+
59+
const anotherSubview = view(subview)
60+
.subview(select([0, 1, 2])) // [1, 5, 7]
61+
.subview('1:') // [5, 7]
62+
63+
expect([...anotherSubview]).toEqual([5, 7]);
64+
65+
anotherSubview.loc[':'] = [55, 77];
66+
expect(originalArray).toEqual([1, 2, 3, 4, 55, 6, 77, 8, 9, 10]);
67+
});

0 commit comments

Comments
 (0)