1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 this.EXPORTED_SYMBOLS = ["Dict"];
10 * Transforms a given key into a property name guaranteed not to collide with
13 function convert(aKey) {
18 * Transforms a property into a key suitable for providing to the outside world.
20 function unconvert(aProp) {
21 return aProp.substr(1);
25 * A dictionary of strings to arbitrary JS objects. This should be used whenever
26 * the keys are potentially arbitrary, to avoid collisions with built-in
29 * @param aInitial An object containing the initial keys and values of this
30 * dictionary. Only the "own" enumerable properties of the
31 * object are considered.
32 * If |aInitial| is a string, it is assumed to be JSON and parsed into an object.
34 this.Dict = function Dict(aInitial) {
35 if (aInitial === undefined)
37 if (typeof aInitial == "string")
38 aInitial = JSON.parse(aInitial);
39 var items = {}, count = 0;
40 // That we don't look up the prototype chain is guaranteed by Iterator.
41 for (var [key, val] in Iterator(aInitial)) {
42 items[convert(key)] = val;
45 this._state = {count: count, items: items};
46 return Object.freeze(this);
49 Dict.prototype = Object.freeze({
51 * The number of items in the dictionary.
54 return this._state.count;
58 * Gets the value for a key from the dictionary. If the key is not a string,
59 * it will be converted to a string before the lookup happens.
61 * @param aKey The key to look up
62 * @param [aDefault] An optional default value to return if the key is not
63 * present. Defaults to |undefined|.
64 * @returns The item, or aDefault if it isn't found.
66 get: function Dict_get(aKey, aDefault) {
67 var prop = convert(aKey);
68 var items = this._state.items;
69 return items.hasOwnProperty(prop) ? items[prop] : aDefault;
73 * Sets the value for a key in the dictionary. If the key is a not a string,
74 * it will be converted to a string before the set happens.
76 set: function Dict_set(aKey, aValue) {
77 var prop = convert(aKey);
78 var items = this._state.items;
79 if (!items.hasOwnProperty(prop))
85 * Sets a lazy getter function for a key's value. If the key is a not a string,
86 * it will be converted to a string before the set happens.
90 * A getter function to be called the first time the value for aKey is
91 * retrieved. It is guaranteed that aThunk wouldn't be called more
92 * than once. Note that the key value may be retrieved either
93 * directly, by |get|, or indirectly, by |listvalues| or by iterating
94 * |values|. For the later, the value is only retrieved if and when
95 * the iterator gets to the value in question. Also note that calling
96 * |has| for a lazy-key does not invoke aThunk.
98 * @note No context is provided for aThunk when it's invoked.
99 * Use Function.bind if you wish to run it in a certain context.
101 setAsLazyGetter: function Dict_setAsLazyGetter(aKey, aThunk) {
102 let prop = convert(aKey);
103 let items = this._state.items;
104 if (!items.hasOwnProperty(prop))
107 Object.defineProperty(items, prop, {
110 return items[prop] = aThunk();
118 * Returns whether a key is set as a lazy getter. This returns
119 * true only if the getter function was not called already.
121 * The key to look up.
122 * @returns whether aKey is set as a lazy getter.
124 isLazyGetter: function Dict_isLazyGetter(aKey) {
125 let descriptor = Object.getOwnPropertyDescriptor(this._state.items,
127 return (descriptor && descriptor.get != null);
131 * Returns whether a key is in the dictionary. If the key is a not a string,
132 * it will be converted to a string before the lookup happens.
134 has: function Dict_has(aKey) {
135 return (this._state.items.hasOwnProperty(convert(aKey)));
139 * Deletes a key from the dictionary. If the key is a not a string, it will be
140 * converted to a string before the delete happens.
142 * @returns true if the key was found, false if it wasn't.
144 del: function Dict_del(aKey) {
145 var prop = convert(aKey);
146 if (this._state.items.hasOwnProperty(prop)) {
147 delete this._state.items[prop];
155 * Returns a shallow copy of this dictionary.
157 copy: function Dict_copy() {
159 for (var [key, val] in this.items)
161 return new Dict(newItems);
165 * List and iterator functions
167 * No guarantees whatsoever are made about the order of elements.
171 * Returns a list of all the keys in the dictionary in an arbitrary order.
173 listkeys: function Dict_listkeys() {
174 return [unconvert(k) for (k in this._state.items)];
178 * Returns a list of all the values in the dictionary in an arbitrary order.
180 listvalues: function Dict_listvalues() {
181 var items = this._state.items;
182 return [items[k] for (k in items)];
186 * Returns a list of all the items in the dictionary as key-value pairs
187 * in an arbitrary order.
189 listitems: function Dict_listitems() {
190 var items = this._state.items;
191 return [[unconvert(k), items[k]] for (k in items)];
195 * Returns an iterator over all the keys in the dictionary in an arbitrary
196 * order. No guarantees are made about what happens if the dictionary is
197 * mutated during iteration.
200 // If we don't capture this._state.items here then the this-binding will be
201 // incorrect when the generator is executed
202 var items = this._state.items;
203 return (unconvert(k) for (k in items));
207 * Returns an iterator over all the values in the dictionary in an arbitrary
208 * order. No guarantees are made about what happens if the dictionary is
209 * mutated during iteration.
212 // If we don't capture this._state.items here then the this-binding will be
213 // incorrect when the generator is executed
214 var items = this._state.items;
215 return (items[k] for (k in items));
219 * Returns an iterator over all the items in the dictionary as key-value pairs
220 * in an arbitrary order. No guarantees are made about what happens if the
221 * dictionary is mutated during iteration.
224 // If we don't capture this._state.items here then the this-binding will be
225 // incorrect when the generator is executed
226 var items = this._state.items;
227 return ([unconvert(k), items[k]] for (k in items));
231 * Returns a String representation of this dictionary.
233 toString: function Dict_toString() {
235 [(key + ": " + val) for ([key, val] in this.items)].join(", ") +
240 * Returns a JSON representation of this dictionary.
242 toJSON: function Dict_toJSON() {
244 for (let [key, item] of Iterator(this._state.items)) {
245 obj[unconvert(key)] = item;
247 return JSON.stringify(obj);