Skip to content

Commit 4ab16ba

Browse files
committed
Refactor, new types
1 parent 93e28bc commit 4ab16ba

File tree

2 files changed

+113
-8
lines changed

2 files changed

+113
-8
lines changed

src/selectors.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import { ArrayMaskView, ArrayIndexListView, ArraySliceView, ArrayView } from "./views";
1+
import { ArrayMaskView, ArrayIndexListView, ArraySliceView } from "./views";
22
import { Slice } from "./structs";
3-
import { ArraySelectorInterface, ArrayViewInterface } from "./types";
3+
import {
4+
ArraySelectorInterface,
5+
ArrayViewInterface,
6+
IndexListSelectorInterface,
7+
MaskSelectorInterface,
8+
SliceSelectorInterface,
9+
} from "./types";
410

511
/**
612
* Represents an index list selector that selects elements based on the provided array of indexes.
713
*
8-
* @implements {ArraySelectorInterface}
14+
* @implements {IndexListSelectorInterface}
915
*/
10-
export class IndexListSelector implements ArraySelectorInterface {
16+
export class IndexListSelector implements IndexListSelectorInterface {
1117
/**
1218
* The array of indexes to select elements from.
1319
*/
@@ -40,9 +46,9 @@ export class IndexListSelector implements ArraySelectorInterface {
4046
/**
4147
* Represents a mask selector that selects elements based on the provided array of boolean mask values.
4248
*
43-
* @implements {ArraySelectorInterface}
49+
* @implements {MaskSelectorInterface}
4450
*/
45-
export class MaskSelector implements ArraySelectorInterface {
51+
export class MaskSelector implements MaskSelectorInterface {
4652
/**
4753
* The array of boolean mask values to select elements based on.
4854
*/
@@ -79,7 +85,7 @@ export class MaskSelector implements ArraySelectorInterface {
7985
*
8086
* @implements {ArraySelectorInterface}
8187
*/
82-
export class SliceSelector extends Slice implements ArraySelectorInterface {
88+
export class SliceSelector extends Slice implements SliceSelectorInterface {
8389
/**
8490
* Creates a new SliceSelector instance with the provided slice parameters.
8591
*

src/types.ts

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export interface ArrayViewInterface<T> {
5555
*
5656
* @returns {ArraySelectorInterface} Boolean mask for selecting elements that satisfy the predicate.
5757
*/
58-
is(predicate: (value: T) => boolean): ArraySelectorInterface;
58+
is(predicate: (value: T) => boolean): MaskSelectorInterface;
5959

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

108+
/**
109+
* Represents a slice definition for selecting a range of elements.
110+
*/
111+
export interface SliceInterface {
112+
/**
113+
* The start index of the slice range.
114+
*/
115+
readonly start: number | undefined;
116+
/**
117+
* The end index of the slice range.
118+
*/
119+
readonly end: number | undefined;
120+
/**
121+
* The step size for selecting elements in the slice range.
122+
*/
123+
readonly step: number | undefined;
124+
125+
/**
126+
* Normalizes the slice parameters based on the container length.
127+
*
128+
* @param {number} containerLength - The length of the container or array.
129+
*
130+
* @returns {NormalizedSlice} The normalized slice parameters.
131+
*/
132+
normalize(containerLength: number): NormalizedSliceInterface;
133+
134+
/**
135+
* Returns the string representation of the Slice.
136+
*
137+
* @returns {string} The string representation of the Slice.
138+
*/
139+
toString(): string;
140+
}
141+
142+
/**
143+
* Represents a normalized slice definition with start, end, and step values.
144+
*/
145+
export interface NormalizedSliceInterface extends SliceInterface {
146+
/**
147+
* The start index of the normalized slice.
148+
*/
149+
readonly start: number;
150+
/**
151+
* The end index of the normalized slice.
152+
*/
153+
readonly end: number;
154+
/**
155+
* The step size for selecting elements in the normalized slice.
156+
*/
157+
readonly step: number;
158+
/**
159+
* Returns the length of the normalized slice.
160+
*
161+
* @type {number}
162+
*/
163+
readonly length: number;
164+
165+
/**
166+
* Converts the provided index to the actual index based on the normalized slice parameters.
167+
*
168+
* @param {number} i - The index to convert.
169+
*
170+
* @returns {number} The converted index value.
171+
*/
172+
convertIndex(i: number): number;
173+
174+
/**
175+
* Generate an iterator for iterating over the elements in the normalized slice range.
176+
*
177+
* @returns {IterableIterator<number>} An iterator for the normalized slice range.
178+
*/
179+
toRange(): IterableIterator<number>;
180+
}
181+
108182
/**
109183
* Interface for selecting elements from an array view.
110184
*/
@@ -122,6 +196,31 @@ export interface ArraySelectorInterface {
122196
select<T>(source: ArrayViewInterface<T>, readonly?: boolean): ArrayViewInterface<T>;
123197
}
124198

199+
/**
200+
* Interface for selecting elements from an array view by boolean mask.
201+
*/
202+
export interface MaskSelectorInterface extends ArraySelectorInterface {
203+
/**
204+
* Boolean mask array.
205+
*/
206+
readonly value: Array<boolean>;
207+
}
208+
209+
/**
210+
* Interface for selecting elements from an array view by boolean mask.
211+
*/
212+
export interface IndexListSelectorInterface extends ArraySelectorInterface {
213+
/**
214+
* Index list array.
215+
*/
216+
readonly value: Array<number>;
217+
}
218+
219+
/**
220+
* Interface for selecting elements from an array view by slice.
221+
*/
222+
export interface SliceSelectorInterface extends ArraySelectorInterface, SliceInterface {}
223+
125224
/**
126225
* Type representing an array that can be sliced.
127226
*

0 commit comments

Comments
 (0)