All files / calendrica-js/src general.js

84.78% Statements 78/92
61.9% Branches 13/21
76.31% Functions 29/38
88.52% Lines 54/61

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      3992643x     33x     8556x     24x 6732x       24x         24x 9497x 9497x 560x 8937x 4446x   4491x       24x 8692x 8264x 428x         24x     11995332x 2858440x         24x 492679x 492679x 492679x 1840243x   492679x       165x       24x                 21723x     4385x     87464x     23283x     3209081x     3077619x     114256x     17206x     16743x     2893x       24x 3647x     3647x 3647x         24x     24x     24x     33x     24x     33x     24x     24x     33x     24x     24x   24x                                                                      
// Mathematical Mod
// Implement mod function that does negative wrap-around
// https://stackoverflow.com/a/17323608
const mod = ( n, m ) => ( ( n % m ) + m ) % m
 
// The value of (x mod y) with y instead of 0.
const amod = ( x, y ) => y + mod( x, -y )
 
// The value of x shifted into the range [a..b). Returns x if a=b.
const mod3 = ( x, a, b ) => ( a === b ? x : a + mod( ( x - a ), ( b - a ) ) )
 
// First integer greater or equal to initial such that condition holds.
const next = ( initial, condition ) => (
  condition( initial ) ? initial : next( initial + 1, condition )
)
 
// Last integer greater or equal to initial such that condition holds.
const final = ( initial, condition ) => (
  !condition( initial ) ? initial - 1 : final( initial + 1, condition )
)
 
// Bisection search for x in [lo..hi] such that end holds. test determines when to go left.
const binarySearch = ( lo, hi, test, end ) => {
  const x = ( lo + hi ) / 2
  if ( test( lo, hi ) ) {
    return x
  } if ( end( x ) ) {
    return binarySearch( lo, x, test, end )
  }
  return binarySearch( x, hi, test, end )
}
 
// Use bisection to find inverse of angular function f at y within interval [a..b].
const invertAngular = ( f, y, a, b, prec = 10 ** -5 ) => {
  const p = ( l, h ) => ( h - l ) <= prec
  const e = x => mod( ( f( x ) - y ), 360 ) < 180
  return binarySearch( a, b, p, e )
}
 
// list is of the form ((i1 l1)...(in ln)).
// Sum of body for indices i1...in running simultaneously thru lists l1...ln.
const sigma = ( list, body ) => {
  // Zip function
  // https://stackoverflow.com/a/10284006
  const zip = rows => rows[ 0 ].map( ( _, c ) => rows.map( row => row[ c ] ) )
  return zip( list ).reduce( ( a, c ) => a + body( c ), 0 )
}
 
// Sum powers of x with coefficients (from order 0 up) in list a.
// Taken from https://github.com/espinielli/pycalcal
const poly = ( x, a ) => {
  const n = a.length - 1
  let p = a[ n ]
  for ( let i = 1; i < ( n + 1 ); i += 1 ) {
    p = p * x + a[ n - i ]
  }
  return p
}
 
// Time from moment tee.
const timeFromMoment = tee => mod( tee, 1 )
 
// Clock time hour:minute:second from moment tee.
// Based on calendrica 3.0
const clockFromMoment = tee => {
  const time = timeFromMoment( tee )
  const hour = Math.floor( time * 24 )
  const minute = Math.floor( mod( time * 24 * 60, 60 ) )
  const second = mod( time * 24 * 60 * 60, 60 )
  return { hour, minute, second }
}
 
// x hours.
const hr = x => x / 24
 
// x seconds.
const sec = x => x / 24 / 60 / 60
 
// Return an angle data structure from d degrees, m arcminutes and s arcseconds.
const angle = ( d, m, s ) => d + ( ( m + ( s / 60 ) ) / 60 )
 
// Convert angle theta from radians to degrees.
const degreesFromRadians = theta => mod( ( theta * ( 180 / Math.PI ) ), 360 )
 
// Convert angle theta from degrees to radians.
const radiansFromDegrees = theta => mod( theta, 360 ) * ( Math.PI / 180 )
 
// Sine of theta (given in degrees).
const sinDegrees = theta => Math.sin( radiansFromDegrees( theta ) )
 
// Cosine of theta (given in degrees).
const cosDegrees = theta => Math.cos( radiansFromDegrees( theta ) )
 
// Tangent of theta (given in degrees).
const tanDegrees = theta => Math.tan( radiansFromDegrees( theta ) )
 
// Arcsine of x in degrees.
const arcsinDegrees = x => degreesFromRadians( Math.asin( x ) )
 
// Arccosine of x in degrees.
const arccosDegrees = x => degreesFromRadians( Math.acos( x ) )
 
// Arctangent of y/x in degrees.
// Returns bogus if x and y are both 0.
const arctanDegrees = ( y, x ) => {
  Iif ( x === 0 && y === 0 ) {
    return null
  }
  const alpha = degreesFromRadians( Math.atan( y / x ) )
  return mod( x === 0 ? Math.sign( y ) * 90 : ( x >= 0 ? alpha : alpha + 180 ), 360 )
}
 
// The residue class of the day of the week of date.
// "- 0 - 0" is superfluous. To change to any epoch.
const dayOfWeekFromFixed = date => mod( date, 7 )
 
// Fixed time of start of the julian day number.
const JD_EPOCH = -1721424.5
 
// Moment of julian day number jd.
const momentFromJd = jd => jd + JD_EPOCH
 
// Julian day number of moment tee.
const jdFromMoment = tee => tee - JD_EPOCH
 
// Fixed date of julian day number jd.
const fixedFromJd = jd => Math.floor( momentFromJd( jd ) )
 
// Julian day number of fixed date.
const jdFromFixed = date => jdFromMoment( date )
 
// Fixed date of the start of the Unix second count.
const UNIX_EPOCH = 719163
 
// Fixed date from Unix second count s
const momentFromUnix = s => UNIX_EPOCH + ( s / ( 24 * 60 * 60 ) )
 
// Unix second count from moment tee
const unixFromMoment = tee => 24 * 60 * 60 * ( tee - UNIX_EPOCH )
 
// True if tee is in half-open range.
const isInRange = ( tee, range ) => range[ 0 ] <= tee && tee < range[ 1 ]
 
// Those moments in list ell that occur in range.
const listRange = ( ell, range ) => ell.filter( tee => isInRange( tee, range ) )
 
module.exports = {
  mod,
  amod,
  mod3,
  next,
  final,
  binarySearch,
  invertAngular,
  sigma,
  poly,
  timeFromMoment,
  clockFromMoment,
  hr,
  sec,
  angle,
  degreesFromRadians,
  radiansFromDegrees,
  sinDegrees,
  cosDegrees,
  tanDegrees,
  arcsinDegrees,
  arccosDegrees,
  arctanDegrees,
  dayOfWeekFromFixed,
  JD_EPOCH,
  momentFromJd,
  jdFromMoment,
  fixedFromJd,
  jdFromFixed,
  UNIX_EPOCH,
  momentFromUnix,
  unixFromMoment,
  isInRange,
  listRange,
}