"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/umask/index.js" (8 Mar 2017, 2008 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 'use strict';
    2 
    3 var util = require("util");
    4 
    5 function toString(val) {
    6     val = val.toString(8);
    7     while (val.length < 4) {
    8         val = "0" + val;
    9     }
   10     return val;
   11 }
   12 
   13 var defaultUmask = 18; // 0022;
   14 var defaultUmaskString = toString(defaultUmask);
   15 
   16 function validate(data, k, val) {
   17     // must be either an integer or an octal string.
   18     if (typeof val === "number" && !isNaN(val)) {
   19         data[k] = val;
   20         return true;
   21     }
   22 
   23     if (typeof val === "string") {
   24         if (val.charAt(0) !== "0") {
   25             return false;
   26         }
   27         data[k] = parseInt(val, 8);
   28         return true;
   29     }
   30 
   31     return false;
   32 }
   33 
   34 function convert_fromString(val, cb) {
   35     if (typeof val === "string") {
   36         // check for octal string first
   37         if (val.charAt(0) === '0' && /^[0-7]+$/.test(val)) {
   38             val = parseInt(val, 8);
   39         } else if (val.charAt(0) !== '0' && /^[0-9]+$/.test(val)) {
   40             // legacy support for decimal strings
   41             val = parseInt(val, 10);
   42         } else {
   43             return cb(new Error(util.format("Expected octal string, got %j, defaulting to %j",
   44                                             val, defaultUmaskString)),
   45                       defaultUmask);
   46         }
   47     } else if (typeof val !== "number") {
   48         return cb(new Error(util.format("Expected number or octal string, got %j, defaulting to %j",
   49                                         val, defaultUmaskString)),
   50                   defaultUmask);
   51     }
   52 
   53     val = Math.floor(val);
   54 
   55     if ((val < 0) || (val > 511)) {
   56         return cb(new Error(util.format("Must be in range 0..511 (0000..0777), got %j", val)),
   57                   defaultUmask);
   58     }
   59 
   60     cb(null, val);
   61 }
   62 
   63 function fromString(val, cb) {
   64 
   65     // synchronous callback, no zalgo
   66     convert_fromString(val, cb || function (err, result) {
   67         /*jslint unparam:true*/
   68         val = result;
   69     });
   70 
   71     return val;
   72 }
   73 
   74 exports.toString = toString;
   75 exports.fromString = fromString;
   76 exports.validate = validate;
   77