addon-sdk/source/lib/sdk/util/list.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/util/list.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,87 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +'use strict';
     1.8 +
     1.9 +module.metadata = {
    1.10 +  "stability": "experimental"
    1.11 +};
    1.12 +
    1.13 +const { Class } = require('../core/heritage');
    1.14 +const listNS = require('../core/namespace').ns();
    1.15 +const { iteratorSymbol } = require('../util/iteration');
    1.16 +
    1.17 +const listOptions = {
    1.18 +  /**
    1.19 +   * List constructor can take any number of element to populate itself.
    1.20 +   * @params {Object|String|Number} element
    1.21 +   * @example
    1.22 +   *    List(1,2,3).length == 3 // true
    1.23 +   */
    1.24 +  initialize: function List() {
    1.25 +    listNS(this).keyValueMap = [];
    1.26 +
    1.27 +    for (let i = 0, ii = arguments.length; i < ii; i++)
    1.28 +      addListItem(this, arguments[i]);
    1.29 +  },
    1.30 +  /**
    1.31 +   * Number of elements in this list.
    1.32 +   * @type {Number}
    1.33 +   */
    1.34 +  get length() listNS(this).keyValueMap.length,
    1.35 +   /**
    1.36 +    * Returns a string representing this list.
    1.37 +    * @returns {String}
    1.38 +    */
    1.39 +  toString: function toString() 'List(' + listNS(this).keyValueMap + ')',
    1.40 +  /**
    1.41 +   * Custom iterator providing `List`s enumeration behavior.
    1.42 +   * We cant reuse `_iterator` that is defined by `Iterable` since it provides
    1.43 +   * iteration in an arbitrary order.
    1.44 +   * @see https://developer.mozilla.org/en/JavaScript/Reference/Statements/for...in
    1.45 +   * @param {Boolean} onKeys
    1.46 +   */
    1.47 +  __iterator__: function __iterator__(onKeys, onKeyValue) {
    1.48 +    let array = listNS(this).keyValueMap.slice(0),
    1.49 +                i = -1;
    1.50 +    for each(let element in array)
    1.51 +      yield onKeyValue ? [++i, element] : onKeys ? ++i : element;
    1.52 +  },
    1.53 +};
    1.54 +listOptions[iteratorSymbol] = function iterator() {
    1.55 +    return listNS(this).keyValueMap.slice(0)[iteratorSymbol]();
    1.56 +};
    1.57 +const List = Class(listOptions);
    1.58 +exports.List = List;
    1.59 +
    1.60 +function addListItem(that, value) {
    1.61 +  let list = listNS(that).keyValueMap,
    1.62 +      index = list.indexOf(value);
    1.63 +
    1.64 +  if (-1 === index) {
    1.65 +    try {
    1.66 +      that[that.length] = value;
    1.67 +    }
    1.68 +    catch (e) {}
    1.69 +    list.push(value);
    1.70 +  }
    1.71 +}
    1.72 +exports.addListItem = addListItem;
    1.73 +
    1.74 +function removeListItem(that, element) {
    1.75 +  let list = listNS(that).keyValueMap,
    1.76 +      index = list.indexOf(element);
    1.77 +
    1.78 +  if (0 <= index) {
    1.79 +    list.splice(index, 1);
    1.80 +    try {
    1.81 +      for (let length = list.length; index < length; index++)
    1.82 +        that[index] = list[index];
    1.83 +      that[list.length] = undefined;
    1.84 +    }
    1.85 +    catch(e){}
    1.86 +  }
    1.87 +}
    1.88 +exports.removeListItem = removeListItem;
    1.89 +
    1.90 +exports.listNS = listNS;

mercurial