| const Cast = require('../../util/cast'); |
| const Clone = require('../../util/clone'); |
|
|
| class Seperator { |
| static _createArrayOfLength(length, item) { |
| length = Cast.toNumber(length); |
| if (length <= 0) return []; |
| if (!isFinite(length)) return []; |
| const newArray = Array.from(Array(length).keys()).map(() => { |
| return item; |
| }); |
| return Clone.simple(newArray); |
| } |
| static _validateUnsignedInteger(int) { |
| int = Cast.toNumber(int); |
| if (int < 0) int = 0; |
| if (!isFinite(int)) int = 0; |
| return Math.round(int); |
| } |
| static _splitNumber(num) { |
| const number = Math.round(Cast.toNumber(num)); |
| if (number === 0) return [0, 0]; |
| if (!isFinite(number)) return [0, 0]; |
| return [ |
| Math.ceil(number / 2), |
| Math.floor(number / 2) |
| ]; |
| } |
| |
| |
| |
| |
| |
| |
| static marginGrid(grid, amount) { |
| |
| amount = Seperator._validateUnsignedInteger(amount); |
|
|
| const newGrid = Clone.simple(grid); |
| |
| const gridLength = Seperator._validateUnsignedInteger(newGrid[0] ? newGrid[0].length : 0); |
| const fillerRow = Seperator._createArrayOfLength(gridLength, 0); |
| |
| for (let i = 0; i < amount; i++) { |
| |
| |
| |
| const first = Clone.simple(fillerRow); |
| const last = Clone.simple(fillerRow); |
| newGrid.unshift(first); |
| newGrid.push(last); |
| } |
| |
| |
| for (const row of newGrid) { |
| for (let i = 0; i < amount; i++) { |
| row.unshift(0); |
| row.push(0); |
| } |
| } |
| return newGrid; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| static padGrid(grid, width, height) { |
| |
| |
| const reasonableW = Math.max(0, Cast.toNumber(width) - 1); |
| const reasonableH = Math.max(0, Cast.toNumber(height) - 1); |
| const splitW = Seperator._splitNumber(reasonableW); |
| const splitH = Seperator._splitNumber(reasonableH); |
| |
| const originalGrid = Clone.simple(grid); |
| const newGrid = Clone.simple(grid); |
| |
| const idx = { |
| row: 0, |
| tile: 0 |
| } |
| for (const row of newGrid) { |
| for (const _ of row) { |
| |
| if (originalGrid[idx.row][idx.tile] <= 0) { |
| idx.tile++; |
| continue; |
| } |
| |
| |
| |
| for (let i = 0; i < splitW[0]; i++) { |
| |
| const nextTile = idx.tile + (i + 1); |
| |
| |
| |
| if (typeof row[nextTile] !== 'number') continue; |
| |
| row[nextTile] = 1; |
| } |
| for (let i = 0; i < splitW[1]; i++) { |
| |
| const nextTile = idx.tile - (i + 1); |
| |
| |
| |
| if (typeof row[nextTile] !== 'number') continue; |
| |
| row[nextTile] = 1; |
| } |
| |
| for (let i = 0; i < splitH[0]; i++) { |
| |
| const nextRow = idx.row - (i + 1); |
| |
| |
| |
| if (!originalGrid[nextRow]) continue; |
| |
| const foundRow = newGrid[nextRow]; |
| |
| foundRow[idx.tile] = 1; |
| } |
| for (let i = 0; i < splitH[1]; i++) { |
| |
| const nextRow = idx.row + (i + 1); |
| |
| |
| |
| if (!originalGrid[nextRow]) continue; |
| |
| const foundRow = newGrid[nextRow]; |
| |
| foundRow[idx.tile] = 1; |
| } |
| |
| |
| |
| |
| for (let i = 0; i < 2; i++) { |
| const isBottom = i === 1; |
| for (let j = 0; j < splitH[isBottom ? 1 : 0]; j++) { |
| |
| let nextRow = idx.row - (j + 1); |
| if (isBottom) nextRow = idx.row + (j + 1); |
| |
| |
| |
| if (!originalGrid[nextRow]) continue; |
| |
| const foundRow = newGrid[nextRow]; |
| for (let k = 0; k < 2; k++) { |
| const isLeft = k === 1; |
| for (let l = 0; l < splitW[isLeft ? 1 : 0]; l++) { |
| |
| let nextTile = idx.tile + (l + 1); |
| if (isLeft) nextTile = idx.tile - (l + 1); |
| |
| |
| |
| if (typeof foundRow[nextTile] !== 'number') continue; |
| |
| foundRow[nextTile] = 1; |
| } |
| } |
| } |
| } |
| |
| idx.tile++; |
| } |
| |
| idx.row++; |
| idx.tile = 0; |
| } |
| return newGrid; |
| } |
| } |
|
|
| module.exports = Seperator; |