All files Bounds.js

100% Statements 12/12
100% Branches 15/15
100% Functions 1/1
100% Lines 12/12
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 691x                               25x               25x               5x                 138x 46x 1x   45x 45x 45x                 12x               1x        
import {
    LatLng
} from './LatLng.js';
 
/**
 * @author Michael Duve <mduve@designmail.net>
 * @file represents boundaries of a geographic coordinate system
 * @copyright Michael Duve 2016
 */
export class Bounds {
 
    /**
     * get width of boundaries
     * @return {Number} distance between east and west boundary
     */
    get width() {
        return Math.abs(this.se.lng - this.nw.lng);
    }
 
    /**
     * get height of boundaries
     * @return {Number} distance between north and south boundary
     */
    get height() {
        return Math.abs(this.se.lat - this.nw.lat);
    }
 
    /**
     * get center of boundaries
     * @return {LatLng} half the distance between northwest and southeast boundary
     */
    get center() {
        return this.nw.clone.add(this.se.clone.substract(this.nw).divide(2));
    }
 
    /**
     * @constructor
     * @param  {Number} northWest = new LatLng() - representation of northWest boundary
     * @param  {Number} southEast = new LatLng() - representation of southEast boundary
     * @return {Bounds} instance of Bounds for chaining
     */
    constructor(northWest = new LatLng(), southEast = new LatLng()) {
        if (northWest.lat < southEast.lat || northWest.lng > southEast.lng) {
            throw new Error(`${northWest} needs to be top-left corner and ${southEast} bottom-right`);
        }
        this.nw = northWest;
        this.se = southEast;
        return this;
    }
 
    /**
     * check if specified bounds equals this bounds
     * @param  {Bounds} bounds - specified bounds
     * @return {Boolean} Whether they are equal or not
     */
    equals(bounds) {
        return (bounds instanceof Bounds) && this.nw.equals(bounds.nw) && this.se.equals(bounds.se);
    }
 
    /**
     * string representation
     * @return {String} string representation of object
     */
    toString() {
        return `(${this.nw}, ${this.se})`;
    }
 
}