main.js (2134B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isDateObject = require( './../../is-date-object' ); 24 var isInteger = require( './../../is-integer' ).isPrimitive; 25 26 27 // MAIN // 28 29 /** 30 * Tests whether a value corresponds to a leap year in the Gregorian calendar. 31 * 32 * ## Notes 33 * 34 * - According to the Gregorian calendar, every year that is exactly divisible by `4` is a leap year, except those years which are also divisible by `100` and not by `400` (e.g., `1900`). 35 * 36 * @param {*} [value] - input value 37 * @returns {boolean} boolean whether a value corresponds to a leap year 38 * 39 * @example 40 * var bool = isLeapYear(); 41 * // returns <boolean> 42 * 43 * @example 44 * var bool = isLeapYear( new Date() ); 45 * // returns <boolean> 46 * 47 * @example 48 * var bool = isLeapYear( 1996 ); 49 * // returns true 50 * 51 * @example 52 * var bool = isLeapYear( 2001 ); 53 * // returns false 54 */ 55 function isLeapYear( value ) { 56 var yr; 57 if ( arguments.length ) { 58 if ( isDateObject( value ) ) { 59 yr = value.getFullYear(); 60 } else if ( isInteger( value ) ) { 61 yr = value; 62 } else { 63 return false; 64 } 65 } else { 66 // Note: cannot cache, as possible for application to cross into a new year: 67 yr = ( new Date() ).getFullYear(); 68 } 69 // Special case if year is a new century... 70 if ( (yr % 100) === 0 ) { 71 // Centuries are only leap years at the end of "leap cycles" which happen every `400` years: 72 return ( (yr % 400) === 0 ); 73 } 74 // All other years which are exactly divisible by `4` are leap years: 75 return ( (yr % 4) === 0 ); 76 } 77 78 79 // EXPORTS // 80 81 module.exports = isLeapYear;