1class Umask {} 2const parse = val => { 3 // this is run via nopt and parse field where everything is 4 // converted to a string first, ignoring coverage for now 5 // instead of figuring out what is happening under the hood in nopt 6 // istanbul ignore else 7 if (typeof val === 'string') { 8 if (/^0o?[0-7]+$/.test(val)) { 9 return parseInt(val.replace(/^0o?/, ''), 8) 10 } else if (/^[1-9][0-9]*$/.test(val)) { 11 return parseInt(val, 10) 12 } else { 13 throw new Error(`invalid umask value: ${val}`) 14 } 15 } else { 16 if (typeof val !== 'number') { 17 throw new Error(`invalid umask value: ${val}`) 18 } 19 val = Math.floor(val) 20 if (val < 0 || val > 511) { 21 throw new Error(`invalid umask value: ${val}`) 22 } 23 return val 24 } 25} 26 27const validate = (data, k, val) => { 28 try { 29 data[k] = parse(val) 30 return true 31 } catch (er) { 32 return false 33 } 34} 35 36module.exports = { Umask, parse, validate } 37