MCPcopy Create free account
hub / github.com/denoland/std / slice

Method slice

data_structures/unstable_2d_array.ts:168–188  ·  view source on GitHub ↗

* Slice the 2D array. Like slice on a normal array, this will create a new D2Array. * If no arguments are specified, it will slice the entire D2Array. * * @example Usage * ```ts * import { D2Array } from "@std/data-structures/unstable-2d-array"; * import { assertEquals } from "@std

(
    x: number = 0,
    y: number = 0,
    width?: number,
    height?: number,
  )

Source from the content-addressed store, hash-verified

166 * @returns A new {@linkcode D2Array} containing the sliced values.
167 */
168 slice(
169 x: number = 0,
170 y: number = 0,
171 width?: number,
172 height?: number,
173 ): D2Array<T> {
174 const actualHeight = Math.min(height ?? Infinity, this.raw.length - y);
175 const actualWidth = Math.min(width ?? Infinity, this.raw[0]!.length - x);
176
177 assert((actualWidth as number) > 0, "Width must be greater than 0");
178 assert((actualHeight as number) > 0, "Height must be greater than 0");
179
180 const out: T[][] = [];
181
182 for (let i = y; i < y + actualHeight; i++) {
183 const row = this.raw[i]!;
184 out.push(row.slice(x, x + actualWidth));
185 }
186
187 return new D2Array(out, this.initialValue);
188 }
189
190 /**
191 * Resize the 2D array.

Callers 10

formatToFormatPartsFunction · 0.45
formatMethod · 0.45
formatToPartsMethod · 0.45
getMethod · 0.45
forEachMethod · 0.45
nextMethod · 0.45
toMapMethod · 0.45
constructorMethod · 0.45
resizeMethod · 0.45

Calls 3

assertFunction · 0.90
minMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected