time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

quarter_of_year.js (2435B)


      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( '@stdlib/assert/is-date-object' );
     24 var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     25 var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
     26 var lowercase = require( '@stdlib/string/lowercase' );
     27 var QUARTERS = require( './quarters.json' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Returns the quarter of the year.
     34 *
     35 * @param {(integer|string|Date)} [month] - month (or `Date`)
     36 * @throws {TypeError} must provide either a string, integer, or `Date` object
     37 * @throws {Error} must provide a recognized month
     38 * @throws {RangeError} an integer month argument must be on the interval `[1,12]`
     39 * @returns {integer} quarter of the year
     40 *
     41 * @example
     42 * var q = quarterOfYear( new Date() );
     43 * // returns <number>
     44 *
     45 * @example
     46 * var q = quarterOfYear( 4 );
     47 * // returns 2
     48 *
     49 * @example
     50 * var q = quarterOfYear( 'June' );
     51 * // returns 2
     52 */
     53 function quarterOfYear( month ) {
     54 	var mon;
     55 	var q;
     56 	if ( arguments.length ) {
     57 		if ( isDateObject( month ) ) {
     58 			mon = month.getMonth() + 1; // zero-based
     59 		} else if ( isString( month ) || isInteger( month ) ) {
     60 			mon = month;
     61 		} else {
     62 			throw new TypeError( 'invalid argument. Must provide either a string, integer, or `Date` object. Value: `'+month+'`.' );
     63 		}
     64 	} else {
     65 		// Note: cannot cache as application may cross over into a new year:
     66 		mon = ( new Date() ).getMonth() + 1; // zero-based
     67 	}
     68 	if ( isInteger( mon ) && (mon < 1 || mon > 12) ) {
     69 		throw new RangeError( 'invalid argument. An integer month value must be on the interval `[1,12]`. Value: `'+mon+'`.' );
     70 	}
     71 	mon = lowercase( mon.toString() );
     72 	q = QUARTERS[ mon ];
     73 	if ( q === void 0 ) {
     74 		throw new Error( 'invalid argument. Must provide a recognized month. Value: `'+month+'`.' );
     75 	}
     76 	return q;
     77 }
     78 
     79 
     80 // EXPORTS //
     81 
     82 module.exports = quarterOfYear;