All files / geonet-geohash/src utils.js

100% Statements 46/46
100% Branches 53/53
100% Functions 9/9
100% Lines 46/46

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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197                            1x                   424x                         14x                       8x 1x     7x                       8x 1x     7x                         67x                             31x 1x     30x                       31x 1x     30x                           56x 1x     55x 21x 1x   20x 1x     19x 19x   19x 1x       18x 18x 18x   18x       53x       11x 1x   10x 1x   9x 1x   8x 2x   6x 2x   4x 2x     2x       1x                    
/** ****************************************************************************************************
 * File: utils.js
 * Project: geohash
 * @author Nick Soggin <iSkore@users.noreply.github.com> on 19-Feb-2019
 *******************************************************************************************************/
'use strict';
 
const
	{
		ENCODE_AUTO,
		MIN_LNG,
		MIN_LAT,
		MAX_LNG,
		MAX_LAT
	} = require( './variables' );
 
/**
 * isNumber
 * @description
 * determine if value is a valid number
 * @param {number} n - value
 * @return {boolean} - true if value is a number
 */
function isNumber( n ) {
	return n === n && n === +n;
}
 
/**
 * clamp
 * @description
 * basic number clamp
 * @param {number} n - number to clamp
 * @param {number} min - minimum value
 * @param {number} max - maximum value
 * @return {number|*} - clamped value
 */
function clamp( n, min, max ) {
	return Math.min( Math.max( min, n ), max );
}
 
/**
 * longitudeClamp
 * @description
 * Determines if longitude is in the realm of possibility
 * If it's not, clamp the longitude value to the MIN/MAX
 * @param {number} lng - longitude
 * @returns {number} - longitude
 */
function longitudeClamp( lng ) {
	if ( !isNumber( lng ) ) {
		throw new Error( 'number required for `lng`' );
	}
 
	return clamp( lng, MIN_LNG, MAX_LNG );
}
 
/**
 * latitudeClamp
 * @description
 * Determines if latitude is in the realm of possibility
 * If it's not, clamp the latitude value to the MIN/MAX
 * @param {number} lat - latitude
 * @returns {number} - latitude
 */
function latitudeClamp( lat ) {
	if ( !isNumber( lat ) ) {
		throw new Error( 'number required for `lat`' );
	}
 
	return clamp( lat, MIN_LAT, MAX_LAT );
}
 
/**
 * clampRelative
 * @description
 * basic relative value clamp
 * @param {number} n - number to clamp
 * @param {number} min - minimum value
 * @param {number} max - maximum value
 * @return {number|*} - clamped value or offset
 */
function clampRelative( n, min, max ) {
	return isNumber( n ) ?
		n > max ? min + n % max :
			n < min ? max + n % max :
				n : n;
}
 
/**
 * longitudeClampRelative
 * @description
 * Determines if longitude is in the realm of possibility
 * If it's not, return the longitude position relative to the amount offset
 * @param {number} lng - longitude
 * @returns {number} - longitude
 */
function longitudeClampRelative( lng ) {
	if ( !isNumber( lng ) ) {
		throw new Error( 'number required for `lng`' );
	}
 
	return clampRelative( lng, MIN_LNG, MAX_LNG );
}
 
/**
 * latitudeClampRelative
 * @description
 * Determines if latitude is in the realm of possibility
 * If it's not, return the latitude position relative to the amount offset
 * @param {number} lat - latitude
 * @returns {number} - latitude
 */
function latitudeClampRelative( lat ) {
	if ( !isNumber( lat ) ) {
		throw new Error( 'number required for `lat`' );
	}
 
	return clampRelative( lat, MIN_LAT, MAX_LAT );
}
 
/**
 * determinePrecision
 * @description
 * Estimate what precision to use based on the input longitude/latitude
 *
 * @param {number} lng - longitude
 * @param {number} lat - latitude
 * @param {number?} [precision=ENCODE_AUTO] - precision override
 * @returns {number} - precision estimate
 */
function determinePrecision( lng, lat, precision = ENCODE_AUTO ) {
	if ( !isNumber( precision ) ) {
		throw new Error( 'number required for `precision`' );
	}
 
	if ( precision === ENCODE_AUTO ) {
		if ( !isNumber( lng ) ) {
			throw new Error( 'number required for `lng`' );
		}
		else if ( !isNumber( lat ) ) {
			throw new Error( 'number required for `lat`' );
		}
 
		lng = longitudeClampRelative( lng );
		lat = latitudeClampRelative( lat );
 
		if ( ~~lat === lat && ~~lng === lng ) {
			precision = 0;
		}
		else {
			const
				latLen  = +( lat.toString( 10 ).length ),
				lngLen  = +( lng.toString( 10 ).length ),
				average = ( latLen + lngLen ) / 2;
 
			precision = average >= 12 ? 12 : average;
		}
	}
 
	return ~~precision;
}
 
function determineDirection( [ x, y ] ) {
	if ( !isNumber( x ) ) {
		throw new Error( 'number required for `x`' );
	}
	else if ( !isNumber( y ) ) {
		throw new Error( 'number required for `y`' );
	}
	else if ( x === 0 && y === 0 ) {
		return 'c';
	}
	else if ( !( x ^ y ) ) {
		return ( x & y ) === 1 ? 'ne' : 'sw';
	}
	else if ( !!( x & y ) ) {
		return x === 1 ? 'se' : 'nw';
	}
	else if ( ( x | y ) > 0 ) {
		return !!x ? 'e' : 'n';
	}
	else {
		return !!x ? 'w' : 's';
	}
}
 
module.exports = {
	isNumber,
	clampRelative,
	longitudeClamp,
	latitudeClamp,
	longitudeClampRelative,
	latitudeClampRelative,
	determinePrecision,
	determineDirection
};