Added WoltLab/WCF/List as a shim for Set
authorAlexander Ebert <ebert@woltlab.com>
Sun, 24 May 2015 23:26:39 +0000 (01:26 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 24 May 2015 23:26:39 +0000 (01:26 +0200)
wcfsetup/install/files/js/WoltLab/WCF/List.js [new file with mode: 0644]
wcfsetup/install/files/js/require.config.js

diff --git a/wcfsetup/install/files/js/WoltLab/WCF/List.js b/wcfsetup/install/files/js/WoltLab/WCF/List.js
new file mode 100644 (file)
index 0000000..f79d54a
--- /dev/null
@@ -0,0 +1,112 @@
+/**
+ * List implementation relying on an array or if supported on a Set to hold values.
+ * 
+ * @author     Alexander Ebert
+ * @copyright  2001-2015 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module     WoltLab/WCF/List
+ */
+define([], function() {
+       "use strict";
+       
+       var _hasSet = window.hasOwnProperty('Set') && typeof window.Set === 'function';
+       
+       /**
+        * @constructor
+        */
+       function List() {
+               this._set = (_hasSet) ? new Set() : [];
+       };
+       List.prototype = {
+               /**
+                * Appends an element to the list, silently rejects adding an already existing value.
+                * 
+                * @param       {*}     value   unique element
+                */
+               add: function(value) {
+                       if (_hasSet) {
+                               this._set.add(value);
+                       }
+                       else if (!this.has(value)) {
+                               this._set.push(value);
+                       }
+               },
+               
+               /**
+                * Removes all elements from the list.
+                */
+               clear: function() {
+                       if (_hasSet) {
+                               this._set.clear();
+                       }
+                       else {
+                               this._set = [];
+                       }
+               },
+               
+               /**
+                * Removes an element from the list, returns true if the element was in the list.
+                * 
+                * @param       {*}             value   element
+                * @return      {boolean}       true if element was in the list
+                */
+               'delete': function(value) {
+                       if (_hasSet) {
+                               return this._set['delete'](value);
+                       }
+                       else {
+                               var index = this._set.indexOf(index);
+                               if (index === -1) {
+                                       return false;
+                               }
+                               
+                               this._set.splice(index, 1);
+                               return true;
+                       }
+               },
+               
+               /**
+                * Calls `callback` for each element in the list.
+                */
+               forEach: function(callback) {
+                       if (_hasSet) {
+                               this._set.forEach(callback);
+                       }
+                       else {
+                               for (var i = 0, length = this._set.length; i < length; i++) {
+                                       callback(this._set[i]);
+                               }
+                       }
+               },
+               
+               /**
+                * Returns true if the list contains the element.
+                * 
+                * @param       {*}             value   element
+                * @return      {boolean}       true if element is in the list
+                */
+               has: function(value) {
+                       if (_hasSet) {
+                               return this._set.has(value);
+                       }
+                       else {
+                               return (this._set.indexOf(value) !== -1);
+                       }
+               }
+       };
+       
+       Object.defineProperty(List.prototype, 'size', {
+               enumerable: false,
+               configurable: true,
+               get: function() {
+                       if (_hasMap) {
+                               return this._set.size;
+                       }
+                       else {
+                               return this._set.length;
+                       }
+               }
+       });
+       
+       return List;
+});
index 17a8071165a06411efcb0e3262c99759130c6587..443999a248799b294cde09fc9a2cb56f3dca3d96 100644 (file)
@@ -17,6 +17,7 @@ requirejs.config({
                        'Environment': 'WoltLab/WCF/Environment',
                        'EventHandler': 'WoltLab/WCF/Event/Handler',
                        'Language': 'WoltLab/WCF/Language',
+                       'List': 'WoltLab/WCF/List',
                        'ObjectMap': 'WoltLab/WCF/ObjectMap',
                        'UI/Alignment': 'WoltLab/WCF/UI/Alignment',
                        'UI/Dialog': 'WoltLab/WCF/UI/Dialog',