All files / src/modules address.js

100% Statements 291/291
100% Branches 70/70
100% Functions 29/29
100% Lines 291/291

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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 2921x 1x 22x 22x 1x 1x 18x 18x 18x 18x 16x 16x 16x 1x 16x 15x 15x 16x 18x 18x 18x 1x 1x 5x 5x 5x 3x 3x 2x 2x 2x 1x 1x 9x 9x 9x 7x 6x 7x 1x 1x 9x 1x 1x 1x 1x 9x 9x 9x 9x 2x 2x 2x 2x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 8x 8x 14x 14x 5x 14x 6x 9x 3x 3x 14x 14x 14x 1x 1x 7x 7x 7x 7x 5x 5x 7x 7x 7x 7x 1x 1x 1x 7x 3x 3x 3x 3x 2x 2x 2x 3x 1x 1x 1x 1x 7x 7x 7x 1x 1x 10x 10x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 3x 3x 1x 1x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 1x 1x 3x 1x 3x 3x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 1x 1x 13x 13x 13x 13x 13x 1x 1x 3x 3x 3x 1x 3x 2x 2x 3x 3x 3x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 4x 4x 4x 2x 2x 2x 2x 2x 2x 4x 4x 4x 1x 1x 1x 9x 9x 9x 9x 2x 2x 7x 7x 36x 36x 7x 7x 14x 14x 7x 7x 3x 3x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 7x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x  
export class Address {
  constructor(pure) {
    this.pure = pure;
  }
 
  zipCode(format) {
    let def = format;
 
    // if zip format is not specified, use the zip format defined for the locale
    if (typeof def === 'undefined') {
      const localeFormat = this.pure.registeredModules.address.postcode;
 
      if (typeof localeFormat === 'string') {
        def = localeFormat;
      } else {
        def = this.pure.random.arrayElement(localeFormat);
      }
    }
 
    return this.pure.helpers.replaceSymbols(def);
  }
 
  zipCodeByState(state) {
    const zipRange = this.pure.registeredModules.address.postcodeByState[state];
 
    if (zipRange) {
      return this.pure.random.number(zipRange);
    }
 
    return this.pure.address.zipCode();
  }
 
  city(state) {
    let def = state;
 
    if (typeof def !== 'string') {
      if (this.pure.registeredModules.address.city[def] === undefined) {
        def = this.pure.random.objectElement(this.pure.registeredModules.address.city, 'key');
      } else {
        def = this.pure.address.state();
      }
    } else if (def.length < 1) {
      def = this.pure.random.objectElement(this.pure.registeredModules.address.city, 'key');
    } else {
      def = def.toLowerCase().charAt(0).toUpperCase() + def.slice(1);
    }
 
    let result = this.pure.random.arrayElement(this.pure.registeredModules.address.city[def]);
 
    if (result === undefined || result.length <= 1) {
      result = this.pure.fake.parse(
        this.pure.random.arrayElement(this.pure.registeredModules.address.city)
      );
    }
 
    return result;
  }
 
  cityPrefix() {
    return this.pure.random.arrayElement(this.pure.registeredModules.address.cityPrefix);
  }
 
  citySuffix() {
    return this.pure.random.arrayElement(this.pure.registeredModules.address.citySuffix);
  }
 
  cityName() {
    return this.pure.address.city();
  }
 
  // TODO: Refactor to use real street names not pure.name.lastName()
  streetName() {
    let result;
    const random = this.pure.random.number(1);
    let suffix = this.pure.address.streetSuffix();
 
    if (suffix !== '') {
      suffix = ` ${suffix}`;
    }
 
    if (random === 0) {
      result = this.pure.name.lastName() + suffix;
    } else if (random === 1) {
      result = this.pure.name.firstName() + suffix;
    } else {
      result = this.pure.address.streetSuffix();
    }
 
    return result;
  }
 
  streetAddress(useFullAddress) {
    let def = useFullAddress;
    const random = this.pure.random.number(2);
 
    if (def === undefined) {
      def = false;
    }
 
    let address = '';
 
    if (random === 0) {
      address = `${this.pure.helpers.replaceSymbolWithNumber({
        string: '#####'
      })} ${this.pure.address.streetName()}`;
    } else if (random === 1) {
      address = `${this.pure.helpers.replaceSymbolWithNumber({
        string: '####'
      })} ${this.pure.address.streetName()}`;
    } else if (random === 2) {
      address = `${this.pure.helpers.replaceSymbolWithNumber({
        string: '###'
      })} ${this.pure.address.streetName()}`;
    } else {
      address = `${this.pure.helpers.replaceSymbolWithNumber({
        string: '##'
      })} ${this.pure.address.streetName()}`;
    }
 
    return def ? `${address} ${this.secondaryAddress()}` : address;
  }
 
  streetSuffix() {
    return this.pure.random.arrayElement(this.pure.registeredModules.address.streetSuffix);
  }
 
  streetPrefix() {
    return this.pure.random.arrayElement(this.pure.registeredModules.address.streetPrefix);
  }
 
  secondaryAddress() {
    return this.pure.helpers.replaceSymbolWithNumber({
      string: this.pure.random.arrayElement(['Apt. ###', 'Suite ###'])
    });
  }
 
  county() {
    // TODO: Add province support
    return this.pure.random.arrayElement(this.pure.registeredModules.address.county);
  }
 
  country() {
    return this.pure.random.arrayElement(this.pure.registeredModules.address.country);
  }
 
  defaultCountry() {
    return this.pure.random.arrayElement(this.pure.registeredModules.address.defaultCountry);
  }
 
  countryCode(alphaCode) {
    let code;
 
    if (alphaCode === 'alpha-3') {
      code = this.pure.random.arrayElement(this.pure.registeredModules.address.countryCodeAlpha3);
    } else {
      code = this.pure.random.arrayElement(this.pure.registeredModules.address.countryCode);
    }
 
    return code;
  }
 
  state(useAbbr) {
    return useAbbr
      ? this.pure.random.arrayElement(this.pure.registeredModules.address.stateAbbr)
      : this.pure.random.arrayElement(this.pure.registeredModules.address.state);
  }
 
  stateAbbr() {
    return this.pure.random.arrayElement(this.pure.registeredModules.address.stateAbbr);
  }
 
  latitude(options) {
    const def = options || {};
    const { max = 90, min = -90, precision = 4 } = def;
 
    return this.pure.random.number({ min, max, precision }).toFixed(precision);
  }
 
  longitude(options) {
    const def = options || {};
    const { max = 180, min = -180, precision = 4 } = def;
 
    return this.pure.random.number({ min, max, precision }).toFixed(precision);
  }
 
  direction(useAbbr) {
    let direction;
 
    if (useAbbr) {
      direction = this.pure.random.objectElement(this.pure.registeredModules.address.directionAbbr);
    } else {
      direction = this.pure.random.objectElement(this.pure.registeredModules.address.direction);
    }
 
    return this.pure.random.arrayElement(direction);
  }
 
  cardinalDirection(useAbbr) {
    let result = '';
 
    if (useAbbr) {
      result = this.pure.random.arrayElement(
        this.pure.registeredModules.address.directionAbbr.cardinal
      );
    } else {
      result = this.pure.random.arrayElement(
        this.pure.registeredModules.address.direction.cardinal
      );
    }
 
    return result;
  }
 
  ordinalDirection(useAbbr) {
    let result = '';
 
    if (useAbbr) {
      result = this.pure.random.arrayElement(
        this.pure.registeredModules.address.directionAbbr.ordinal
      );
    } else {
      result = this.pure.random.arrayElement(this.pure.registeredModules.address.direction.ordinal);
    }
 
    return result;
  }
 
  // TODO: rename radius param to distance
  nearbyGPSCoordinate(options) {
    const def = options || {};
    const { coordinate, radius = 10.0, isMetric = false } = def;
    // If there is no coordinate, the best we can do is return a random GPS coordinate.
    if (coordinate === undefined) {
      return [this.pure.address.latitude(), this.pure.address.longitude()];
    }
 
    function degreesToRadians(degrees) {
      return degrees * (Math.PI / 180.0);
    }
 
    function radiansToDegrees(radians) {
      return radians * (180.0 / Math.PI);
    }
 
    function kilometersToMiles(miles) {
      return miles * 0.621371;
    }
 
    function coordinateWithOffset(coord, bearing, distance, ismetric) {
      // Radius of the Earth (http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html)
      const R = 6378.137;
      // Distance in km
      const d = ismetric ? distance : kilometersToMiles(distance);
      // Current lat point converted to radians
      const lat1 = degreesToRadians(coord[0]);
      // Current long point converted to radians
      const lon1 = degreesToRadians(coord[1]);
      const lat2 = Math.asin(
        Math.sin(lat1) * Math.cos(d / R) + Math.cos(lat1) * Math.sin(d / R) * Math.cos(bearing)
      );
      let lon2 =
        lon1 +
        Math.atan2(
          Math.sin(bearing) * Math.sin(d / R) * Math.cos(lat1),
          Math.cos(d / R) - Math.sin(lat1) * Math.sin(lat2)
        );
 
      // Keep longitude in range [-180, 180]
      if (lon2 > degreesToRadians(180)) {
        lon2 -= degreesToRadians(360);
      } else if (lon2 < degreesToRadians(-180)) {
        lon2 += degreesToRadians(360);
      }
 
      return [radiansToDegrees(lat2), radiansToDegrees(lon2)];
    }
    // This approach will likely result in a higher density of points near the center.
    const randomCoord = coordinateWithOffset(
      coordinate,
      degreesToRadians(Math.random() * 360.0),
      radius,
      isMetric
    );
 
    return [randomCoord[0].toFixed(4), randomCoord[1].toFixed(4)];
  }
 
  timeZone() {
    return this.pure.random.arrayElement(this.pure.registeredModules.address.timeZone);
  }
}