all files / app/models/ user-city-junction.js

100% Statements 44/44
100% Branches 14/14
100% Functions 10/10
100% Lines 8/8
1 statement, 4 branches Ignored     
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                                                                                65× 65× 65× 65×   65×     65×                         65×        
import Model from 'ember-data/model'
// import attr from 'ember-data/attr'
import {belongsTo} from 'ember-data/relationships'
import {reads} from 'ember-computed'
import computed from 'ember-computed-decorators'
 
const {
  abs,
  atan2,
  cos,
  PI,
  pow,
  sin,
  sqrt
} = Math
 
 
 
export default Model.extend({
 
  // ----- Relationships -----
  user: belongsTo('user'),
  city: belongsTo('city'),
 
 
 
  // ----- Static properties -----
  earthMeanRadiusKm: 6371,
 
 
 
  // ----- Computed properties -----
  name:          reads('user.name'),
  latitude:      reads('user.latitude'),
  longitude:     reads('user.longitude'),
  cityLatitude:  reads('city.latitude'),
  cityLongitude: reads('city.longitude'),
 
  @computed  ('latitude',   'longitude',   'cityLatitude', 'cityLongitude', 'earthMeanRadiusKm')
  distanceKm  (userLatitude, userLongitude, cityLatitude,   cityLongitude,    radius) {
    const phi1    = userLatitude  * PI / 180
    const lambda1 = userLongitude * PI / 180
    const phi2    = cityLatitude  * PI / 180
    const lambda2 = cityLongitude * PI / 180
 
    const deltaLambda = abs((lambda1 > lambda2) ? lambda1 - lambda2 : lambda2 - lambda1)
 
    // special case of the Vincenty formula https://en.wikipedia.org/wiki/Great-circle_distance
    const angleRad = atan2(
      sqrt(
        pow((
          cos(phi2) * sin(deltaLambda)
        ), 2)
        + pow((
          cos(phi1) * sin(phi2) - sin(phi1) * cos(phi2) * cos(deltaLambda)
        ), 2)
      ),
 
      sin(phi1) * sin(phi2) + cos(phi1) * cos(phi2) * cos(deltaLambda)
    )
 
    return angleRad * radius
  }
})