core module
createGrid
createGrid<T>(props: CreateGridProps<T>): Grid<T>
Creates a new immutable grid instance.
- props: CreateGridProps<T>The properties to create a new grid.
const grid = createGrid({
columnCount: 3,
rowCount: 4,
x: 1,
y: 2,
createCell: (_pos, idx) => idx,
});
// => {
// x: 1,
// y: 2,
// cellCount: 12,
// columnCount: 4,
// rowCount: 3,
// array2D: [
// [0, 1, 2, 3],
// [4, 5, 6, 7],
// [8, 9, 10, 11],
// ],
// }
createGridFromArray2D
createGridFromArray2D<T>(props: CreateGridFromArray2DProps<T>): Grid<T>
Creates a new grid instance from an existing two-dimensional grid array and adds a position.
- props: CreateGridFromArray2DProps<T>The props to create a grid from an 2d array.
const grid = createGridFromArray2D({
x: 5,
y: 6,
array2D: [
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
],
});
// => {
// x: 5,
// y: 6,
// cellCount: 12,
// columnCount: 4,
// rowCount: 3,
// array2D: [
// [0, 1, 2, 3],
// [4, 5, 6, 7],
// [8, 9, 10, 11],
// ]
// }
createGridFromArray2D<T>(array2D: T[][]): Grid<T>
Creates a new grid instance from an existing two-dimensional grid array and sets the position to the default values of {x: 0, y: 0}.
- array2D: T[][]The array from which to create the new grid instance.
const grid = createGridFromArray2D([
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
]);
// => {
// x: 0,
// y: 0,
// cellCount: 12,
// columnCount: 4,
// rowCount: 3,
// array2D: [
// [0, 1, 2, 3],
// [4, 5, 6, 7],
// [8, 9, 10, 11],
// ]
// }
createWalker
createWalker(shape: AdvancedShape, walk: GridWalker): Generator<GridIterationResult, any, unknown>
Creates a walker that iterates over all cells with an index starting from `0` to `cellCount - 1`.
- shape: AdvancedShapeThe shape of the grid that is traversed.
- walk: GridWalkerFunction that converts the iteration index into a grid position and thus determines the order in which the iterator traverses the cells. If the function is omitted the default walker walkDefault is used.
const shape = {columnCount: 3, rowCount: 2, cellCount: 6};
const walker = createWalker(shape);
walker.next(); // => {value: {index: 0, position: {x: 0, y: 0}}, done: false}
walker.next(); // => {value: {index: 1, position: {x: 1, y: 0}}, done: false}
walker.next(); // => {value: {index: 2, position: {x: 2, y: 0}}, done: false}
walker.next(); // => {value: {index: 3, position: {x: 0, y: 1}}, done: false}
walker.next(); // => {value: {index: 4, position: {x: 1, y: 1}}, done: false}
walker.next(); // => {value: {index: 5, position: {x: 2, y: 1}}, done: false}
walker.next(); // => {value: undefined, done: true}
selectCell
selectCell<T>(props: SelectCellProps<T>): undefined | T
Returns the cell value at the given position.
- props: SelectCellProps<T>The properties.
selectColumn
selectColumn<T>(props: SelectColumnProps<T>): undefined | T[]
Returns the column at the given x-position or undefined if the column doesn't exist.
- props: SelectColumnProps<T>The properties.
selectNeighbours
selectNeighbours<T>(props: SelectNeighboursProps<T>): SelectNeighboursResult<undefined | T>[]
Returns the neighbouring cells of the given origin with the grid.
- props: SelectNeighboursProps<T>
selectRow
selectRow<T>(props: SelectRowProps<T>): undefined | T[]
Returns the row at the given y-position or undefined if the row doesn't exist.
- props: SelectRowProps<T>The properties.
selectSubGrid
selectSubGrid<T>(props: SelectSubGridProps<T>): Readonly<GridObject<undefined | T>>
Returns a subset of the grid defined by given coordinates.
- props: SelectSubGridProps<T>The properties.
walkDefault
walkDefault(shape: AdvancedShape, index: number): Position
Alias of walkWENS
- shape: AdvancedShape
- index: number
walkEWNS
walkEWNS(shape: AdvancedShape, index: number): Position
Converts a grid index into a grid position, going from east to west and north to south (EWNS).
- shape: AdvancedShapeThe shape to iterate over. Note that since a grid implements the Shape interface, you can just pass in the grid as well.
- index: numberThe index to be converted.
const grid = createGridFromArray2D([
[ 2, 1, 0],
[ 5, 4, 3],
[ 8, 7, 6],
[11, 10, 9],
]);
walkEWNS(grid, 0); // => {x: 2, y: 0}
walkEWNS(grid, 1); // => {x: 1, y: 0}
walkEWNS(grid, 2); // => {x: 0, y: 0}
walkEWNS(grid, 3); // => {x: 2, y: 1}
walkEWNS(grid, 4); // => {x: 1, y: 1}
walkEWNS(grid, 5); // => {x: 0, y: 1}
walkEWNS(grid, 6); // => {x: 2, y: 2}
walkEWNS(grid, 7); // => {x: 1, y: 2}
walkEWNS(grid, 8); // => {x: 0, y: 2}
walkEWNS(grid, 9); // => {x: 2, y: 3}
walkEWNS(grid, 10); // => {x: 1, y: 3}
walkEWNS(grid, 11); // => {x: 0, y: 3}
walkEWSN
walkEWSN(shape: AdvancedShape, index: number): Position
Converts a grid index into a grid position, going from east to west and south to north (EWSN).
- shape: AdvancedShapeThe grid shape to iterate. Note that since a grid implements the Shape interface, you can just provide a grid instance as well.
- index: numberThe index to be converted.
const grid = createGridFromArray2D([
[11, 10, 9],
[ 8, 7, 6],
[ 5, 4, 3],
[ 2, 1, 0],
]);
walkEWSN(grid, 0); // => {x: 2, y: 3}
walkEWSN(grid, 1); // => {x: 1, y: 3}
walkEWSN(grid, 2); // => {x: 0, y: 3}
walkEWSN(grid, 3); // => {x: 2, y: 2}
walkEWSN(grid, 4); // => {x: 1, y: 2}
walkEWSN(grid, 5); // => {x: 0, y: 2}
walkEWSN(grid, 6); // => {x: 2, y: 1}
walkEWSN(grid, 7); // => {x: 1, y: 1}
walkEWSN(grid, 8); // => {x: 0, y: 1}
walkEWSN(grid, 9); // => {x: 2, y: 0}
walkEWSN(grid, 10); // => {x: 1, y: 0}
walkEWSN(grid, 11); // => {x: 0, y: 0}
walkNSEW
walkNSEW(shape: AdvancedShape, index: number): Position
Converts a grid index into a grid position, going from north to south and east to west (NSEW).
- shape: AdvancedShapeThe grid shape to iterate. Note that since a grid implements the Shape interface, you can just provide a grid instance as well.
- index: numberThe index to be converted.
const grid = createGridFromArray2D([
[ 8, 4, 0],
[ 9, 5, 1],
[10, 6, 2],
[11, 7, 3],
]);
walkNSEW(grid, 0); // => {x: 2, y: 0}
walkNSEW(grid, 1); // => {x: 2, y: 1}
walkNSEW(grid, 2); // => {x: 2, y: 2}
walkNSEW(grid, 3); // => {x: 2, y: 3}
walkNSEW(grid, 4); // => {x: 1, y: 0}
walkNSEW(grid, 5); // => {x: 1, y: 1}
walkNSEW(grid, 6); // => {x: 1, y: 2}
walkNSEW(grid, 7); // => {x: 1, y: 3}
walkNSEW(grid, 8); // => {x: 0, y: 0}
walkNSEW(grid, 9); // => {x: 0, y: 1}
walkNSEW(grid, 10); // => {x: 0, y: 2}
walkNSEW(grid, 11); // => {x: 0, y: 3}
walkNSWE
walkNSWE(shape: AdvancedShape, index: number): Position
Converts a grid index into a grid position, going from north to south and west to east (NSWE).
- shape: AdvancedShapeThe grid shape to iterate. Note that since a grid implements the Shape interface, you can just provide a grid instance as well.
- index: numberThe index to be converted.
const grid = createGridFromArray2D([
[0, 4, 8],
[1, 5, 9],
[2, 6, 10],
[3, 7, 11],
]);
walkNSWE(grid, 0)); // => {x: 0, y: 0}
walkNSWE(grid, 1)); // => {x: 0, y: 1}
walkNSWE(grid, 2)); // => {x: 0, y: 2}
walkNSWE(grid, 3)); // => {x: 0, y: 3}
walkNSWE(grid, 4)); // => {x: 1, y: 0}
walkNSWE(grid, 5)); // => {x: 1, y: 1}
walkNSWE(grid, 6)); // => {x: 1, y: 2}
walkNSWE(grid, 7)); // => {x: 1, y: 3}
walkNSWE(grid, 8)); // => {x: 2, y: 0}
walkNSWE(grid, 9)); // => {x: 2, y: 1}
walkNSWE(grid, 10)); // => {x: 2, y: 2}
walkNSWE(grid, 11)); // => {x: 2, y: 3}
walkSNEW
walkSNEW(shape: AdvancedShape, index: number): Position
Converts a grid index into a grid position, going from south to north and east to west (SNEW).
- shape: AdvancedShapeThe grid shape to iterate. Note that since a grid implements the Shape interface, you can just provide a grid instance as well.
- index: numberThe index to be converted.
const grid = createGridFromArray2D([
[11, 7, 3],
[10, 6, 2],
[ 9, 5, 1],
[ 8, 4, 0],
]);
walkSNEW(grid, 0); // => {x: 2, y: 3}
walkSNEW(grid, 1); // => {x: 2, y: 2}
walkSNEW(grid, 2); // => {x: 2, y: 1}
walkSNEW(grid, 3); // => {x: 2, y: 0}
walkSNEW(grid, 4); // => {x: 1, y: 3}
walkSNEW(grid, 5); // => {x: 1, y: 2}
walkSNEW(grid, 6); // => {x: 1, y: 1}
walkSNEW(grid, 7); // => {x: 1, y: 0}
walkSNEW(grid, 8); // => {x: 0, y: 3}
walkSNEW(grid, 9); // => {x: 0, y: 2}
walkSNEW(grid, 10); // => {x: 0, y: 1}
walkSNEW(grid, 11); // => {x: 0, y: 0}
walkSNWE
walkSNWE(shape: AdvancedShape, index: number): Position
Converts a grid index into a grid position, going from south to north and west to east (SNWE).
- shape: AdvancedShapeThe grid shape to iterate. Note that since a grid implements the Shape interface, you can just provide a grid instance as well.
- index: numberThe index to be converted.
const grid = createGridFromArray2D([
[3, 7, 11],
[2, 6, 10],
[1, 5, 9],
[0, 4, 8],
]);
walkSNWE(grid, 0); // => {x: 0, y: 3}
walkSNWE(grid, 1); // => {x: 0, y: 2}
walkSNWE(grid, 2); // => {x: 0, y: 1}
walkSNWE(grid, 3); // => {x: 0, y: 0}
walkSNWE(grid, 4); // => {x: 1, y: 3}
walkSNWE(grid, 5); // => {x: 1, y: 2}
walkSNWE(grid, 6); // => {x: 1, y: 1}
walkSNWE(grid, 7); // => {x: 1, y: 0}
walkSNWE(grid, 8); // => {x: 2, y: 3}
walkSNWE(grid, 9); // => {x: 2, y: 2}
walkSNWE(grid, 10); // => {x: 2, y: 1}
walkSNWE(grid, 11); // => {x: 2, y: 0}
walkWENS
walkWENS(shape: AdvancedShape, index: number): Position
Converts an index to a grid position, going from west to east and north to south (WENS).
- shape: AdvancedShapeThe grid shape to iterate. Note that since a grid implements the Shape interface, you can just provide a grid instance as well.
- index: numberThe index to be converted.
walkWESN
walkWESN(shape: AdvancedShape, index: number): Position
Converts a grid index into a grid position, going from west to east and south to north (WESN).
- shape: AdvancedShapeThe grid shape to iterate. Note that since a grid implements the Shape interface, you can just provide a grid instance as well.
- index: numberThe index to be converted.
const grid = createGridFromArray2D([
[9, 10, 11],
[6, 7, 8],
[3, 4, 5],
[0, 1, 2],
]);
walkWESN(grid, 0)); // => {x: 0, y: 3}
walkWESN(grid, 1)); // => {x: 1, y: 3}
walkWESN(grid, 2)); // => {x: 2, y: 3}
walkWESN(grid, 3)); // => {x: 0, y: 2}
walkWESN(grid, 4)); // => {x: 1, y: 2}
walkWESN(grid, 5)); // => {x: 2, y: 2}
walkWESN(grid, 6)); // => {x: 0, y: 1}
walkWESN(grid, 7)); // => {x: 1, y: 1}
walkWESN(grid, 8)); // => {x: 2, y: 1}
walkWESN(grid, 9)); // => {x: 0, y: 0}
walkWESN(grid, 10)); // => {x: 1, y: 0}
walkWESN(grid, 11)); // => {x: 2, y: 0}
reducers module
everyCell
everyCell<T>(grid: Grid<T>, predicate: EveryCellPredicate<T>): boolean
True if `predicate` returns true for all cells in the grid.
- grid: Grid<T>The grid to be checked.
- predicate: EveryCellPredicate<T>The callback function to be matched.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
]);
const lessThan10 = (cell: number) => cell < 10;
everyCell(grid, lessThan10); // => true
const equal5 = (cell: number) => cell === 5;
everyCell(grid, equal5); // => false
isEmptyGrid
isEmptyGrid<T>(grid: Grid<T>): boolean
Returns true if the grid includes no values.
- grid: Grid<T>The grid to be checked.
isEmptyGrid(
createGrid({
columnCount: 0,
rowCount: 0,
createCell: () => 7,
})
); // => true
noCells
noCells<T>(grid: Grid<T>, predicate: NoCellsPredicate<T>): boolean
True if `predicate` returns false for all cells in the grid.
- grid: Grid<T>The grid to be checked.
- predicate: NoCellsPredicate<T>The callback function to be matched.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
]);
const equal666 = (cell: number) => cell === 666;
noCells(grid, equal666); // => true
const equal5 = (cell: number) => cell === 5;
noCells(grid, equal5); // => false
reduceGrid
reduceGrid<T, U>(grid: Grid<T>, callback: ReduceCallback<T, U>, initialValue: U, walk: GridWalker): U
Creates a reducer function that executes the provided callback function on each cell of the grid, resulting in a single output value.
- grid: Grid<T>The grid to be reduced.
- callback: ReduceCallback<T, U>The callback function that is executed on each cell in the grid.
- initialValue: UThe value to use as the first argument to the first call of the callback.
- walk: GridWalkerThis function calculates the grid position based on the iteration step and therefore defines the order in which the cells are iterated. If no walker is defined walkDefault is used.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
]);
const initialValue = 10;
const sum = reduceGrid(grid, (acc, cellValue: number) => acc + cellValue, initialValue);
// => 31 (10 + 1 + 2 + 3 + 4 + 5 + 6)
someCells
someCells<T>(grid: Grid<T>, predicate: SomeCellsPredicate<T>): boolean
True if `predicate` returns true for at least one cells in the grid.
- grid: Grid<T>The grid to be checked.
- predicate: SomeCellsPredicate<T>The callback function to be matched.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
]);
const lessThan10 = (cell: number) => cell < 10;
someCells(grid, lessThan10); // => true
const equal5 = (cell: number) => cell === 5;
someCells(grid, equal5); // => false
search module
findCell
findCell<T>(grid: Readonly<GridObject<T>>, callback: FindCallback<T>, walk: GridWalker): undefined | T
Receives a grid and returns the position of the first element that satisfies the provided testing function or equals the provided value. If no cell satisfies the testing function, `undefined` is returned.
- grid: Readonly<GridObject<T>>The grid in which to search.
- callback: FindCallback<T>Callback function that is called on each cell. Should return `true` if the element is found or `false` if not.
- walk: GridWalkerThis function calculates the grid position based on the iteration step and therefore defines the order in which the cells are iterated. If no walker is defined walkDefault is used.
const grid = createGridFromArray2D([
[1, 1, 1, 4],
[5, 6, 2, 8],
[0, 2, 3, 4],
]);
findCell(grid, (v) => v > 2); // => 4
findMax
findMax<T>(grid: Readonly<GridObject<T>>): undefined | T
Finds the maximum value in the grid by comparing values with the greater than operator.
- grid: Readonly<GridObject<T>>The grid in which to search.
const grid = createGridFromArray2D([
[1, 1, 1, 4],
[5, 6, 2, 8],
[0, 2, 3, 4],
]);
findMax(grid); // => 8
findMin
findMin<T>(grid: Readonly<GridObject<T>>): undefined | T
Finds the minimum value in the grid by comparing values with the less than operator.
- grid: Readonly<GridObject<T>>The grid in which to search.
const grid = createGridFromArray2D([
[2, 9, 2, 4],
[5, 6, 2, 8],
[1, 2, 3, 4],
]);
findMin(grid); // => 1
findPosition
findPosition<T>(grid: Readonly<GridObject<T>>, valueOrCallback: T | FindCallback, walk: GridWalker): undefined | Position
Receives a grid and returns the value of the first element that satisfies the provided testing function. If no values satisfies the testing function, `undefined` is returned.
- grid: Readonly<GridObject<T>>The grid in which to search.
- valueOrCallback: T | FindCallbackCell value to find or callback function that is called on each cell. Should return true if the element is found or false if not.
- walk: GridWalkerThis function calculates the grid position based on the iteration index and therefore defines the order in which the cells are iterated. If no walker is defined walkDefault is used.
const grid = createGridFromArray2D([
[1, 1, 1, 4],
[5, 6, "test", 8],
[1, "test", 3, 4],
]);
findPosition(grid, (v) => v > 2); // => {x: 3, y: 0}
findPosition(grid, (v) => typeof v === "string"); // => {x: 2, y: 1}
includes
includes<T>(grid: Readonly<GridObject<T>>, cell: T, walk: GridWalker): boolean
Returns true if the grid includes the given cell and false otherwise.
- grid: Readonly<GridObject<T>>The grid in which to search.
- cell: TThe value of the cell to search for.
- walk: GridWalkerThis function calculates the grid position based on the iteration step and therefore defines the order in which the cells are iterated. If no walker is defined walkDefault is used.
const grid = createGridFromArray2D([
[1, 1, 1, 4],
[5, 6, 2, 8],
[0, 2, 3, 4],
]);
includes(grid, 2); // => true
includes(grid, 10); // => false
includesWhere
includesWhere<T>(grid: Readonly<GridObject<T>>, callback: FindCallback<T>, walk: GridWalker): boolean
Returns true if at least one cell in the grid satisfies the given callback function.
- grid: Readonly<GridObject<T>>The grid in which to search.
- callback: FindCallback<T>The callback function used to search for a cell. It is called on each cell and has to return true if the cell was found or false if not.
- walk: GridWalkerThis function calculates the grid position based on the iteration step and therefore defines the order in which the cells are iterated. If no walker is defined walkDefault is used.
const grid = createGridFromArray2D([
[{v: 1}, {v: 1}, {v: 1}, {v: 4}],
[{v: 5}, {v: 6}, {v: 2}, {v: 8}],
[{v: 0}, {v: 2}, {v: 3}, {v: 4}],
]);
includesWhere(grid, (cell) => cell.v === 6); // => true
includesWhere(grid, (cell) => cell.v === 10); // => false
sideEffects module
forEachCell
forEachCell<T>(grid: Grid<T>, callback: ForEachCellCallback<T>, walk: GridWalker): void
Calls the given callback function on each cell in the grid.
- grid: Grid<T>The grid in which to search.
- callback: ForEachCellCallback<T>The callback function that is called on each cell.
- walk: GridWalkerThis function calculates the grid position based on the iteration step and therefore defines the order in which the cells are iterated.
import {createGridFromArray2D} from "gridl/core";
import {forEachCell} from "gridl/sideEffects";
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
]);
let str = "";
forEachCell(grid, (cellValue) => {
str = `${str}${cellValue}`;
});
// => str === "123456"
transformers module
addColumn
addColumn<T>(x: number, column: T[]): GridTransformer<T>
Creates a transformer that adds the given column at the given x-position.
- x: numberThe x-position of where to add the column.
- column: T[]The column to add.
const x = 1;
const y = 2;
const grid = createGridFromArray2D({
x,
y,
array2D: [
[1, 2, 3],
[4, 5, 6],
],
});
const newCol = [8, 9];
const newGrid = addColumn(1, newCol)(grid);
// => {
// x: 1,
// y: 2,
// cellCount: 8,
// rowCount: 2,
// columnCount: 4,
// array2D: [
// [1, 8, 2, 3],
// [4, 9, 5, 6],
// ],
// }
addColumnLeft
addColumnLeft<T>(column: T[]): GridTransformer<T>
Creates a transformer that adds the given column at the left side of a grid.
- column: T[]The column to add.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
]);
const newCol = [8, 9];
const newGrid = addColumnLeft(newCol)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 8,
// columnCount: 4,
// rowCount: 2,
// array2D: [
// [8, 1, 2, 3],
// [9, 4, 5, 6],
// ],
// }
addColumnRight
addColumnRight<T>(column: T[]): GridTransformer<T>
Creates a transformer that adds the given column at the right side of a grid.
- column: T[]The column to add.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
]);
const newColumn = [8, 9];
const newGrid = addColumnRight(newColumn)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 8,
// columnCount: 4,
// rowCount: 2,
// array2D: [
// [1, 2, 3, 8],
// [4, 5, 6, 9],
// ],
// }
addColumns
addColumns<T>(x: number, columns: T[][]): GridTransformer<T>
Creates a transformer that adds the given columns at the given x-position.
- x: numberThe x-position of where to add the columns.
- columns: T[][]The columns to add.
const grid = createGridFromArray2D([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]);
const newColumns = [
[ 7, 8, 9],
[10, 11, 12],
];
const newGrid = addColumns(1, newColumns)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 15,
// columnCount: 5,
// rowCount: 3,
// array2D: [
// [0, 7, 10, 0, 0],
// [0, 8, 11, 0, 0],
// [0, 9, 12, 0, 0],
// ],
// }
addRow
addRow<T>(y: number, row: T[]): GridTransformer<T>
Creates a transformer that adds the given row at the given y-position.
- y: numberThe y-position of where to add the row.
- row: T[]The row to add.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
]);
const newRow = [7, 8, 9];
const newGrid = addRow(1, newRow)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 9,
// columnCount: 3,
// rowCount: 3,
// array2D: [
// [1, 2, 3],
// [7, 8, 9],
// [4, 5, 6],
// ],
// }
addRowBottom
addRowBottom<T>(row: T[]): GridTransformer<T>
Creates a transformer that adds the given row to the bottom of the grid.
- row: T[]The row to add.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
]);
const newRow = [7, 8, 9];
const newGrid = addRowBottom(newRow)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 9,
// columnCount: 3,
// rowCount: 3,
// array2D: [
// [1, 2, 3],
// [4, 5, 6],
// [7, 8, 9],
// ],
// }
addRowTop
addRowTop<T>(row: T[]): GridTransformer<T>
Creates a transformer that adds the given row to the top of the grid.
- row: T[]The row to add.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
]);
const newRow = [7, 8, 9];
const newGrid = addRowTop(newRow)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 9,
// columnCount: 3,
// rowCount: 3,
// array2D: [
// [7, 8, 9],
// [1, 2, 3],
// [4, 5, 6],
// ],
// }
addRows
addRows<T>(y: number, rows: T[][]): GridTransformer<T>
Creates a transformer that adds the given rows at the given y-position.
- y: numberThe y-position of where to add the rows.
- rows: T[][]The rows to add.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
]);
const newRows = [
[ 7, 8, 9],
[10, 11, 12],
];
const newGrid = addRows(1, newRows)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 12,
// columnCount: 3,
// rowCount: 4,
// array2D: [
// [ 1, 2, 3],
// [ 7, 8, 9],
// [10, 11, 12],
// [ 4, 5, 6],
// ],
// }
crop
crop<T>(area: Area): GridTransformer<T>
Creates a transformer that crops a grid to the given area. Positions that are outside of the grid are ignored.
- area: AreaThe area which defines the subgrid to be extracted.
const grid = createGridFromArray2D([
[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23],
[24, 25, 26, 27],
]);
const area = {x: 1, y: 2, columnCount: 2, rowCount: 3};
const croppedGrid = crop(area)(grid);
// => {
// x: 1,
// y: 2,
// cellCount: 6,
// columnCount: 2,
// rowCount: 3,
// array2D: [
// [ 9, 10],
// [13, 14],
// [17, 18],
// ],
// }
crop<T>(x1: number, y1: number, x2: number, y2: number): GridTransformer<T>
Creates a transformer that crops a grid to the given coordinates. Positions that are outside of the grid are ignored.
- x1: numberThe first x-coordinate of the area be extracted.
- y1: numberThe first y-coordinate of the area be extracted.
- x2: numberThe second x-coordinate of the area be extracted.
- y2: numberThe second y-coordinate of the area be extracted.
const grid = createGridFromArray2D([
[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23],
[24, 25, 26, 27],
]);
const croppedGrid = crop(1, 2, 2, 4)(grid);
// => {
// x: 1,
// y: 2,
// cellCount: 6,
// columnCount: 2,
// rowCount: 3,
// array2D: [
// [ 9, 10],
// [13, 14],
// [17, 18],
// ],
// }
cropIntersection
cropIntersection<T>(area: Area): GridTransformer<T>
Cuts out the intersecting grid of two grids.
- area: AreaThe other grid to intersect with.
const grid = createGridFromArray2D([
[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
]);
const area = {columnCount: 3, rowCount: 3, x: 2, y: 1};
const newGrid = cropIntersection(area)(grid);
// => {
// cellCount: 9,
// columnCount: 3,
// rowCount: 3,
// x: 2,
// y: 1,
// array2D: [
// [ 7, 8, 9],
// [12, 13, 14],
// [17, 18, 19],
// ],
// }
fill
fill<T, U>(value: U): GridTransformer<T, U>
Creates a transformer that fills all cells of the grid with the given value.
- value: UThe value to fill the grid with.
const grid = createGrid({columnCount: 4, rowCount: 5, createCell: () => 0});
const newGrid = fill(6)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 20,
// columnCount: 4,
// rowCount: 5,
// array2D: [
// [6, 6, 6, 6],
// [6, 6, 6, 6],
// [6, 6, 6, 6],
// [6, 6, 6, 6],
// [6, 6, 6, 6],
// ],
// }
map
map<T, U>(callback: MapCallback<T, U>, walk: GridWalker): GridTransformer<T, U>
Creates a transformer that iterates over each cell of the grid and replaces the cell with the value returned by the callback function.
- callback: MapCallback<T, U>The callback function that is called for each cell.
- walk: GridWalkerThis function calculates the grid position based on the iteration step and therefore defines the order in which the cells are iterated. If no walker is defined walkDefault is used.
const grid = createGridFromArray2D([
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
]);
const increaseByOne = (value) => value + 1;
const increaseAllByOne = map(increaseByOne);
const mappedGrid = increaseAllByOne(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 12,
// columnCount: 4,
// rowCount: 3,
// array2D: [
// [1, 2, 3, 4],
// [5, 6, 7, 8],
// [9, 10, 11, 12],
// ],
// }
mirrorHorizontally
mirrorHorizontally<T>(): GridTransformer<T>
Creates a transformer that mirrors the grid horizontally.
const grid = createGridFromArray2D([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 10, 11],
]);
const mirroredGrid = mirrorHorizontally()(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 12,
// columnCount: 3,
// rowCount: 4,
// array2D: [
// [9, 10, 11],
// [6, 7, 8],
// [3, 4, 5],
// [0, 1, 2],
// ],
// }
mirrorVertically
mirrorVertically<T>(): GridTransformer<T>
Creates a transformer that mirrors a grid vertically.
const grid = createGridFromArray2D([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 10, 11],
]);
const mirroredGrid = mirrorVertically()(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 12,
// columnCount: 3,
// rowCount: 4,
// array2D: [
// [ 2, 1, 0],
// [ 5, 4, 3],
// [ 8, 7, 6],
// [11, 10, 9],
// ],
// }
moveColumn
moveColumn<T>(fromX: number, toX: number): GridTransformer<T>
Creates a transformer that moves a column from one x-position to another.
- fromX: numberDefines the x-position of the column that should be moved.
- toX: numberDefines the x-position of where the column is to be moved.
const grid = createGridFromArray2D([
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
]);
const fromX = 2;
const toX = 1;
const newGrid = moveColumn(fromX, toX)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 12,
// columnCount: 4,
// rowCount: 3,
// array2D: [
// [0, 2, 1, 3],
// [4, 6, 5, 7],
// [8, 10, 9, 11],
// ],
// }
moveGrid
moveGrid<T>(position: Position): GridTransformer<T>
Creates a transformer that moves the grid to the given position.
- position: PositionThe position of where to move the grid.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
moveGrid({x: -2, y: -4})(grid); // => {...grid, x: -2, y: -4});
moveRow
moveRow<T>(fromY: number, toY: number): GridTransformer<T>
Creates a transformer that moves a row from one y-position to another.
- fromY: numberDefines the y-position of the row that should be moved.
- toY: numberDefines the y-position of where the row is to be moved.
createGridFromArray2D([
[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12],
]);
const newGrid = moveRow(0, 3)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 12,
// columnCount: 3,
// rowCount: 4,
// array2D: [
// [ 4, 5, 6],
// [ 7, 8, 9],
// [10, 11, 12],
// [ 1, 2, 3],
// ],
// }
removeColumn
removeColumn<T>(x: number): GridTransformer<T>
Creates a transformer removes the column at the given x-position.
- x: numberThe x-position of the column to be removed. Invalid positions are ignored.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
const newGrid = removeColumn(1)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 6,
// columnCount: 2,
// rowCount: 3,
// array2D: [
// [1, 3],
// [4, 6],
// [7, 9],
// ],
// }
removeColumnLeft
removeColumnLeft<T>(): GridTransformer<T>
Creates a transformer that removes the most left column.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
const newGrid = removeColumnLeft()(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 6,
// columnCount: 2,
// rowCount: 3,
// array2D: [
// [2, 3],
// [5, 6],
// [8, 9],
// ],
// }
removeColumnRight
removeColumnRight<T>(): GridTransformer<T>
Creates a transformer that removes the most right column.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
const newGrid = removeColumnRight()(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 6,
// columnCount: 2,
// rowCount: 3,
// array2D: [
// [1, 2],
// [4, 5],
// [7, 8],
// ],
// }
removeRow
removeRow<T>(y: number): GridTransformer<T, T>
Creates a transformer removes the row at the given y-position.
- y: numberThe y-position of the column to be removed. Invalid positions are ignored.
const grid = createGrid({
createCell: (_pos, idx) => idx + 1
columnCount: 3,
rowCount: 3,
x; 1,
y: 2,
});
const newGrid = removeRow(1)(grid);
// => {
// x: 1,
// y: 2,
// cellCount: 6,
// columnCount: 3,
// rowCount: 2,
// array2D: [
// [1, 2, 3],
// [7, 8, 9],
// ],
// });
removeRowBottom
removeRowBottom<T>(): GridTransformer<T>
Creates a transformer that removes the bottom row.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
const newGrid = removeRowBottom()(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 6,
// columnCount: 3,
// rowCount: 2,
// array2D: [
// [1, 2, 3],
// [4, 5, 6],
// ],
// }
removeRowTop
removeRowTop<T>(): GridTransformer<T>
Creates a transformer that removes the top row.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
const newGrid = removeRowTop()(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 6,
// columnCount: 3,
// rowCount: 2,
// array2D: [
// [4, 5, 6],
// [7, 8, 9],
// ],
// }
rotate90
rotate90<T>(times: number): GridTransformer<T>
Rotates a grid by 90 degrees `times` times.
- times: numberThe number of times to rotate the array by 90 degrees. Positives integers rotate clockwise, whereas negative rotate counterclockwise.
const grid = createGridFromArray2D([
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
]);
// 1 step can be thought of as 90 degrees, 2 steps 180 degrees and so on
const steps = 1;
const rotatedGrid = rotate90(steps)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 12,
// columnCount: 3,
// rowCount: 4,
// array2D: [
// [ 8, 4, 0],
// [ 9, 5, 1],
// [10, 6, 2],
// [11, 7, 3],
// ],
// }
setCell
setCell<T>(position: Position, value: T): GridTransformer<T, T>
Creates a transformer that sets the cell value at the given x- and y-coordinates.
- position: PositionThe position of the cell to update.
- value: TThe value to set.
const grid = createGridFromArray2D([
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
]);
const newGrid = setCell({x: 2, y: 1}, "moin")(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 12,
// columnCount: 4,
// rowCount: 3,
// array2D: [
// [0, 1, 2, 3],
// [4, 5, "moin", 7],
// [8, 9, 10, 11],
// ],
// }
setColumn
setColumn<T>(x: number, column: T[], yOffset: number): GridTransformer<T>
Creates a transformer that replaces the column at the given x-positon with the given column.
- x: numberThe x-position of the column to be replaced.
- column: T[]The new column to replace the old one.
- yOffset: numberAn optional offset to shift the new column on the y-axis.
const grid = createGridFromArray2D([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]);
const newColumn = [6, 6, 6];
const newGrid = setColumn(0, newColumn)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 9,
// columnCount: 3,
// rowCount: 3,
// array2D: [
// [6, 0, 0],
// [6, 0, 0],
// [6, 0, 0],
// ],
// }
setRow
setRow<T>(y: number, newRow: T[], xOffset: number): GridTransformer<T>
Replaces the row at the given y-positon with the provided row.
- y: numberThe y-position of the row to be replaced.
- newRow: T[]The new row to replace to old one.
- xOffset: numberAn optional offset to shift the new row on the x-axis.
const grid = createGridFromArray2D([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]);
const newRow = [6, 6, 6];
const newGrid = setRow(0, newRow)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 9,
// columnCount: 3,
// rowCount: 3,
// array2D: [
// [6, 6, 6],
// [0, 0, 0],
// [0, 0, 0],
// ],
// }
setSubGrid
setSubGrid<T>(subGrid: Grid<T>): GridTransformer<T>
Creates a transformer that applies the values of a subgrid on the main grid.
- subGrid: Grid<T>The subgrid that contains the values to be applied to the main grid.
// create base grid
const grid = createGridFromArray2D([
[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29],
]);
// create subgrid at position {x: 1, y: 2}
const subGrid = createGridFromArray2D({
x: 1,
y: 2,
array2D: [
[6, 6, 6],
[6, 6, 6],
[6, 6, 6],
],
});
// apply subgrid
const newGrid = setSubGrid(subGrid)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 30,
// columnCount: 6,
// rowCount: 5,
// array2D: [
// [ 0, 1, 2, 3, 4, 5],
// [ 6, 7, 8, 9, 10, 11],
// [12, 6, 6, 6, 16, 17],
// [18, 6, 6, 6, 22, 23],
// [24, 6, 6, 6, 28, 29],
// ],
// }
shiftColumn
shiftColumn<T>(props: ShiftColumnProps): GridTransformer<T>
Shifts a column by the given amount of steps in a positive (downwards) or negative (upwards) direction.
- props: ShiftColumnPropsThe x-value that indicates the column to shift and the steps which indicate the number of steps to shift.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
const newGrid = shiftColumn({x: 0, steps: 1})(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 9,
// columnCount: 3,
// rowCount: 3,
// array2D: [
// [7, 2, 3],
// [1, 5, 6],
// [4, 8, 9],
// ],
// }
shiftRow
shiftRow<T>(props: ShiftRowProps): GridTransformer<T>
Shifts a row by the given amount of steps in a positive (right) or negative (left) direction.
- props: ShiftRowPropsThe y-value that indicates the row to shift and the steps which indicate the number of steps to shift.
const grid = createGridFromArray2D([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
const newGrid = shiftRow({y: 0, steps: 1})(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 9,
// columnCount: 3,
// rowCount: 3,
// array2D: [
// [3, 1, 2],
// [4, 5, 6],
// [7, 8, 9],
// ],
// }
swapCellGroups
swapCellGroups<T>(shape: Shape, positionA: Position, positionB: Position): GridTransformer<T | undefined>
Creates a transformer that swaps two cell groups. If cells of a group are outside the grid, the swapped cell value will be undefined. Watch out when swapping overlapping cell groups: Overlapping cells are duplicated and replace other cells.
- shape: ShapeThe shape of the groups to swap.
- positionA: PositionThe position of the top left corner of the first group to be swapped.
- positionB: PositionThe position of the top left corner of the second group to be swapped.
const grid = createGridFromArray2D([
[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
]);
const shape = {columnCount: 2, rowCount: 2};
const positionA = {x: 0, y: 0};
const positionB = {x: 2, y: 2};
const newGrid = swapCellGroups(shape, positionA, positionB)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 9,
// columnCount: 3,
// rowCount: 3,
// array2D: [
// [ 6, 7, 2, 3],
// [10, 11, 0, 1],
// [ 8, 9, 4, 5],
// ],
// }
swapCells
swapCells<T>(position1: Position, position2: Position): GridTransformer<T>
Creates a transformer that swaps the values of two cells.
- position1: PositionPosition of the first cell.
- position2: PositionPosition of the second cell.
const grid = createGridFromArray2D([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
]);
const pos1 = {x: 1, y: 0};
const pos2 = {x: 2, y: 2};
const newGrid = swapCells(pos1, pos2)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 9,
// columnCount: 3,
// rowCount: 3,
// array2D: [
// [0, 8, 2],
// [3, 4, 5],
// [6, 7, 1],
// ],
// }
swapColumns
swapColumns<T>(x1: number, x2: number): GridTransformer<T>
Creates a transformer that swaps two columns at the given x-positions. If one or both x-positions are outside of the grid, the original grid is returned without any transformations.
- x1: numberThe x-position of the first column.
- x2: numberThe x-position of the second column.
const grid = createGridFromArray2D([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 10, 11],
]);
const newGrid = swapColumns(1, 2)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 12,
// columnCount: 3,
// rowCount: 4,
// array2D: [
// [0, 2, 1],
// [3, 5, 4],
// [6, 8, 7],
// [9, 11, 10],
// ],
// }
swapRows
swapRows<T>(y1: number, y2: number): GridTransformer<T>
Creates a transformer that swaps two rows at the given y-positions. If one or both y-positions are outside of the grid, the original grid is returned without any transformations.
- y1: numberThe y-position of the first row.
- y2: numberThe y-position of the second row.
const grid = createGridFromArray2D([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
]);
const newGrid = swapRows(0, 1)(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 9,
// columnCount: 3,
// rowCount: 3,
// array2D: [
// [3, 4, 5],
// [0, 1, 2],
// [6, 7, 8],
// ],
// }
transform
transform<T>(transformers: GridTransformer<T, T>[]): GridTransformer<T, T>
Creates a transformer that combines a series of other transformers and applies them to the grid one by one.
- transformers: GridTransformer<T, T>[]The list of transformers to apply.
const oldGrid = createGridFromArray2D([
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
]);
const newGrid = transform(
map((_cell, pos) => pos.x < 2 ? 1 : 2),
setCell({x: 2, y: 1}, 666),
rotate90(1),
removeRow(1),
)(oldGrid);
// => {
// x: 0,
// y: 0,
// cellCount: 9,
// columnCount: 3,
// rowCount: 3,
// array2D: [
// [1, 1, 1],
// [2, 666, 2],
// [2, 2, 2],
// ],
// }
transformArea
transformArea<T>(area: Area, transformerList: GridTransformer<T, any>[]): GridTransformer<T>
Creates a transformer that combines a series of other transformers and applies them to an area within the grid one by one.
- area: AreaThe area to be transformed.
- transformerList: GridTransformer<T, any>[]The list of transformer function to be applied.
const grid = createGridFromArray2D([
[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29],
]);
const area = {x: 2, y: 1, columnCount: 2, rowCount: 3};
const newGrid = transformArea(area, [
map((_cell, pos) => pos.x < 1 ? 1 : 2),
setCell({x: 1, y: 1}, 666),
rotate90(1),
removeColumnRight(),
])(grid);
// => {
// x: 0,
// y: 0,
// cellCount: 30,
// columnCount: 6,
// rowCount: 5,
// array2D: [
// [ 0, 1, 2, 3, 4, 5],
// [ 6, 7, 1, 1, 10, 11],
// [12, 13, 2, 666, 16, 17],
// [18, 19, 20, 21, 22, 23],
// [24, 25, 26, 27, 28, 29],
// ],
// }