From 10f1c560a15f6177c056ddafa16d45e72c4493a4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Sun, 17 May 2015 18:35:41 +0200 Subject: [PATCH] Add WoltLab/WCF/Dictionary.fromObject() --- .../files/js/WoltLab/WCF/Dictionary.js | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/wcfsetup/install/files/js/WoltLab/WCF/Dictionary.js b/wcfsetup/install/files/js/WoltLab/WCF/Dictionary.js index b3cd007341..3998242a02 100644 --- a/wcfsetup/install/files/js/WoltLab/WCF/Dictionary.js +++ b/wcfsetup/install/files/js/WoltLab/WCF/Dictionary.js @@ -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 * @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, -- 2.20.1