"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/array-index/index.js" (7 Feb 2017, 3743 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 
    2 /**
    3  * Module dependencies.
    4  */
    5 
    6 var Symbol = require('es6-symbol');
    7 var debug = require('debug')('array-index');
    8 
    9 var get = Symbol('get');
   10 var set = Symbol('set');
   11 var length = Symbol('length');
   12 
   13 /**
   14  * JavaScript Array "length" is bound to an unsigned 32-bit int.
   15  * See: http://stackoverflow.com/a/6155063/376773
   16  */
   17 
   18 var MAX_LENGTH = Math.pow(2, 32);
   19 
   20 /**
   21  * Module exports.
   22  */
   23 
   24 module.exports = ArrayIndex;
   25 ArrayIndex.get = get;
   26 ArrayIndex.set = set;
   27 
   28 /**
   29  * Subclass this.
   30  */
   31 
   32 function ArrayIndex (_length) {
   33   Object.defineProperty(this, 'length', {
   34     get: getLength,
   35     set: setLength,
   36     enumerable: false,
   37     configurable: true
   38   });
   39 
   40   this[length] = 0;
   41 
   42   if (arguments.length > 0) {
   43     setLength.call(this, _length);
   44   }
   45 }
   46 
   47 /**
   48  * You overwrite the "get" Symbol in your subclass.
   49  */
   50 
   51 ArrayIndex.prototype[ArrayIndex.get] = function () {
   52   throw new Error('you must implement the `ArrayIndex.get` Symbol');
   53 };
   54 
   55 /**
   56  * You overwrite the "set" Symbol in your subclass.
   57  */
   58 
   59 ArrayIndex.prototype[ArrayIndex.set] = function () {
   60   throw new Error('you must implement the `ArrayIndex.set` Symbol');
   61 };
   62 
   63 /**
   64  * Converts this array class into a real JavaScript Array. Note that this
   65  * is a "flattened" array and your defined getters and setters won't be invoked
   66  * when you interact with the returned Array. This function will call the
   67  * getter on every array index of the object.
   68  *
   69  * @return {Array} The flattened array
   70  * @api public
   71  */
   72 
   73 ArrayIndex.prototype.toArray = function toArray () {
   74   var i = 0;
   75   var l = this.length;
   76   var array = new Array(l);
   77   for (; i < l; i++) {
   78     array[i] = this[i];
   79   }
   80   return array;
   81 };
   82 
   83 /**
   84  * Basic support for `JSON.stringify()`.
   85  */
   86 
   87 ArrayIndex.prototype.toJSON = function toJSON () {
   88   return this.toArray();
   89 };
   90 
   91 /**
   92  * toString() override. Use Array.prototype.toString().
   93  */
   94 
   95 ArrayIndex.prototype.toString = function toString () {
   96   var a = this.toArray();
   97   return a.toString.apply(a, arguments);
   98 };
   99 
  100 /**
  101  * inspect() override. For the REPL.
  102  */
  103 
  104 ArrayIndex.prototype.inspect = function inspect () {
  105   var a = this.toArray();
  106   Object.keys(this).forEach(function (k) {
  107     a[k] = this[k];
  108   }, this);
  109   return a;
  110 };
  111 
  112 /**
  113  * Getter for the "length" property.
  114  * Returns the value of the "length" Symbol.
  115  */
  116 
  117 function getLength () {
  118   debug('getting "length": %o', this[length]);
  119   return this[length];
  120 };
  121 
  122 /**
  123  * Setter for the "length" property.
  124  * Calls "ensureLength()", then sets the "length" Symbol.
  125  */
  126 
  127 function setLength (v) {
  128   debug('setting "length": %o', v);
  129   return this[length] = ensureLength(this, v);
  130 };
  131 
  132 /**
  133  * Ensures that getters/setters from 0 up to "_newLength" have been defined
  134  * on `Object.getPrototypeOf(this)`.
  135  *
  136  * @api private
  137  */
  138 
  139 function ensureLength (self, _newLength) {
  140   var newLength;
  141   if (_newLength > MAX_LENGTH) {
  142     newLength = MAX_LENGTH;
  143   } else {
  144     newLength = _newLength | 0;
  145   }
  146   var proto = Object.getPrototypeOf(self);
  147   var cur = proto[length] | 0;
  148   var num = newLength - cur;
  149   if (num > 0) {
  150     var desc = {};
  151     debug('creating a descriptor object with %o entries', num);
  152     for (var i = cur; i < newLength; i++) {
  153       desc[i] = setup(i);
  154     }
  155     debug('calling `Object.defineProperties()` with %o entries', num);
  156     Object.defineProperties(proto, desc);
  157     proto[length] = newLength;
  158   }
  159   return newLength;
  160 }
  161 
  162 /**
  163  * Returns a property descriptor for the given "index", with "get" and "set"
  164  * functions created within the closure.
  165  *
  166  * @api private
  167  */
  168 
  169 function setup (index) {
  170   function get () {
  171     return this[ArrayIndex.get](index);
  172   }
  173   function set (v) {
  174     return this[ArrayIndex.set](index, v);
  175   }
  176   return {
  177     enumerable: true,
  178     configurable: true,
  179     get: get,
  180     set: set
  181   };
  182 }