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

Refactor, new types. #28

Merged
merged 3 commits into from
Mar 11, 2024
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "array-view",
"version": "0.2.1",
"version": "0.3.0",
"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
20 changes: 13 additions & 7 deletions src/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { ArrayMaskView, ArrayIndexListView, ArraySliceView, ArrayView } from "./views";
import { ArrayMaskView, ArrayIndexListView, ArraySliceView } from "./views";
import { Slice } from "./structs";
import { ArraySelectorInterface, ArrayViewInterface } from "./types";
import {
ArraySelectorInterface,
ArrayViewInterface,
IndexListSelectorInterface,
MaskSelectorInterface,
SliceSelectorInterface,
} from "./types";

/**
* Represents an index list selector that selects elements based on the provided array of indexes.
*
* @implements {ArraySelectorInterface}
* @implements {IndexListSelectorInterface}
*/
export class IndexListSelector implements ArraySelectorInterface {
export class IndexListSelector implements IndexListSelectorInterface {
/**
* The array of indexes to select elements from.
*/
Expand Down Expand Up @@ -40,9 +46,9 @@ export class IndexListSelector implements ArraySelectorInterface {
/**
* Represents a mask selector that selects elements based on the provided array of boolean mask values.
*
* @implements {ArraySelectorInterface}
* @implements {MaskSelectorInterface}
*/
export class MaskSelector implements ArraySelectorInterface {
export class MaskSelector implements MaskSelectorInterface {
/**
* The array of boolean mask values to select elements based on.
*/
Expand Down Expand Up @@ -79,7 +85,7 @@ export class MaskSelector implements ArraySelectorInterface {
*
* @implements {ArraySelectorInterface}
*/
export class SliceSelector extends Slice implements ArraySelectorInterface {
export class SliceSelector extends Slice implements SliceSelectorInterface {
/**
* Creates a new SliceSelector instance with the provided slice parameters.
*
Expand Down
101 changes: 100 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export interface ArrayViewInterface<T> {
*
* @returns {ArraySelectorInterface} Boolean mask for selecting elements that satisfy the predicate.
*/
is(predicate: (value: T) => boolean): ArraySelectorInterface;
is(predicate: (value: T) => boolean): MaskSelectorInterface;

/**
* Returns a subview of this view based on a selector or string slice.
Expand Down Expand Up @@ -105,6 +105,80 @@ export interface ArrayViewInterface<T> {
[Symbol.iterator](): IterableIterator<T>;
}

/**
* Represents a slice definition for selecting a range of elements.
*/
export interface SliceInterface {
/**
* The start index of the slice range.
*/
readonly start: number | undefined;
/**
* The end index of the slice range.
*/
readonly end: number | undefined;
/**
* The step size for selecting elements in the slice range.
*/
readonly step: number | undefined;

/**
* Normalizes the slice parameters based on the container length.
*
* @param {number} containerLength - The length of the container or array.
*
* @returns {NormalizedSlice} The normalized slice parameters.
*/
normalize(containerLength: number): NormalizedSliceInterface;

/**
* Returns the string representation of the Slice.
*
* @returns {string} The string representation of the Slice.
*/
toString(): string;
}

/**
* Represents a normalized slice definition with start, end, and step values.
*/
export interface NormalizedSliceInterface extends SliceInterface {
/**
* The start index of the normalized slice.
*/
readonly start: number;
/**
* The end index of the normalized slice.
*/
readonly end: number;
/**
* The step size for selecting elements in the normalized slice.
*/
readonly step: number;
/**
* Returns the length of the normalized slice.
*
* @type {number}
*/
readonly length: number;

/**
* Converts the provided index to the actual index based on the normalized slice parameters.
*
* @param {number} i - The index to convert.
*
* @returns {number} The converted index value.
*/
convertIndex(i: number): number;

/**
* Generate an iterator for iterating over the elements in the normalized slice range.
*
* @returns {IterableIterator<number>} An iterator for the normalized slice range.
*/
toRange(): IterableIterator<number>;
}

/**
* Interface for selecting elements from an array view.
*/
Expand All @@ -122,6 +196,31 @@ export interface ArraySelectorInterface {
select<T>(source: ArrayViewInterface<T>, readonly?: boolean): ArrayViewInterface<T>;
}

/**
* Interface for selecting elements from an array view by boolean mask.
*/
export interface MaskSelectorInterface extends ArraySelectorInterface {
/**
* Boolean mask array.
*/
readonly value: Array<boolean>;
}

/**
* Interface for selecting elements from an array view by boolean mask.
*/
export interface IndexListSelectorInterface extends ArraySelectorInterface {
/**
* Index list array.
*/
readonly value: Array<number>;
}

/**
* Interface for selecting elements from an array view by slice.
*/
export interface SliceSelectorInterface extends ArraySelectorInterface, SliceInterface {}

/**
* Type representing an array that can be sliced.
*
Expand Down
1 change: 1 addition & 0 deletions tests/views/array-view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ describe.each([

// Then
expect(v.toArray()).toEqual(expected);
expect([...v]).toEqual(expected);
});
},
);
Expand Down
Loading