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

Method resize

data_structures/unstable_2d_array.ts:214–250  ·  view source on GitHub ↗

* Resize the 2D array. * The origin for the resize is the top left corner, or rather the first element. * * @example Usage * ```ts * import { D2Array } from "@std/data-structures/unstable-2d-array"; * import { assertEquals } from "@std/assert"; * * const arr = new D2Array<boo

(width: number, height: number)

Source from the content-addressed store, hash-verified

212 * If larger than current height, the 2d array will add instances of {@linkcode D2Array.prototype.initialValue | `initialValue`} to fill up.
213 */
214 resize(width: number, height: number) {
215 assert((width as number) > 0, "Width must be greater than 0");
216 assert((height as number) > 0, "Height must be greater than 0");
217
218 if (height == this.raw.length && width == this.raw[0]!.length) {
219 return;
220 } else if (height < this.raw.length && width < this.raw[0]!.length) {
221 this.raw = this.raw.slice(0, height);
222
223 for (let i = 0; i < this.raw.length; i++) {
224 this.raw[i] = this.raw[i]!.slice(0, width);
225 }
226 } else {
227 if (width <= this.raw[0]!.length) {
228 for (let i = 0; i < this.raw.length; i++) {
229 this.raw[i] = this.raw[i]!.slice(0, width);
230 }
231 } else {
232 for (let i = 0; i < this.raw.length; i++) {
233 this.raw[i]!.push(
234 ...Array(width - this.raw[i]!.length).fill(this.initialValue),
235 );
236 }
237 }
238
239 if (height <= this.raw.length) {
240 this.raw = this.raw.slice(0, height);
241 } else {
242 this.raw.push(
243 ...Array.from(
244 { length: height - this.raw.length },
245 () => Array(this.raw[0]!.length).fill(this.initialValue),
246 ),
247 );
248 }
249 }
250 }
251
252 /**
253 * Insert another 2d array at specific coordinates.

Callers 1

Calls 4

assertFunction · 0.90
sliceMethod · 0.45
pushMethod · 0.45
fromMethod · 0.45

Tested by

no test coverage detected