vael:slices
Mutable and read-only live views over arrays, typed arrays, and Unicode code-point slices.
Available in both vjs (vaeljs) and vjs-lite (vaeljs-lite) runtimes.
Provides zero-copy [start, end) views over Array and typed arrays
(Slice, ReadOnlySlice) as well as Unicode code-point views over
Uint32Array (CharSlice, ReadOnlyCharSlice).
import ... from "vael:slices"
vael-slices (enabled by default in vaeljs-core and vaeljs-lite-core; requires vael-chars)Example
Section titled “Example”import { CharSlice, slice } from "vael:slices";
const nums = [10, 20, 30, 40];const middle = slice(nums, 1, 3);middle.set(0, 99);console.log(nums); // [10, 99, 30, 40]
const chars = CharSlice.fromString("hello");chars.slice(0, 1).toUpper();console.log(chars.toString()); // "Hello"slicev0.0.0-alpha.0Slice over target[start..end).readonlySlicev0.0.0-alpha.0ReadOnlySlice over target[start..end).charSlicev0.0.0-alpha.0CharSlice over buffer[start..end).readonlyCharSlicev0.0.0-alpha.0ReadOnlyCharSlice over buffer[start..end).Slicev0.0.0-alpha.0[start, end) range of an Array or genuine typed array.ReadOnlySlicev0.0.0-alpha.0[start, end) range of an Array or genuine typed array.CharSlicev0.0.0-alpha.0Uint32Array.ReadOnlyCharSlicev0.0.0-alpha.0Uint32Array.TypedArrayv0.0.0-alpha.0Slice and ReadOnlySlice (excludes DataView and proxies of typed arrays).SliceTargetv0.0.0-alpha.0Slice and ReadOnlySlice.SliceElementv0.0.0-alpha.0Slice<T> / ReadOnlySlice<T>.SliceCopyv0.0.0-alpha.0copy() (a new plain array for array targets, or a new intrinsic typed array of the same type).Functions
Section titled “Functions”export function slice<T extends SliceTarget>(target: T, start?: number, end?: number): Slice<T>;
Create a mutable live Slice over target[start..end).
| Name | Type | Description |
|---|---|---|
target | T | Live Array or genuine typed array. |
start optional | number | Inclusive start index (default 0). |
end optional | number | Exclusive end index (default target.length). |
Slice<T> — Mutable Slice<T> view.TypeError— whentargetis invalid or a bound is not a number.RangeError— when bounds are invalid or out of range.
import { slice } from "vael:slices";
const buf = new Uint8Array([1, 2, 3, 4]);const head = slice(buf, 0, 2);head.set(1, 20);console.log(buf[1]); // 20readonlySlice
Section titled “readonlySlice”export function readonlySlice<T extends SliceTarget>(
target: T,
start?: number,
end?: number,
): ReadOnlySlice<T>;
Create a read-only live ReadOnlySlice over target[start..end).
| Name | Type | Description |
|---|---|---|
target | T | Live Array or genuine typed array. |
start optional | number | Inclusive start index (default 0). |
end optional | number | Exclusive end index (default target.length). |
ReadOnlySlice<T> — Read-only ReadOnlySlice<T> view.TypeError— whentargetis invalid or a bound is not a number.RangeError— when bounds are invalid or out of range.
charSlice
Section titled “charSlice”export function charSlice(buffer: Uint32Array, start?: number, end?: number): CharSlice;
Create a mutable CharSlice over buffer[start..end).
| Name | Type | Description |
|---|---|---|
buffer | Uint32Array | Genuine Uint32Array of Unicode code points. |
start optional | number | Inclusive start code-point index (default 0). |
end optional | number | Exclusive end code-point index (default buffer.length). |
CharSlice — Mutable CharSlice view.TypeError— whenbufferis invalid or a bound is not a number.RangeError— when bounds are invalid or out of range.
readonlyCharSlice
Section titled “readonlyCharSlice”export function readonlyCharSlice(
buffer: Uint32Array,
start?: number,
end?: number,
): ReadOnlyCharSlice;
Create a read-only ReadOnlyCharSlice over buffer[start..end).
| Name | Type | Description |
|---|---|---|
buffer | Uint32Array | Genuine Uint32Array of Unicode code points. |
start optional | number | Inclusive start code-point index (default 0). |
end optional | number | Exclusive end code-point index (default buffer.length). |
ReadOnlyCharSlice — Read-only ReadOnlyCharSlice view.TypeError— whenbufferis invalid or a bound is not a number.RangeError— when bounds are invalid or out of range.
Classes
Section titled “Classes”export class Slice<T extends SliceTarget = SliceTarget> {
constructor(target: T, start?: number, end?: number);
readonly length: number;
readonly isEmpty: boolean;
at(index: number): SliceElement<T>;
set(index: number, value: SliceElement<T>): this;
slice(start?: number, end?: number): Slice<T>;
copy(): SliceCopy<T>;
indexOf(value: SliceElement<T>): number;
includes(value: SliceElement<T>): boolean;
[Symbol.iterator](): IterableIterator<SliceElement<T>>;
}
Mutable live view over a fixed [start, end) range of an Array or genuine
typed array.
Construction, slice, at, set, indexOf, includes, and iteration do
not copy; copy() is the explicit snapshot operation.
constructor(target: T, start?: number, end?: number);
Construct a mutable live view over target[start..end).
| Name | Type | Description |
|---|---|---|
target | T | Live Array or genuine typed array. |
start optional | number | Inclusive start index (integer >= 0, default 0). |
end optional | number | Exclusive end index (integer <= target.length, default target.length). |
TypeError— whentargetis invalid or a bound is not a number.RangeError— when bounds are non-integer, negative, reversed, or out of range.
| Property | Type | Description | Since |
|---|---|---|---|
length readonly | number | Fixed number of elements in the view. | v0.0.0-alpha.0 |
isEmpty readonly | boolean | Whether length === 0. | v0.0.0-alpha.0 |
Slice.at
Section titled “Slice.at”at(index: number): SliceElement<T>;
Read the element at view index index.
| Name | Type | Description |
|---|---|---|
index | number | Zero-based view element index in 0..length. |
SliceElement<T> — Element at index.TypeError— whenindexis not a number.RangeError— whenindexis not an integer in0..length.
Slice.set
Section titled “Slice.set”set(index: number, value: SliceElement<T>): this;
Write value through to the underlying target at view index index.
| Name | Type | Description |
|---|---|---|
index | number | Zero-based view element index in 0..length. |
value | SliceElement<T> | New element value to write to the underlying target. |
this — this for chaining.TypeError— whenindexis not a number or target rejects write.RangeError— whenindexis not an integer in0..length.
Slice.slice
Section titled “Slice.slice”slice(start?: number, end?: number): Slice<T>;
Return a nested mutable view over [start, end) relative to this view.
| Name | Type | Description |
|---|---|---|
start optional | number | Inclusive start index relative to this view (default 0). |
end optional | number | Exclusive end index relative to this view (default this.length). |
Slice<T> — Nested mutable Slice<T>.TypeError— when a bound is not a number.RangeError— when bounds are invalid or out of range.
Slice.copy
Section titled “Slice.copy”copy(): SliceCopy<T>;
Copy the current view range into a new Array or typed array of the same kind.
SliceCopy<T> — Fresh snapshot copy of this slice's elements.RangeError— when a typed-array target was detached or shrunk below the fixed range.
Slice.indexOf
Section titled “Slice.indexOf”indexOf(value: SliceElement<T>): number;
Return the first view index strictly equal (===) to value, or -1.
| Name | Type | Description |
|---|---|---|
value | SliceElement<T> | Element value to search for. |
number — First matching index in 0..length, or -1.Slice.includes
Section titled “Slice.includes”includes(value: SliceElement<T>): boolean;
Return true if any view element matches value under SameValueZero (NaN matches).
| Name | Type | Description |
|---|---|---|
value | SliceElement<T> | Element value to search for. |
boolean — true if present in this view.Slice.[Symbol.iterator]
Section titled “Slice.[Symbol.iterator]”[Symbol.iterator](): IterableIterator<SliceElement<T>>;
Iterate current elements in view order.
IterableIterator<SliceElement<T>> — Iterator over elements in view order.ReadOnlySlice
Section titled “ReadOnlySlice”export class ReadOnlySlice<T extends SliceTarget = SliceTarget> {
constructor(target: T, start?: number, end?: number);
readonly length: number;
readonly isEmpty: boolean;
at(index: number): SliceElement<T>;
slice(start?: number, end?: number): ReadOnlySlice<T>;
copy(): SliceCopy<T>;
indexOf(value: SliceElement<T>): number;
includes(value: SliceElement<T>): boolean;
[Symbol.iterator](): IterableIterator<SliceElement<T>>;
}
Read-only live view over a fixed [start, end) range of an Array or
genuine typed array.
Reads observe writes made through the target or another view.
constructor(target: T, start?: number, end?: number);
Construct a read-only live view over target[start..end).
| Name | Type | Description |
|---|---|---|
target | T | Live Array or genuine typed array. |
start optional | number | Inclusive start index (integer >= 0, default 0). |
end optional | number | Exclusive end index (integer <= target.length, default target.length). |
TypeError— whentargetis invalid or a bound is not a number.RangeError— when bounds are non-integer, negative, reversed, or out of range.
| Property | Type | Description | Since |
|---|---|---|---|
length readonly | number | Fixed number of elements in the view. | v0.0.0-alpha.0 |
isEmpty readonly | boolean | Whether length === 0. | v0.0.0-alpha.0 |
ReadOnlySlice.at
Section titled “ReadOnlySlice.at”at(index: number): SliceElement<T>;
Read the element at view index index.
| Name | Type | Description |
|---|---|---|
index | number | Zero-based view element index in 0..length. |
SliceElement<T> — Element at index.TypeError— whenindexis not a number.RangeError— whenindexis out of range.
ReadOnlySlice.slice
Section titled “ReadOnlySlice.slice”slice(start?: number, end?: number): ReadOnlySlice<T>;
Return a nested read-only view over [start, end) relative to this view.
| Name | Type | Description |
|---|---|---|
start optional | number | Inclusive start index relative to this view (default 0). |
end optional | number | Exclusive end index relative to this view (default this.length). |
ReadOnlySlice<T> — Nested ReadOnlySlice<T>.TypeError— when a bound is not a number.RangeError— when bounds are invalid or out of range.
ReadOnlySlice.copy
Section titled “ReadOnlySlice.copy”copy(): SliceCopy<T>;
Copy the current view range into a new Array or typed array of the same kind.
SliceCopy<T> — Fresh snapshot copy of this slice's elements.RangeError— when a typed-array target was detached or shrunk below the fixed range.
ReadOnlySlice.indexOf
Section titled “ReadOnlySlice.indexOf”indexOf(value: SliceElement<T>): number;
Return the first view index strictly equal (===) to value, or -1.
| Name | Type | Description |
|---|---|---|
value | SliceElement<T> | Element value to search for. |
number — First matching index in 0..length, or -1.ReadOnlySlice.includes
Section titled “ReadOnlySlice.includes”includes(value: SliceElement<T>): boolean;
Return true if any view element matches value under SameValueZero (NaN matches).
| Name | Type | Description |
|---|---|---|
value | SliceElement<T> | Element value to search for. |
boolean — true if present in this view.ReadOnlySlice.[Symbol.iterator]
Section titled “ReadOnlySlice.[Symbol.iterator]”[Symbol.iterator](): IterableIterator<SliceElement<T>>;
Iterate current elements in view order.
IterableIterator<SliceElement<T>> — Iterator over elements in view order.CharSlice
Section titled “CharSlice”export class CharSlice {
constructor(buffer: Uint32Array, start?: number, end?: number);
static fromString(text: string): CharSlice;
readonly length: number;
readonly isEmpty: boolean;
at(index: number): number;
set(index: number, value: number): this;
slice(start?: number, end?: number): CharSlice;
toUpper(): this;
toLower(): this;
toString(): string;
toArray(): Uint32Array;
[Symbol.iterator](): IterableIterator<number>;
}
Mutable live view over Unicode code points in a caller-owned Uint32Array.
constructor(buffer: Uint32Array, start?: number, end?: number);
Construct a CharSlice over buffer[start..end).
| Name | Type | Description |
|---|---|---|
buffer | Uint32Array | Genuine Uint32Array holding Unicode code points. |
start optional | number | Inclusive start code-point index (default 0). |
end optional | number | Exclusive end code-point index (default buffer.length). |
TypeError— whenbufferis not a genuineUint32Arrayor a bound is not a number.RangeError— when bounds are invalid or out of range.
| Property | Type | Description | Since |
|---|---|---|---|
length readonly | number | Fixed number of code points in the view. | v0.0.0-alpha.0 |
isEmpty readonly | boolean | Whether length === 0. | v0.0.0-alpha.0 |
CharSlice.fromString
Section titled “CharSlice.fromString”static fromString(text: string): CharSlice;
Allocate a Uint32Array of code points from text (combining surrogate
pairs) and return a CharSlice over it.
| Name | Type | Description |
|---|---|---|
text | string | Source JavaScript string. |
TypeError— whentextis not a string.
import { CharSlice } from "vael:slices";
const cs = CharSlice.fromString("abc");console.log(cs.toUpper().toString()); // "ABC"CharSlice.at
Section titled “CharSlice.at”at(index: number): number;
Read the code point at view index index.
| Name | Type | Description |
|---|---|---|
index | number | Zero-based code-point index in 0..length. |
number — Numeric Unicode code point at index.TypeError— whenindexis not a number.RangeError— whenindexis out of range.
CharSlice.set
Section titled “CharSlice.set”set(index: number, value: number): this;
Write Unicode code point value (0..=0x10FFFF) at view index index.
| Name | Type | Description |
|---|---|---|
index | number | Zero-based code-point index in 0..length. |
value | number | Unicode code point integer in 0..=0x10FFFF. |
this — this for chaining.TypeError— whenindexorvalueis not a number.RangeError— whenindexorvalueis out of range.
CharSlice.slice
Section titled “CharSlice.slice”slice(start?: number, end?: number): CharSlice;
Return a nested mutable CharSlice over [start, end) relative to this view.
| Name | Type | Description |
|---|---|---|
start optional | number | Inclusive start index relative to this view (default 0). |
end optional | number | Exclusive end index relative to this view (default this.length). |
CharSlice — Nested mutable CharSlice.TypeError— when a bound is not a number.RangeError— when bounds are invalid or out of range.
CharSlice.toUpper
Section titled “CharSlice.toUpper”toUpper(): this;
Map every Unicode scalar value in place using vael:chars toUpper.
Expanding mappings and surrogates are left unchanged.
this — this for chaining.CharSlice.toLower
Section titled “CharSlice.toLower”toLower(): this;
Map every Unicode scalar value in place using vael:chars toLower.
Expanding mappings and surrogates are left unchanged.
this — this for chaining.CharSlice.toString
Section titled “CharSlice.toString”toString(): string;
Build a new JavaScript string from the view’s code points.
string — Decoded string representing the slice's code points.CharSlice.toArray
Section titled “CharSlice.toArray”toArray(): Uint32Array;
Copy the view’s code points into a new Uint32Array.
Uint32Array — New Uint32Array snapshot of code points.CharSlice.[Symbol.iterator]
Section titled “CharSlice.[Symbol.iterator]”[Symbol.iterator](): IterableIterator<number>;
Iterate code points in view order.
IterableIterator<number> — Iterator over numeric code points.ReadOnlyCharSlice
Section titled “ReadOnlyCharSlice”export class ReadOnlyCharSlice {
constructor(buffer: Uint32Array, start?: number, end?: number);
static fromString(text: string): ReadOnlyCharSlice;
readonly length: number;
readonly isEmpty: boolean;
at(index: number): number;
slice(start?: number, end?: number): ReadOnlyCharSlice;
toString(): string;
toArray(): Uint32Array;
[Symbol.iterator](): IterableIterator<number>;
}
Read-only live view over Unicode code points in a caller-owned Uint32Array.
constructor(buffer: Uint32Array, start?: number, end?: number);
Construct a ReadOnlyCharSlice over buffer[start..end).
| Name | Type | Description |
|---|---|---|
buffer | Uint32Array | Genuine Uint32Array holding Unicode code points. |
start optional | number | Inclusive start code-point index (default 0). |
end optional | number | Exclusive end code-point index (default buffer.length). |
TypeError— whenbufferis not a genuineUint32Arrayor a bound is not a number.RangeError— when bounds are invalid or out of range.
| Property | Type | Description | Since |
|---|---|---|---|
length readonly | number | Fixed number of code points in the view. | v0.0.0-alpha.0 |
isEmpty readonly | boolean | Whether length === 0. | v0.0.0-alpha.0 |
ReadOnlyCharSlice.fromString
Section titled “ReadOnlyCharSlice.fromString”static fromString(text: string): ReadOnlyCharSlice;
Allocate a Uint32Array of code points from text and return a ReadOnlyCharSlice over it.
| Name | Type | Description |
|---|---|---|
text | string | Source JavaScript string. |
TypeError— whentextis not a string.
ReadOnlyCharSlice.at
Section titled “ReadOnlyCharSlice.at”at(index: number): number;
Read the code point at view index index.
| Name | Type | Description |
|---|---|---|
index | number | Zero-based code-point index in 0..length. |
number — Numeric Unicode code point at index.TypeError— whenindexis not a number.RangeError— whenindexis out of range.
ReadOnlyCharSlice.slice
Section titled “ReadOnlyCharSlice.slice”slice(start?: number, end?: number): ReadOnlyCharSlice;
Return a nested read-only CharSlice over [start, end) relative to this view.
| Name | Type | Description |
|---|---|---|
start optional | number | Inclusive start index relative to this view (default 0). |
end optional | number | Exclusive end index relative to this view (default this.length). |
ReadOnlyCharSlice — Nested ReadOnlyCharSlice.TypeError— when a bound is not a number.RangeError— when bounds are invalid or out of range.
ReadOnlyCharSlice.toString
Section titled “ReadOnlyCharSlice.toString”toString(): string;
Build a new JavaScript string from the view’s code points.
string — Decoded string representing the slice's code points.ReadOnlyCharSlice.toArray
Section titled “ReadOnlyCharSlice.toArray”toArray(): Uint32Array;
Copy the view’s code points into a new Uint32Array.
Uint32Array — New Uint32Array snapshot of code points.ReadOnlyCharSlice.[Symbol.iterator]
Section titled “ReadOnlyCharSlice.[Symbol.iterator]”[Symbol.iterator](): IterableIterator<number>;
Iterate code points in view order.
IterableIterator<number> — Iterator over numeric code points.Type Aliases
Section titled “Type Aliases”TypedArray
Section titled “TypedArray”export type TypedArray =
| Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array
| BigInt64Array
| BigUint64Array;
Genuine typed array types accepted by Slice and ReadOnlySlice (excludes
DataView and proxies of typed arrays).
SliceTarget
Section titled “SliceTarget”export type SliceTarget<E = unknown> = E[] | TypedArray;
Valid live target for Slice and ReadOnlySlice.
SliceElement
Section titled “SliceElement”export type SliceElement<T extends SliceTarget> =
T extends readonly (infer E)[]
? E
: T extends BigInt64Array | BigUint64Array
? bigint
: number;
Element type yielded by reading or iterating Slice<T> / ReadOnlySlice<T>.
SliceCopy
Section titled “SliceCopy”export type SliceCopy<T extends SliceTarget> = T extends readonly (infer E)[] ? E[] : T;
Snapshot type returned by copy() (a new plain array for array targets, or
a new intrinsic typed array of the same type).
