All files / nanakshahi-js/lib getDateFromNanakshahi.js

88.88% Statements 8/9
83.33% Branches 5/6
100% Functions 1/1
88.88% Lines 8/9

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 573x 3x                       10x         10x     10x                   10x   10x                                           3x  
const { leapYear, toGurmukhiNum } = require( './utils' )
const { months, weekdays } = require( './consts' )
 
/**
 * Converts Nanakshahi Date into the Gregorian Calendar
 * @param {!number} year Nanakshahi Year
 * @param {!number} month Nanakshahi Month, 1-12
 * @param {!number} date Nanakshahi Day
 * @return {Object} Gregorian Date + Nanakshahi Date in English and Punjabi
 * @example getDateFromNanakshahi( 550, 10, 23 )
 */
function getDateFromNanakshahi( year, month, date ) {
  // Check if before 535 N.S. (Nanakshahi Adoption)
  Iif ( year < 535 ) {
    throw new RangeError( 'Nanakshahi Date Out of Range' )
  }
 
  // NS Month Offsets
  const monthOffsets = [ 14, 14, 15, 15, 16, 16, 15, 15, 14, 14, 13, 12 ]
 
  // Date Object
  const gregorianDate = new Date(
    // Calculate Gregorian Year
    month < 11 ? year + 1468 : year + 1469,
    // Set month start from Nanakshahi [0..11]
    month < 11 ? month + 1 : month - 11,
    // Add days to months
    monthOffsets[ month - 1 ] + ( date - 1 ),
  )
 
  // Get Day of Week
  const weekday = gregorianDate.getDay()
 
  return {
    gregorianDate,
    englishDate: {
      month,
      monthName: months[ month - 1 ].en,
      date,
      year,
      day: weekdays[ weekday ].en,
      dayShort: weekdays[ weekday ].enShort,
    },
    punjabiDate: {
      month: toGurmukhiNum( month ),
      monthName: months[ month - 1 ].pa,
      date: toGurmukhiNum( date ),
      year: toGurmukhiNum( year ),
      day: weekdays[ weekday ].pa,
      dayShort: weekdays[ weekday ].paShort,
    },
    leapYear: leapYear( year + 1469 ),
  }
}
 
module.exports = getDateFromNanakshahi