diff --git a/package.json b/package.json index 3e4a40e..a3ca926 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/selectors.ts b/src/selectors.ts index b03a40b..1bf7bba 100644 --- a/src/selectors.ts +++ b/src/selectors.ts @@ -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. */ @@ -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. */ @@ -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. * diff --git a/src/types.ts b/src/types.ts index 1bb281c..d215dcc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -55,7 +55,7 @@ export interface ArrayViewInterface { * * @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. @@ -105,6 +105,80 @@ export interface ArrayViewInterface { [Symbol.iterator](): IterableIterator; } +/** + * 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} An iterator for the normalized slice range. + */ + toRange(): IterableIterator; +} + /** * Interface for selecting elements from an array view. */ @@ -122,6 +196,31 @@ export interface ArraySelectorInterface { select(source: ArrayViewInterface, readonly?: boolean): ArrayViewInterface; } +/** + * Interface for selecting elements from an array view by boolean mask. + */ +export interface MaskSelectorInterface extends ArraySelectorInterface { + /** + * Boolean mask array. + */ + readonly value: Array; +} + +/** + * Interface for selecting elements from an array view by boolean mask. + */ +export interface IndexListSelectorInterface extends ArraySelectorInterface { + /** + * Index list array. + */ + readonly value: Array; +} + +/** + * Interface for selecting elements from an array view by slice. + */ +export interface SliceSelectorInterface extends ArraySelectorInterface, SliceInterface {} + /** * Type representing an array that can be sliced. * diff --git a/tests/views/array-view.test.ts b/tests/views/array-view.test.ts index de1cf6e..91b7ca3 100644 --- a/tests/views/array-view.test.ts +++ b/tests/views/array-view.test.ts @@ -187,6 +187,7 @@ describe.each([ // Then expect(v.toArray()).toEqual(expected); + expect([...v]).toEqual(expected); }); }, );