implementation * TODO(kt3k): Remove this jsdoc when the issue below is resolved. * https://github.com/denoland/deno/issues/30037 * * @param widthOrValue The width of the 2d array, or the array to use * @param heightOrInitialValue The height of the 2d array, or the initial value to use
(
widthOrValue: number | (readonly T[][]),
heightOrInitialValue: number | T,
initialValue?: T,
)
| 112 | * @param initialValue The initial value to use when resizing the |
| 113 | */ |
| 114 | constructor( |
| 115 | widthOrValue: number | (readonly T[][]), |
| 116 | heightOrInitialValue: number | T, |
| 117 | initialValue?: T, |
| 118 | ) { |
| 119 | if (Array.isArray(widthOrValue)) { |
| 120 | assert((widthOrValue.length) > 0, "Height must be greater than 0"); |
| 121 | assert((widthOrValue[0].length) > 0, "Width must be greater than 0"); |
| 122 | |
| 123 | this.raw = []; |
| 124 | |
| 125 | for (let i = 0; i < widthOrValue.length; i++) { |
| 126 | this.raw.push(widthOrValue[i]!.slice()); |
| 127 | } |
| 128 | |
| 129 | this.initialValue = heightOrInitialValue as T; |
| 130 | } else { |
| 131 | assert((widthOrValue as number) > 0, "Width must be greater than 0"); |
| 132 | assert( |
| 133 | (heightOrInitialValue as number) > 0, |
| 134 | "Height must be greater than 0", |
| 135 | ); |
| 136 | |
| 137 | this.raw = Array.from( |
| 138 | { length: heightOrInitialValue as number }, |
| 139 | () => Array(widthOrValue as number).fill(initialValue as number), |
| 140 | ); |
| 141 | this.initialValue = initialValue!; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Slice the 2D array. Like slice on a normal array, this will create a new D2Array. |