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

@mitchallen/grid-square

v0.1.9

Published

2D square grid

Downloads

14

Readme

@mitchallen/grid-square

2D square grid


Installation

You must use npm 2.7.0 or higher because of the scoped package name.

$ npm init
$ npm install @mitchallen/grid-square --save

Usage

    "use strict";
    var gridFactory = require("@mitchallen/grid-square");
    
    var xSize = 5;
    var ySize = 10;
    var value = 100;
    var i = xSize - 1;
    var j = ySize - 1;
    
    var grid = gridFactory.create( { x: xSize, y: ySize } );
    
	if(!grid) {
    	console.error("couldn't create grid");
	}
    
    if(! grid.isCell( i, j ) ) {
    	console.error("parameters not within grid");
    }
    
    if(! grid.set( i, j, value )) {
    	console.error("couldn't set grid value");
    }
    
    let result = grid.get( i, j );
    
    if(! result) {
    	console.error("couldn't get grid value");
    } else {
    	console.log("grid value: ", result );
    }

Methods

create( spec )

Factory method that returns a square grid object.

It takes one spec parameter that must be an object with x and y values specifying the size of the grid.

The x and y values can not be less than one (1).

The method will set xSize and ySize to 0 if no parameters are set

You can call create multiple times to create multiple grids.

    var gridFactory = require("@mitchallen/grid-square");
    
    var grid1 = gridFactory.create( { x: 5, y: 10 } );
    var grid2 = gridFactory.create( { x: 7, y: 20 } );
    
  if(!grid1 || !grid2) ...

squareGrid.xSize

Returns the size of the x dimension.

  grid.xSize.should.eql(5);

squareGrid.ySize

Returns the size of the y dimension.

	grid.ySize.should.eql(10);

grid.isCell( x, y )

The x and y parameters should be zero-based coordinates ranging from zero (0) to axis size minus one.

The method is called internally by get.

    if(! grid.isCell( i, j ) ) {
    	console.error("parameters not within grid");
    }

grid.set( x, y, value )

The x and y values must be greater than zero. If the parameters fail validation then a value of false is returned. Otherwise true is returned.

The value parameter can be a number, a string or even an object.

    if(! grid.set( i, j, value )) {
    	console.error("couldn't set grid value");
    }

grid.get( x, y )

The x and y values are passed to the isCell method internally for validation. If the parameters fail validation then a null object is returned. Otherwise the value of the cell (grid location) is returned.

The returned value can be a number, a string or even an object.

    let result = grid.get( i, j );
    
    if(! result) {
    	console.error("couldn't get grid value");
    } else {
    	console.log("grid value: ", result );
    }

grid.fill(value)

Fills the grid with whatever is passed in as value. Value can be a number, a string or even an object. Any existing values in the grid will be replaced with the new fill value.

    let fillValue = "foo";
    
    var result = grid.fill(fillValue);

grid.cloneArray()

Returns a clone of the internal array. This is not a reference. So changes to the cloned array should not change the original.

	let tX = 0;
	let tY = 0;
	let gridValue = 100;
	let cloneValue = 500;
	
	// Set a value in the original grid
	grid.set(tX,tY,gridValue);

	// Clone the grid	
	let arr = grid.cloneArray();
	
	// Verify value exists in clone
	arr[tX][tY].should.eql(gridValue);
	
	// Change value in clone
	arr[tX][tY] = cloneValue;
	
	// Verify new value is set in clone
	arr[tX][tY].should.eql(cloneValue);
	
	// Ensure that value does not alter original grid
  grid.get(tX,tY).should.eql(gridValue);

grid.rows

Number of rows in the grid.

  var r = grid.rows;

grid.rowSize(rowIndex)

  var r = grid.rowSize(1);

Size of row.

grid.log()

Logs the size and contents of the internal array.

    grid.log();

Example output:

size: 4
[ [ 20, 10, 10, 10, 10 ],
  [ 10, 10, 10, 10, 10 ],
  [ 10, 10, 10, 10, 10 ],
  [ 10, 10, 10, 10, 30 ] ]
  

Browser Client Example

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Grid Square Example</title>
        <meta name="description" content="Grid Square Example">
        <script src="https://unpkg.com/@mitchallen/[email protected]/dist/grid-square.min.js"></script>
        <script>
          var factory = window.MitchAllen.GridSquare;
          console.log(factory);
          var xSize = 5,
              ySize = 6;
          var gs = factory.create( { x: xSize, y: ySize } );
          gs.set( xSize-1, ySize-1, "alpha" );
          console.log(gs);
          gs.log(); 
        </script>
      </head>
      <body>
        <h1>Grid Square Example</h1>
        <p>See JavaScript developer console for output.</p>
      </body>
    </html>

Testing

To test, go to the root folder and type (sans $):

$ npm test

Repo(s)


Contributing

In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.


Version History

Version 0.1.9

  • refactored
  • updated dependency

Version 0.1.8

  • updated dependency
  • updated .npmignore
  • updated client example

Version 0.1.7

  • fixed npm start command

Version 0.1.6

  • added test cases for 100% code coverage

Version 0.1.5

  • integrated travis-ci and codecov.io

Version 0.1.4

  • updated grid-core version

Version 0.1.3

  • updated grid-core version, examples and documentation

Version 0.1.2

  • resolving tag issue

Version 0.1.1

  • reset tag

Version 0.1.0

  • initial release