Add WoltLab/WCF/Dictionary.fromObject()
authorTim Düsterhus <duesterhus@woltlab.com>
Sun, 17 May 2015 16:35:41 +0000 (18:35 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Sun, 17 May 2015 16:36:06 +0000 (18:36 +0200)
wcfsetup/install/files/js/WoltLab/WCF/Dictionary.js

index b3cd007341eb9508a7ecc04c5e3d2992cd0f07f5..3998242a02129a98c84813e9d6f4e792ecdaac99 100644 (file)
@@ -1,7 +1,7 @@
 /**
  * Dictionary implemention relying on an object or if supported on a Map to hold key => value data.
  * 
- * @author     Alexander Ebert
+ * @author     Tim Duesterhus, Alexander Ebert
  * @copyright  2001-2015 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @module     WoltLab/WCF/Dictionary
@@ -120,6 +120,26 @@ define(function() {
                }
        };
        
+       /**
+        * Creates a new Dictionary based on the given object.
+        * All properties that are owned by the object will be added
+        * as keys to the resulting Dictionary.
+        * 
+        * @param       {object}        object
+        * @return      {Dictionary}
+        */
+       Dictionary.fromObject = function(object) {
+               var result = new Dictionary();
+               
+               for (var key in object) {
+                       if (object.hasOwnProperty(key)) {
+                               result.set(key, object[key]);
+                       }
+               }
+               
+               return result;
+       };
+       
        Object.defineProperty(Dictionary.prototype, 'size', {
                enumerable: false,
                configurable: true,