Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "array-2d"

Index

Constructor Functions

create

  • create<T>(width: number, height: number, initial?: T): Array2D<T>
  • Creates an empty Array2D with fixed dimensions and an optional initial value for each element.

    // create an array with width = 3 and height = 6
    Array2D.create(3, 6)
    
    // create an array with width = 2, height = 2, filled with "a"
    Array2D.create(2, 2, "a")

    Type parameters

    • T

      The type of the elements in the array

    Parameters

    • width: number
    • height: number
    • Optional initial: T

    Returns Array2D<T>

    The new array

generate

  • generate<T>(width: number, height: number, generator: (point: Point) => T): Array2D<T>
  • Creates an empty Array2D with fixed dimensions and an optional initial value for each element.

    // create an array with width = 3 and height = 6
    Array2D.create(3, 6)
    
    // create an array with width = 2, height = 2, filled with "a"
    Array2D.create(2, 2, "a")

    Type parameters

    • T

      The type of the elements in the array

    Parameters

    • width: number
    • height: number
    • generator: (point: Point) => T
        • Parameters

          Returns T

    Returns Array2D<T>

    The new array

Conversion Functions

from2DArray

  • from2DArray<T>(array: T[][]): Array2D<T>
  • Creates an Array2D from a two dimensional array.

    Each element in array corresponds to a row in the returned array.

    The height will be the number or rows and the width will be the length of the longest row.

    let map = [
      [0, 1, 0],
      [1, 1, 1],
      [0, 0, 0],
    ];
    
    Array2D.from2DArray(map)

    If you want to create an Array2D from a one dimensional (flat) array, then use fromArray instead.

    Type parameters

    • T

      The type of the elements in array

    Parameters

    • array: T[][]

      The two-dimensional array

    Returns Array2D<T>

fromArray

  • fromArray<T>(width: number, height: number, array: Array<T>): Array2D<T>
  • Creates an Array2D from a one dimensional array in row-major order.

    let map = [
      0, 1, 0,
      1, 1, 1,
      0, 0, 0,
    ];
    
    Array2D.fromArray(map, 3, 3);

    Type parameters

    • T

      The type of the elements in the array

    Parameters

    • width: number

      The width of the array in columns

    • height: number

      The height of the array in rows

    • array: Array<T>

      The one dimensional array. The length of array should be equal to width * height

    Returns Array2D<T>

    The new array

to2DArray

  • to2DArray<T>(array2D: Array2D<T>): T[][]
  • Creates a two dimensional array from array2D.

    let map = Array2D.create(2, 2, 0);
    
    Array2D.to2DArray(map) // [[0, 0], [0, 0]]

    Type parameters

    • T

      The type of the elements in array2D

    Parameters

    Returns T[][]

    The two dimensional array

toArray

  • toArray<T>(array2D: Array2D<T>): Array<T>
  • Creates a one dimensional array in row-major order. from an Array2D.

    let map = Array2D.create(2, 2, 0);
    
    Array2D.toArray(map) // [0, 0, 0, 0]

    Type parameters

    • T

      The type of the elements in the array

    Parameters

    Returns Array<T>

    The new one dimensional array

Other Functions

clone

  • Creates a new Array2D by shallowly cloning an existing one.

    let a1 = Array2D.create(10, 10);
    
    // create a copy of an existing Array2D
    let a2 = Array2D.clone(a1);

    Type parameters

    • T

      The type of the elements in the array

    Parameters

    • array2D: Array2D<T>

      The array to clone

    Returns Array2D<T>

    The cloned array

equals

  • Performs a shallow comparison to determine whether a1 and a2 are equivalent.

    let a1 = Array2D.create(2, 2, 0);
    let a2 = Array2D.create(2, 2, 0);
    
    Array2D.equals(a1, a2) // true

    Array2D objects are considered to be equivalent if:

    • They have the same dimensions
    • They have the same elements in the same order

    Type parameters

    • T

      The type of the elements in a1 and a2

    Parameters

    Returns boolean

fill

  • fill<T>(array2D: Array2D<T>, value: T, startX?: number, startY?: number, endX?: number, endY?: number): void
  • Fills the elements of array2D with value.

    Can optionally fill a subrectangle within the Array2D by passing start and end coordinates.

    let a = Array2D.create(3, 3, "a");
    
    // fill with "b"
    Array2D.fill(a, "b");
    
    // fill the top left quarter with "c"
    Array2D.fill(a, "c", 0, 0, 2, 2);

    This function modifies array2D. Use filled to create a new Array2D instead.

    Type parameters

    • T

      The type of the elements in array2D

    Parameters

    • array2D: Array2D<T>

      The array to fill

    • value: T

      The value to fill array2D with

    • Default value startX: number = 0

      The x coordinate to start filling from

    • Default value startY: number = 0

      The y coordinate to start filling from

    • Default value endX: number = array2D.width

      The x coordinate to finish filling before

    • Default value endY: number = array2D.height

      The y coordinate to finish filling before

    Returns void

filled

  • filled<T>(array2D: Array2D<T>, value: T, startX?: number, startY?: number, endX?: number, endY?: number): Array2D<T>
  • Creates a new Array2D by filling a copy of array2D with value.

    Can optionally fill a subrectangle within the Array2D by passing the start and end coordinates.

    let a = Array2D.create(3, 3, "a");
    
    // fill with "b"
    let b = Array2D.fill(a, "b");
    
    // fill the top left quarter with "c"
    let c = Array2D.fill(a, "c", 0, 0, 2, 2);

    This function creates a new Array2D. Use fill to modify the existing one instead.

    Type parameters

    • T

      The element type of array2D

    Parameters

    • array2D: Array2D<T>

      The array to fill

    • value: T

      The value to fill array2D with

    • Default value startX: number = 0

      The x coordinate to start filling from

    • Default value startY: number = 0

      The y coordinate to start filling from

    • Default value endX: number = array2D.width

      The x coordinate to finish filling before

    • Default value endY: number = array2D.height

      The y coordinate to finish filling before

    Returns Array2D<T>

    The filled array2D

fromString

  • fromString(str: string): Array2D<any>
  • Parameters

    • str: string

    Returns Array2D<any>

get

  • get<T>(array2D: Array2D<T>, x: number, y: number): T | undefined
  • Gets the element at the given indices.

    let a = Array2D.create(2, 2, 0);
    
    // get the element at 0, 0
    Array2D.get(a, 0, 0)

    Type parameters

    • T

      The type of the elements in array2D

    Parameters

    • array2D: Array2D<T>
    • x: number

      The x index

    • y: number

      The y index

    Returns T | undefined

    The element (or undefined if the indices were out of bounds).

inBounds

  • inBounds(array2D: Array2D<any>, x: number, y: number): boolean
  • Parameters

    • array2D: Array2D<any>
    • x: number
    • y: number

    Returns boolean

is

  • is<T>(value: any, guard?: undefined | ((v: any) => v is T)): value is Array2D<T>
  • Determines whether value is an Array2D.

    Type parameters

    • T

    Parameters

    • value: any
    • Optional guard: undefined | ((v: any) => v is T)

      An on optional type guard that will check the type of the elements of in the array.

      // Check that grid is an Array2D<Point.Point>
      Array2D.is(grid, Point.is)
      
      function isNumber(value): value is number {
        return typeof value === "number";
      }
      
      // Check that samples is an Array2D<number>
      Array2D.is(samples, isNumber)

    Returns value is Array2D<T>

iter

  • Parameters

    Returns Generator<Point, void>

map

  • map<T, U>(array2D: Array2D<T>, callback: (item: T, x: number, y: number) => U): Array2D<U>
  • Maps over array2D with a function to create a new Array2D.

    This works like Array.prototype.map but rather than having a single index passed into the callback, both x and y coordinates are provided.

    let a = Array2D.from2DArray([
      [0, 1, 2],
      [3, 4, 5]
    ]);
    
    // Create a new Array2D by incrementing the value of each element
    let b = Array2D.map(a, n => n + 1);

    Type parameters

    • T

      The element type of array2D

    • U

      The element type of the returned Array2D

    Parameters

    • array2D: Array2D<T>
    • callback: (item: T, x: number, y: number) => U

      A function that takes a value of type T and returns a value of type U

        • (item: T, x: number, y: number): U
        • Parameters

          • item: T
          • x: number
          • y: number

          Returns U

    Returns Array2D<U>

points

  • Parameters

    Returns Point[]

rotateLeft90

  • Type parameters

    • T

    Parameters

    Returns Array2D<T>

rotateRight90

  • Type parameters

    • T

    Parameters

    Returns Array2D<T>

set

  • set<T>(array2D: Array2D<T>, x: number, y: number, value: T): void
  • Sets the element at the given indices.

    let a = Array2D.create(2, 2);
    
    // set the element at 0, 0 to "a"
    Array2D.set(a, 0, 0, "a")

    Type parameters

    • T

      The type of the elements in array2D

    Parameters

    • array2D: Array2D<T>
    • x: number

      The x index

    • y: number

      The y index

    • value: T

      The value to insert

    Returns void

slice

  • slice<T>(array2D: Array2D<T>, x: number, y: number, width: number, height: number): Array2D<unknown>
  • Creates an Array2D from rectangular slice of array2d.

    let a1 = Array2D.create(10, 10, "a");
    
    // Copy the top left slice into a new Array2D
    let a2 = Array2D.slice(a1, 0, 0, 5, 5);

    Type parameters

    • T

      The element type of array2D

    Parameters

    • array2D: Array2D<T>
    • x: number

      The top left x coordinate of the slice

    • y: number

      The top left y coordinate of the slice

    • width: number

      The width of the slice

    • height: number

      The height of the slice

    Returns Array2D<unknown>

    The sliced rectangle as an Array2D

toString

  • toString(array2D: Array2D<any>, separator?: string): string
  • Parameters

    • array2D: Array2D<any>
    • Default value separator: string = ""

    Returns string

transpose

  • Type parameters

    • T

    Parameters

    Returns Array2D<T>

Generated using TypeDoc