Skip to content
VaelJS Blog

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"
Since v0.0.0-alpha.0 vjs (V8) vjs-lite (QuickJS)
Cargo feature: vael-slices (enabled by default in vaeljs-core and vaeljs-lite-core; requires vael-chars)
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"
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function slice<T extends SliceTarget>(target: T, start?: number, end?: number): Slice<T>;

Create a mutable live Slice over target[start..end).

Parameters
NameTypeDescription
targetTLive Array or genuine typed array.
start optionalnumberInclusive start index (default 0).
end optionalnumberExclusive end index (default target.length).
Returns
Slice<T> — Mutable Slice<T> view.
Exceptions
  • TypeError — when target is 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]); // 20
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function readonlySlice<T extends SliceTarget>(
  target: T,
  start?: number,
  end?: number,
): ReadOnlySlice<T>;

Create a read-only live ReadOnlySlice over target[start..end).

Parameters
NameTypeDescription
targetTLive Array or genuine typed array.
start optionalnumberInclusive start index (default 0).
end optionalnumberExclusive end index (default target.length).
Returns
ReadOnlySlice<T> — Read-only ReadOnlySlice<T> view.
Exceptions
  • TypeError — when target is invalid or a bound is not a number.
  • RangeError — when bounds are invalid or out of range.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function charSlice(buffer: Uint32Array, start?: number, end?: number): CharSlice;

Create a mutable CharSlice over buffer[start..end).

Parameters
NameTypeDescription
bufferUint32ArrayGenuine Uint32Array of Unicode code points.
start optionalnumberInclusive start code-point index (default 0).
end optionalnumberExclusive end code-point index (default buffer.length).
Returns
CharSlice — Mutable CharSlice view.
Exceptions
  • TypeError — when buffer is invalid or a bound is not a number.
  • RangeError — when bounds are invalid or out of range.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function readonlyCharSlice(
  buffer: Uint32Array,
  start?: number,
  end?: number,
): ReadOnlyCharSlice;

Create a read-only ReadOnlyCharSlice over buffer[start..end).

Parameters
NameTypeDescription
bufferUint32ArrayGenuine Uint32Array of Unicode code points.
start optionalnumberInclusive start code-point index (default 0).
end optionalnumberExclusive end code-point index (default buffer.length).
Returns
ReadOnlyCharSlice — Read-only ReadOnlyCharSlice view.
Exceptions
  • TypeError — when buffer is invalid or a bound is not a number.
  • RangeError — when bounds are invalid or out of range.
classSince v0.0.0-alpha.0vjs & vjs-lite
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
constructor(target: T, start?: number, end?: number);

Construct a mutable live view over target[start..end).

Parameters
NameTypeDescription
targetTLive Array or genuine typed array.
start optionalnumberInclusive start index (integer >= 0, default 0).
end optionalnumberExclusive end index (integer <= target.length, default target.length).
Exceptions
  • TypeError — when target is invalid or a bound is not a number.
  • RangeError — when bounds are non-integer, negative, reversed, or out of range.
Properties
PropertyTypeDescriptionSince
length readonlynumberFixed number of elements in the view.v0.0.0-alpha.0
isEmpty readonlybooleanWhether length === 0.v0.0.0-alpha.0
Methods
methodSince v0.0.0-alpha.0
at(index: number): SliceElement<T>;

Read the element at view index index.

Parameters
NameTypeDescription
indexnumberZero-based view element index in 0..length.
Returns
SliceElement<T> — Element at index.
Exceptions
  • TypeError — when index is not a number.
  • RangeError — when index is not an integer in 0..length.
methodSince v0.0.0-alpha.0
set(index: number, value: SliceElement<T>): this;

Write value through to the underlying target at view index index.

Parameters
NameTypeDescription
indexnumberZero-based view element index in 0..length.
valueSliceElement<T>New element value to write to the underlying target.
Returns
this — this for chaining.
Exceptions
  • TypeError — when index is not a number or target rejects write.
  • RangeError — when index is not an integer in 0..length.
methodSince v0.0.0-alpha.0
slice(start?: number, end?: number): Slice<T>;

Return a nested mutable view over [start, end) relative to this view.

Parameters
NameTypeDescription
start optionalnumberInclusive start index relative to this view (default 0).
end optionalnumberExclusive end index relative to this view (default this.length).
Returns
Slice<T> — Nested mutable Slice<T>.
Exceptions
  • TypeError — when a bound is not a number.
  • RangeError — when bounds are invalid or out of range.
methodSince v0.0.0-alpha.0
copy(): SliceCopy<T>;

Copy the current view range into a new Array or typed array of the same kind.

Returns
SliceCopy<T> — Fresh snapshot copy of this slice's elements.
Exceptions
  • RangeError — when a typed-array target was detached or shrunk below the fixed range.
methodSince v0.0.0-alpha.0
indexOf(value: SliceElement<T>): number;

Return the first view index strictly equal (===) to value, or -1.

Parameters
NameTypeDescription
valueSliceElement<T>Element value to search for.
Returns
number — First matching index in 0..length, or -1.
methodSince v0.0.0-alpha.0
includes(value: SliceElement<T>): boolean;

Return true if any view element matches value under SameValueZero (NaN matches).

Parameters
NameTypeDescription
valueSliceElement<T>Element value to search for.
Returns
boolean — true if present in this view.
methodSince v0.0.0-alpha.0
[Symbol.iterator](): IterableIterator<SliceElement<T>>;

Iterate current elements in view order.

Returns
IterableIterator<SliceElement<T>> — Iterator over elements in view order.
classSince v0.0.0-alpha.0vjs & vjs-lite
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
constructor(target: T, start?: number, end?: number);

Construct a read-only live view over target[start..end).

Parameters
NameTypeDescription
targetTLive Array or genuine typed array.
start optionalnumberInclusive start index (integer >= 0, default 0).
end optionalnumberExclusive end index (integer <= target.length, default target.length).
Exceptions
  • TypeError — when target is invalid or a bound is not a number.
  • RangeError — when bounds are non-integer, negative, reversed, or out of range.
Properties
PropertyTypeDescriptionSince
length readonlynumberFixed number of elements in the view.v0.0.0-alpha.0
isEmpty readonlybooleanWhether length === 0.v0.0.0-alpha.0
Methods
methodSince v0.0.0-alpha.0
at(index: number): SliceElement<T>;

Read the element at view index index.

Parameters
NameTypeDescription
indexnumberZero-based view element index in 0..length.
Returns
SliceElement<T> — Element at index.
Exceptions
  • TypeError — when index is not a number.
  • RangeError — when index is out of range.
methodSince v0.0.0-alpha.0
slice(start?: number, end?: number): ReadOnlySlice<T>;

Return a nested read-only view over [start, end) relative to this view.

Parameters
NameTypeDescription
start optionalnumberInclusive start index relative to this view (default 0).
end optionalnumberExclusive end index relative to this view (default this.length).
Returns
ReadOnlySlice<T> — Nested ReadOnlySlice<T>.
Exceptions
  • TypeError — when a bound is not a number.
  • RangeError — when bounds are invalid or out of range.
methodSince v0.0.0-alpha.0
copy(): SliceCopy<T>;

Copy the current view range into a new Array or typed array of the same kind.

Returns
SliceCopy<T> — Fresh snapshot copy of this slice's elements.
Exceptions
  • RangeError — when a typed-array target was detached or shrunk below the fixed range.
methodSince v0.0.0-alpha.0
indexOf(value: SliceElement<T>): number;

Return the first view index strictly equal (===) to value, or -1.

Parameters
NameTypeDescription
valueSliceElement<T>Element value to search for.
Returns
number — First matching index in 0..length, or -1.
methodSince v0.0.0-alpha.0
includes(value: SliceElement<T>): boolean;

Return true if any view element matches value under SameValueZero (NaN matches).

Parameters
NameTypeDescription
valueSliceElement<T>Element value to search for.
Returns
boolean — true if present in this view.
methodSince v0.0.0-alpha.0
[Symbol.iterator](): IterableIterator<SliceElement<T>>;

Iterate current elements in view order.

Returns
IterableIterator<SliceElement<T>> — Iterator over elements in view order.
classSince v0.0.0-alpha.0vjs & vjs-lite
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
constructor(buffer: Uint32Array, start?: number, end?: number);

Construct a CharSlice over buffer[start..end).

Parameters
NameTypeDescription
bufferUint32ArrayGenuine Uint32Array holding Unicode code points.
start optionalnumberInclusive start code-point index (default 0).
end optionalnumberExclusive end code-point index (default buffer.length).
Exceptions
  • TypeError — when buffer is not a genuine Uint32Array or a bound is not a number.
  • RangeError — when bounds are invalid or out of range.
Properties
PropertyTypeDescriptionSince
length readonlynumberFixed number of code points in the view.v0.0.0-alpha.0
isEmpty readonlybooleanWhether length === 0.v0.0.0-alpha.0
Methods
methodstaticSince v0.0.0-alpha.0
static fromString(text: string): CharSlice;

Allocate a Uint32Array of code points from text (combining surrogate pairs) and return a CharSlice over it.

Parameters
NameTypeDescription
textstringSource JavaScript string.
Returns
CharSlice — Mutable CharSlice backed by a newly allocated Uint32Array.
Exceptions
  • TypeError — when text is not a string.
import { CharSlice } from "vael:slices";
const cs = CharSlice.fromString("abc");
console.log(cs.toUpper().toString()); // "ABC"
methodSince v0.0.0-alpha.0
at(index: number): number;

Read the code point at view index index.

Parameters
NameTypeDescription
indexnumberZero-based code-point index in 0..length.
Returns
number — Numeric Unicode code point at index.
Exceptions
  • TypeError — when index is not a number.
  • RangeError — when index is out of range.
methodSince v0.0.0-alpha.0
set(index: number, value: number): this;

Write Unicode code point value (0..=0x10FFFF) at view index index.

Parameters
NameTypeDescription
indexnumberZero-based code-point index in 0..length.
valuenumberUnicode code point integer in 0..=0x10FFFF.
Returns
this — this for chaining.
Exceptions
  • TypeError — when index or value is not a number.
  • RangeError — when index or value is out of range.
methodSince v0.0.0-alpha.0
slice(start?: number, end?: number): CharSlice;

Return a nested mutable CharSlice over [start, end) relative to this view.

Parameters
NameTypeDescription
start optionalnumberInclusive start index relative to this view (default 0).
end optionalnumberExclusive end index relative to this view (default this.length).
Returns
CharSlice — Nested mutable CharSlice.
Exceptions
  • TypeError — when a bound is not a number.
  • RangeError — when bounds are invalid or out of range.
methodSince v0.0.0-alpha.0
toUpper(): this;

Map every Unicode scalar value in place using vael:chars toUpper. Expanding mappings and surrogates are left unchanged.

Returns
this — this for chaining.
methodSince v0.0.0-alpha.0
toLower(): this;

Map every Unicode scalar value in place using vael:chars toLower. Expanding mappings and surrogates are left unchanged.

Returns
this — this for chaining.
methodSince v0.0.0-alpha.0
toString(): string;

Build a new JavaScript string from the view’s code points.

Returns
string — Decoded string representing the slice's code points.
methodSince v0.0.0-alpha.0
toArray(): Uint32Array;

Copy the view’s code points into a new Uint32Array.

Returns
Uint32Array — New Uint32Array snapshot of code points.
methodSince v0.0.0-alpha.0
[Symbol.iterator](): IterableIterator<number>;

Iterate code points in view order.

Returns
IterableIterator<number> — Iterator over numeric code points.
classSince v0.0.0-alpha.0vjs & vjs-lite
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
constructor(buffer: Uint32Array, start?: number, end?: number);

Construct a ReadOnlyCharSlice over buffer[start..end).

Parameters
NameTypeDescription
bufferUint32ArrayGenuine Uint32Array holding Unicode code points.
start optionalnumberInclusive start code-point index (default 0).
end optionalnumberExclusive end code-point index (default buffer.length).
Exceptions
  • TypeError — when buffer is not a genuine Uint32Array or a bound is not a number.
  • RangeError — when bounds are invalid or out of range.
Properties
PropertyTypeDescriptionSince
length readonlynumberFixed number of code points in the view.v0.0.0-alpha.0
isEmpty readonlybooleanWhether length === 0.v0.0.0-alpha.0
Methods
methodstaticSince v0.0.0-alpha.0
static fromString(text: string): ReadOnlyCharSlice;

Allocate a Uint32Array of code points from text and return a ReadOnlyCharSlice over it.

Parameters
NameTypeDescription
textstringSource JavaScript string.
Returns
ReadOnlyCharSlice — Read-only ReadOnlyCharSlice backed by a newly allocated Uint32Array.
Exceptions
  • TypeError — when text is not a string.
methodSince v0.0.0-alpha.0
at(index: number): number;

Read the code point at view index index.

Parameters
NameTypeDescription
indexnumberZero-based code-point index in 0..length.
Returns
number — Numeric Unicode code point at index.
Exceptions
  • TypeError — when index is not a number.
  • RangeError — when index is out of range.
methodSince v0.0.0-alpha.0
slice(start?: number, end?: number): ReadOnlyCharSlice;

Return a nested read-only CharSlice over [start, end) relative to this view.

Parameters
NameTypeDescription
start optionalnumberInclusive start index relative to this view (default 0).
end optionalnumberExclusive end index relative to this view (default this.length).
Returns
ReadOnlyCharSlice — Nested ReadOnlyCharSlice.
Exceptions
  • TypeError — when a bound is not a number.
  • RangeError — when bounds are invalid or out of range.
methodSince v0.0.0-alpha.0
toString(): string;

Build a new JavaScript string from the view’s code points.

Returns
string — Decoded string representing the slice's code points.
methodSince v0.0.0-alpha.0
toArray(): Uint32Array;

Copy the view’s code points into a new Uint32Array.

Returns
Uint32Array — New Uint32Array snapshot of code points.
methodSince v0.0.0-alpha.0
[Symbol.iterator](): IterableIterator<number>;

Iterate code points in view order.

Returns
IterableIterator<number> — Iterator over numeric code points.
typeSince v0.0.0-alpha.0vjs & vjs-lite
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).

typeSince v0.0.0-alpha.0vjs & vjs-lite
export type SliceTarget<E = unknown> = E[] | TypedArray;

Valid live target for Slice and ReadOnlySlice.

typeSince v0.0.0-alpha.0vjs & vjs-lite
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>.

typeSince v0.0.0-alpha.0vjs & vjs-lite
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).