time-to-botec

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

browser.js (3942B)


      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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     24 var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
     25 var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
     26 var isObject = require( '@stdlib/assert/is-plain-object' );
     27 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     28 var toSymbolic = require( './to_symbolic.js' );
     29 var fromSymbolic = require( './from_symbolic.js' );
     30 
     31 
     32 // VARIABLES //
     33 
     34 var MASK = 0|0; // asm type annotation
     35 
     36 
     37 // MAIN //
     38 
     39 /**
     40 * Get/set the process mask.
     41 *
     42 * ## Notes
     43 *
     44 * -   If provided a mask, the function sets the current mask and returns the previous process mask. Otherwise, the function returns the current process mask.
     45 * -   Browser environments do not support process masks. Hence, this function always returns either `0` or `u=rwx,g=rwx,o=rwx`.
     46 *
     47 *
     48 * @param {(NonNegativeInteger|string)} [mask] - mask
     49 * @param {Object} [options] - options
     50 * @param {boolean} [options.symbolic] - boolean indicating whether to return a mask using symbolic notation
     51 * @throws {TypeError} must provide either a string, nonnegative integer, or an options object
     52 * @throws {TypeError} must provide valid options
     53 * @throws {Error} must provide a parseable expression mask
     54 * @returns {(NonNegativeInteger|string)} process mask
     55 *
     56 * @example
     57 * var mask = umask();
     58 * // returns 0
     59 */
     60 function umask() {
     61 	var options;
     62 	var nargs;
     63 	var mask;
     64 	var opts;
     65 	var arg;
     66 
     67 	nargs = arguments.length;
     68 	if ( nargs === 0 ) {
     69 		return MASK;
     70 	}
     71 	opts = {};
     72 	arg = arguments[ 0 ];
     73 	if ( nargs === 1 ) {
     74 		if ( isString( arg ) ) {
     75 			mask = fromSymbolic( MASK, arg );
     76 			if ( mask instanceof Error ) {
     77 				throw mask;
     78 			}
     79 			return MASK;
     80 		}
     81 		if ( isNonNegativeInteger( arg ) ) {
     82 			// Easy case where we use the built-in function to set the mask and return its return value:
     83 			return MASK;
     84 		}
     85 		if ( isObject( arg ) ) {
     86 			if ( hasOwnProp( arg, 'symbolic' ) ) {
     87 				opts.symbolic = arg.symbolic;
     88 				if ( !isBoolean( opts.symbolic ) ) {
     89 					throw new TypeError( 'invalid option. `symbolic` option must be a boolean. Option: `' + opts.symbolic + '`.' );
     90 				}
     91 			}
     92 			mask = MASK;
     93 			if ( opts.symbolic ) {
     94 				mask = toSymbolic( mask );
     95 			}
     96 			return mask;
     97 		}
     98 		throw new TypeError( 'invalid argument. Must provide either a string, nonnegative integer, or an options object. Value: `' + arg + '`.' );
     99 	}
    100 	options = arguments[ 1 ];
    101 	if ( !isObject( options ) ) {
    102 		throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' );
    103 	}
    104 	if ( hasOwnProp( options, 'symbolic' ) ) {
    105 		opts.symbolic = options.symbolic;
    106 		if ( !isBoolean( opts.symbolic ) ) {
    107 			throw new TypeError( 'invalid option. `symbolic` option must be a boolean. Option: `' + opts.symbolic + '`.' );
    108 		}
    109 	}
    110 	if ( isString( arg ) ) {
    111 		mask = fromSymbolic( MASK, arg );
    112 		if ( mask instanceof Error ) {
    113 			throw mask;
    114 		}
    115 	} else if ( isNonNegativeInteger( arg ) ) {
    116 		mask = arg;
    117 	} else {
    118 		throw new TypeError( 'invalid argument. First argument must be either a string or nonnegative integer. Value: `' + arg + '`.' );
    119 	}
    120 	// Set the mask:
    121 	mask = MASK;
    122 
    123 	// Determine how to format the output value:
    124 	if ( opts.symbolic ) {
    125 		mask = toSymbolic( mask );
    126 	}
    127 	return mask;
    128 }
    129 
    130 
    131 // EXPORTS //
    132 
    133 module.exports = umask;