Merge branch '6.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / WoltLabSuite / Core / Dictionary.js
CommitLineData
f10d9af6
AE
1/**
2 * Dictionary implementation relying on an object or if supported on a Map to hold key => value data.
3 *
4 * If you're looking for a dictionary with object keys, please see `WoltLabSuite/Core/ObjectMap`.
5 *
6 * This is a legacy implementation, that does not implement all methods of `Map`, furthermore it has
7 * the side effect of converting all numeric keys to string values, treating 1 === "1".
8 *
9 * @author Tim Duesterhus, Alexander Ebert
10 * @copyright 2001-2019 WoltLab GmbH
11 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
f10d9af6 12 */
fd6a70e0 13define(["require", "exports"], function (require, exports) {
c9c0b67c 14 "use strict";
f10d9af6 15 /** @deprecated 5.4 Use a `Map` instead. */
592ea62f 16 class Dictionary {
2b2ad7a7 17 _dictionary = new Map();
c9c0b67c
AE
18 /**
19 * Sets a new key with given value, will overwrite an existing key.
c9c0b67c 20 */
592ea62f
AE
21 set(key, value) {
22 this._dictionary.set(key.toString(), value);
23 }
c9c0b67c
AE
24 /**
25 * Removes a key from the dictionary.
c9c0b67c 26 */
592ea62f
AE
27 delete(key) {
28 return this._dictionary.delete(key.toString());
29 }
c9c0b67c
AE
30 /**
31 * Returns true if dictionary contains a value for given key and is not undefined.
c9c0b67c 32 */
592ea62f
AE
33 has(key) {
34 return this._dictionary.has(key.toString());
35 }
c9c0b67c
AE
36 /**
37 * Retrieves a value by key, returns undefined if there is no match.
c9c0b67c 38 */
592ea62f
AE
39 get(key) {
40 return this._dictionary.get(key.toString());
41 }
c9c0b67c
AE
42 /**
43 * Iterates over the dictionary keys and values, callback function should expect the
44 * value as first parameter and the key name second.
c9c0b67c 45 */
592ea62f 46 forEach(callback) {
6ab9b916
TD
47 if (typeof callback !== "function") {
48 throw new TypeError("forEach() expects a callback as first parameter.");
f73a2744 49 }
592ea62f
AE
50 this._dictionary.forEach(callback);
51 }
c9c0b67c
AE
52 /**
53 * Merges one or more Dictionary instances into this one.
c9c0b67c 54 */
592ea62f
AE
55 merge(...dictionaries) {
56 for (let i = 0, length = dictionaries.length; i < length; i++) {
57 const dictionary = dictionaries[i];
58 dictionary.forEach((value, key) => this.set(key, value));
c9c0b67c 59 }
592ea62f 60 }
c9c0b67c
AE
61 /**
62 * Returns the object representation of the dictionary.
c9c0b67c 63 */
592ea62f
AE
64 toObject() {
65 const object = {};
6ab9b916 66 this._dictionary.forEach((value, key) => (object[key] = value));
c9c0b67c 67 return object;
c9c0b67c 68 }
592ea62f
AE
69 /**
70 * Creates a new Dictionary based on the given object.
71 * All properties that are owned by the object will be added
72 * as keys to the resulting Dictionary.
73 */
74 static fromObject(object) {
75 const result = new Dictionary();
665fa171
AE
76 Object.keys(object).forEach((key) => {
77 result.set(key, object[key]);
78 });
592ea62f
AE
79 return result;
80 }
81 get size() {
82 return this._dictionary.size;
83 }
84 }
c9c0b67c
AE
85 return Dictionary;
86});