node package manager
Painless code sharing. npm Orgs help your team discover, share, and reuse code. Create a free org »

habitants-location

Location

Build Status

A helper module for dealing with locations. You can assign these to units, tiles, whatever.

Example

var Location = require('location');
 
var myLocation = new Location('2:3');
var hugeArrayOfLocations = [location, ...]
 
hugeArrayOfLocations.forEach(function (location) {
    if (myLocation.isEqualTo(location)) return 'Same Location!' + location;
    else return 'Not the same Location!' + location;
});

Methods

var Location = require('location');

var location = new Location(x, y)

Returns a Location object at (x, y).

Also accepts String input:

var loc = new Location('8:8')`

is equivalent to:

var loc = new Location(8,8)

location.toString()

Returns (x,y) coordinates as a string, separated by a colon.

var location = new Location(5, 4);
location.toString() -> '5:4'

location.isEqualTo(location)

Compare two location objects, and it will return a boolean.

Both x and y values must match to be true.

var location1 = new Location(5, 4);
var location2 = new Location(5, 4);
 
location1.isEqualTo(location2) -> true
 
var location3 = new Location(2, 8);
var location4 = new Location(2, 2);
 
location3.isEqualTo(location4) -> false

location.isNextTo(location)

Compare two location objects. If adjacent, returns true.

var location1 = new Location(5, 4);
var location2 = new Location(5, 3);
 
location1.isNextTo(location2) -> true

location.copy()

Returns a new instance of Location with the same values.

Installation

npm install habitants-location

Tests

npm test