npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fixed-2d-array

v2.0.0

Published

A fixed size 2D array in javascript

Downloads

14

Readme

fixed-2d-array

NPM version Build Status devDependency Status Coverage Status Code Climate

A fixed size 2D array in JavaScript

This module gives you a two-dimensional array with a fixed size. The top left coordinate is (0,0).

Fixed2DArray(rows, columns, defaultValue)

rows is the number of rows the Fixed2DArray will start with, columns is the columns. During the creation of the array the defaultValue will be assigned to all elements.

validateCoords(row, col)

The validateCoords method checks if the given coordinates are valid. (lie inside of the array) If the coordinates are not valid an Error is thrown.

get(row, col)

Returns the value of the given coordinate. The coordinate is checked using validateCoords.

getRow(rowIndex)

Returns an array of the requested row.

getColumn(colIndex)

Returns an array of the requested column.

set(row, col, value)

Sets the value of the given coordinate to value. The coordinate is checked using validateCoords.

setRow(rowIndex, array)

Sets values of the given array as the values of the specified row.

pushRow([array1, array2, ..., arrayN])

Adds one or more arrays as rows to the bottom of the Fixed2DArray. Returns the new width of the Fixed2DArray.

Only arguments that are arrays will be appended as rows.

If the given array is smaller then the height of the Fixed2DArray, undefined will fill the given array until it is the same length as the current row.

setColumn(colIndex, array)

Sets values of the given array as the values of the specified column.

getHeight()

The number of rows corresponds with the height.

getWidth()

The number of columns corresponds to the width.

getNeighbours(row, col, [, distance])

Returns an array containing all values of the cells next to the given coordinate.

For example, distance not set:

[ ][ ][ ][ ][ ]
[ ][*][*][*][ ]
[ ][*][X][*][ ]
[ ][*][*][*][ ]
[ ][ ][ ][ ][ ]

The given coordinate is marked with an X. The function will return an array containing the values for the fields marked with an *.

Example, distance = 2:

[*][*][*][*][*]
[*][*][*][*][*]
[*][*][X][*][*]
[*][*][*][*][*]
[*][*][*][*][*]

The function will return an array containing the values for the fields marked with an '*'. Notice that distance will change what cells count as neighbors.

areNeighbours(row1, col1, row2, col2, [, distance])

Returns true if the given coordinates are neighbors, false otherwise.

The distance between each coordinate must be within distance or one unit away from each other for the given coordinates to be considered neighbors.

For example, distance not set:

   0  1  2 
0 [A][ ][ ]
1 [ ][B][ ]
2 [ ][ ][ ]
3 [ ][ ][ ]

The first given coordinate (0,0) is marked with an A, the second (1,1), a B. A and B are neighbors.

If A and B were instead placed at (0,0) and (2,2) respectively, like this:

   0  1  2 
0 [A][ ][ ]
1 [ ][ ][ ]
2 [ ][ ][B]
3 [ ][ ][ ]

A and B are no longer neighbors.

Example, distance = 2:

   0  1  2 
0 [A][ ][ ]
1 [ ][ ][ ]
2 [ ][ ][B]
3 [ ][ ][ ]

A (0,0) and B (2,2) are neighbors.

forEach(fn, [, thisArg])

Executes a provided function once per array element.

fn is the function to execute for each element, taking three arguments:

  • currentValue: The current element being processed in the array.
  • index: The object index, {x: x, y: y}, of the current element being processed in the 2d array.
  • array: The Fixed2DArray that forEach is being applied to.
  • thisArg: Optional. Value to use as this when executing callback.