All files / geonet-geohash/src neighbors.js

100% Statements 6/6
100% Branches 3/3
100% Functions 1/1
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44              1x                               2x                     2x 1x     1x       1x  
/** ****************************************************************************************************
 * File: neighbors.js
 * Project: geohash
 * @author Nick Soggin <iSkore@users.noreply.github.com> on 19-Feb-2019
 *******************************************************************************************************/
'use strict';
 
const neighbor = require( './neighbor' );
 
/**
 * neighbors
 * Returns all 8 neighbor cells to specified geohash.
 * This algorithm is approximately two times faster than ngeohash.
 * Times for 1,000,000 iterations
 * | ngeohash | Geohash  |
 * | 0m3.108s | 0m1.675s |
 *
 * @param   {string} geohash - Geohash neighbors are required of.
 * @param   {boolean} asObject - to return as an object or array
 * @returns {{c,n,ne,e,se,s,sw,w,nw}|array} - surrounding geohashes
 * @throws  Invalid geohash.
 */
function neighbors( geohash, asObject = false ) {
	const [ n, ne, e, se, s, sw, w, nw ] = [
		neighbor( geohash, 'n' ),
		neighbor( neighbor( geohash, 'n' ), 'e' ),
		neighbor( geohash, 'e' ),
		neighbor( neighbor( geohash, 's' ), 'e' ),
		neighbor( geohash, 's' ),
		neighbor( neighbor( geohash, 's' ), 'w' ),
		neighbor( geohash, 'w' ),
		neighbor( neighbor( geohash, 'n' ), 'w' )
	];
 
	if ( asObject ) {
		return { c: geohash, n, ne, e, se, s, sw, w, nw };
	}
	else {
		return [ n, ne, e, se, s, sw, w, nw ];
	}
}
 
module.exports = neighbors;