"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/path-array/index.js" (11 Apr 2017, 3053 Bytes) of package /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 
    2 /**
    3  * Module dependencies.
    4  */
    5 
    6 var inherits = require('util').inherits;
    7 var delimiter = require('path').delimiter || ':';
    8 var ArrayIndex = require('array-index');
    9 
   10 /**
   11  * Module exports.
   12  */
   13 
   14 module.exports = PathArray;
   15 
   16 /**
   17  * `PathArray` constructor. Treat your `$PATH` like a mutable JavaScript Array!
   18  *
   19  * @param {Env} env - `process.env` object to use.
   20  * @param {String} [property] - optional property name to use (`PATH` by default).
   21  * @public
   22  */
   23 
   24 function PathArray (env, property) {
   25   if (!(this instanceof PathArray)) return new PathArray(env);
   26   ArrayIndex.call(this);
   27 
   28   this.property = property || 'PATH';
   29 
   30   // overwrite only the `get` operator of the ".length" property
   31   Object.defineProperty(this, 'length', {
   32     get: this._getLength
   33   });
   34 
   35   // store the `process.env` object as a non-enumerable `_env`
   36   Object.defineProperty(this, '_env', {
   37     value: env || process.env,
   38     writable: true,
   39     enumerable: false,
   40     configurable: true
   41   });
   42 
   43   // need to invoke the `length` getter to ensure that the
   44   // indexed getters/setters are set up at this point
   45   void(this.length);
   46 }
   47 
   48 // inherit from ArrayIndex
   49 inherits(PathArray, ArrayIndex);
   50 
   51 /**
   52  * Returns the current $PATH representation as an Array.
   53  *
   54  * @api private
   55  */
   56 
   57 PathArray.prototype._array = function () {
   58   var path = this._env[this.property];
   59   if (!path) return [];
   60   return path.split(delimiter);
   61 };
   62 
   63 /**
   64  * Sets the `env` object's `PATH` string to the values in the passed in Array
   65  * instance.
   66  *
   67  * @api private
   68  */
   69 
   70 PathArray.prototype._setArray = function (arr) {
   71   // mutate the $PATH
   72   this._env[this.property] = arr.join(delimiter);
   73 };
   74 
   75 /**
   76  * `.length` getter operation implementation.
   77  *
   78  * @api private
   79  */
   80 
   81 PathArray.prototype._getLength = function () {
   82   var length = this._array().length;
   83 
   84   // invoke the ArrayIndex internal `set` operator to ensure that
   85   // there's getters/setters defined for the determined length so far...
   86   this.length = length;
   87 
   88   return length;
   89 };
   90 
   91 /**
   92  * ArrayIndex [0] getter operator implementation.
   93  *
   94  * @api private
   95  */
   96 
   97 PathArray.prototype[ArrayIndex.get] = function get (index) {
   98   return this._array()[index];
   99 };
  100 
  101 /**
  102  * ArrayIndex [0]= setter operator implementation.
  103  *
  104  * @api private
  105  */
  106 
  107 PathArray.prototype[ArrayIndex.set] = function set (index, value) {
  108   var arr = this._array();
  109   arr[index] = value;
  110   this._setArray(arr);
  111   return value;
  112 };
  113 
  114 /**
  115  * `toString()` returns the current $PATH string.
  116  *
  117  * @api public
  118  */
  119 
  120 PathArray.prototype.toString = function toString () {
  121   return this._env[this.property] || '';
  122 };
  123 
  124 // proxy the JavaScript Array functions, and mutate the $PATH
  125 Object.getOwnPropertyNames(Array.prototype).forEach(function (name) {
  126   if ('constructor' == name) return;
  127   if ('function' != typeof Array.prototype[name]) return;
  128   if (/to(Locale)?String/.test(name)) return;
  129   //console.log('proxy %s', name);
  130 
  131   PathArray.prototype[name] = function () {
  132     var arr = this._array();
  133     var rtn = arr[name].apply(arr, arguments);
  134     this._setArray(arr);
  135     return rtn;
  136   };
  137 });