1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/lib/sdk/util/collection.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,115 @@ 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 + 1.8 +"use strict"; 1.9 + 1.10 +module.metadata = { 1.11 + "stability": "experimental" 1.12 +}; 1.13 + 1.14 +exports.Collection = Collection; 1.15 + 1.16 +/** 1.17 + * Adds a collection property to the given object. Setting the property to a 1.18 + * scalar value empties the collection and adds the value. Setting it to an 1.19 + * array empties the collection and adds all the items in the array. 1.20 + * 1.21 + * @param obj 1.22 + * The property will be defined on this object. 1.23 + * @param propName 1.24 + * The name of the property. 1.25 + * @param array 1.26 + * If given, this will be used as the collection's backing array. 1.27 + */ 1.28 +exports.addCollectionProperty = function addCollProperty(obj, propName, array) { 1.29 + array = array || []; 1.30 + let publicIface = new Collection(array); 1.31 + 1.32 + Object.defineProperty(obj, propName, { 1.33 + configurable: true, 1.34 + enumerable: true, 1.35 + 1.36 + set: function set(itemOrItems) { 1.37 + array.splice(0, array.length); 1.38 + publicIface.add(itemOrItems); 1.39 + }, 1.40 + 1.41 + get: function get() { 1.42 + return publicIface; 1.43 + } 1.44 + }); 1.45 +}; 1.46 + 1.47 +/** 1.48 + * A collection is ordered, like an array, but its items are unique, like a set. 1.49 + * 1.50 + * @param array 1.51 + * The collection is backed by an array. If this is given, it will be 1.52 + * used as the backing array. This way the caller can fully control the 1.53 + * collection. Otherwise a new empty array will be used, and no one but 1.54 + * the collection will have access to it. 1.55 + */ 1.56 +function Collection(array) { 1.57 + array = array || []; 1.58 + 1.59 + /** 1.60 + * Provides iteration over the collection. Items are yielded in the order 1.61 + * they were added. 1.62 + */ 1.63 + this.__iterator__ = function Collection___iterator__() { 1.64 + let items = array.slice(); 1.65 + for (let i = 0; i < items.length; i++) 1.66 + yield items[i]; 1.67 + }; 1.68 + 1.69 + /** 1.70 + * The number of items in the collection. 1.71 + */ 1.72 + this.__defineGetter__("length", function Collection_get_length() { 1.73 + return array.length; 1.74 + }); 1.75 + 1.76 + /** 1.77 + * Adds a single item or an array of items to the collection. Any items 1.78 + * already contained in the collection are ignored. 1.79 + * 1.80 + * @param itemOrItems 1.81 + * An item or array of items. 1.82 + * @return The collection. 1.83 + */ 1.84 + this.add = function Collection_add(itemOrItems) { 1.85 + let items = toArray(itemOrItems); 1.86 + for (let i = 0; i < items.length; i++) { 1.87 + let item = items[i]; 1.88 + if (array.indexOf(item) < 0) 1.89 + array.push(item); 1.90 + } 1.91 + return this; 1.92 + }; 1.93 + 1.94 + /** 1.95 + * Removes a single item or an array of items from the collection. Any items 1.96 + * not contained in the collection are ignored. 1.97 + * 1.98 + * @param itemOrItems 1.99 + * An item or array of items. 1.100 + * @return The collection. 1.101 + */ 1.102 + this.remove = function Collection_remove(itemOrItems) { 1.103 + let items = toArray(itemOrItems); 1.104 + for (let i = 0; i < items.length; i++) { 1.105 + let idx = array.indexOf(items[i]); 1.106 + if (idx >= 0) 1.107 + array.splice(idx, 1); 1.108 + } 1.109 + return this; 1.110 + }; 1.111 +}; 1.112 + 1.113 +function toArray(itemOrItems) { 1.114 + let isArr = itemOrItems && 1.115 + itemOrItems.constructor && 1.116 + itemOrItems.constructor.name === "Array"; 1.117 + return isArr ? itemOrItems : [itemOrItems]; 1.118 +}