Skip to content

Commit

Permalink
fix for types
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 10, 2024
1 parent 90dc202 commit 1c4b2f6
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ 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]
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "array-view",
"version": "0.1.5",
"version": "0.1.6",
"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.",
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -41,6 +41,7 @@
"slice",
"array",
"array-view",
"data-view",
"selector",
"collection",
"python-like"
Expand Down
3 changes: 2 additions & 1 deletion src/functions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ArrayView } from "./views";
import { Slice } from "./structs";
import { IndexListSelector, MaskSelector, SliceSelector } from "./selectors";
import { ArrayViewInterface } from "./types";

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

Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export interface ArrayViewInterface<T> {
* Flag indicating if the view is read-only.
*/
readonly readonly: boolean;
/**
* Base source array.
*/
readonly source: Array<T>;

/**
* Returns the array representation of the view.
Expand Down
16 changes: 8 additions & 8 deletions src/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ export class ArrayView<T> implements ArrayViewInterface<T> {
* Source array of the view.
* @protected
*/
protected readonly source: Array<T>;
public readonly source: Array<T>;
/**
* Parent view (exists if source is another array view).
* @protected
*/
protected readonly parentView?: ArrayView<T>;
protected readonly parentView?: ArrayViewInterface<T>;

/**
* Creates an ArrayView instance from the given source array or ArrayView.
Expand All @@ -37,12 +37,12 @@ export class ArrayView<T> implements ArrayViewInterface<T> {
*
* @template T
*
* @param {Array<T> | ArrayView<T>} source - The source array or ArrayView to create a view from.
* @param {Array<T> | ArrayViewInterface<T>} source - The source array or ArrayView to create a view from.
* @param {boolean} [readonly] - Optional flag to indicate whether the view should be readonly.
*
* @returns {ArrayView<T>} An ArrayView instance based on the source array or ArrayView.
*/
public static toView<T>(source: Array<T> | ArrayView<T>, readonly?: boolean): ArrayView<T> {
public static toView<T>(source: Array<T> | ArrayViewInterface<T>, readonly?: boolean): ArrayView<T> {
if (!(source instanceof ArrayView)) {
return new ArrayView(source, { readonly });
}
Expand All @@ -64,12 +64,12 @@ export class ArrayView<T> implements ArrayViewInterface<T> {
* @constructor
*/
constructor(
source: Array<T> | ArrayView<T>,
source: Array<T> | ArrayViewInterface<T>,
{ readonly }: { readonly?: boolean } = {},
) {
const loc = (source instanceof ArrayView) ? source.loc : source;
this.source = (source instanceof ArrayView) ? source.source : source;
this.parentView = (source instanceof ArrayView) ? source : undefined;
const loc = Array.isArray(source) ? source : source.loc;
this.source = Array.isArray(source) ? source : source.source;
this.parentView = Array.isArray(source) ? undefined : source;
this.readonly = readonly ?? ((source instanceof ArrayView) ? (source as ArrayView<T>).readonly : false);

if ((source instanceof ArrayView) && source.readonly && !this.readonly) {
Expand Down
25 changes: 22 additions & 3 deletions tests/examples/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ it("Slicing example", () => {

expect(originalView.loc[2]).toEqual(3);
expect(originalView.loc[4]).toEqual(5);
expect(originalView.loc[-1]).toEqual(9);
expect(originalView.loc[-2]).toEqual(8);

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

const subview = view(originalArray)
.subview('::2') // [1, 3, 5, 7, 9]
.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(select([0, 1, 2])) // [1, 5, 7]
.subview('1:') // [5, 7]

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

subview.loc[':'] = [55, 77];
expect(originalArray).toEqual([1, 2, 3, 4, 55, 6, 77, 8, 9, 10]);
});

it("Another combined example", () => {
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]

const anotherSubview = view(subview)
.subview(select([0, 1, 2])) // [1, 5, 7]
.subview('1:') // [5, 7]

expect([...anotherSubview]).toEqual([5, 7]);

anotherSubview.loc[':'] = [55, 77];
expect(originalArray).toEqual([1, 2, 3, 4, 55, 6, 77, 8, 9, 10]);
});

0 comments on commit 1c4b2f6

Please sign in to comment.