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