aboutsummaryrefslogtreecommitdiffhomepage
path: root/js/map.js
diff options
context:
space:
mode:
authorGravatar Adam Cozzette <acozzette@google.com>2016-11-17 16:48:38 -0800
committerGravatar Adam Cozzette <acozzette@google.com>2016-11-17 16:59:59 -0800
commit5a76e633ea9b5adb215e93fdc11e1c0c08b3fc74 (patch)
tree0276f81f8848a05d84cd7e287b43d665e30f04e3 /js/map.js
parente28286fa05d8327fd6c5aa70cfb3be558f0932b8 (diff)
Integrated internal changes from Google
Diffstat (limited to 'js/map.js')
-rw-r--r--js/map.js54
1 files changed, 53 insertions, 1 deletions
diff --git a/js/map.js b/js/map.js
index 0f8401c4..93f08e03 100644
--- a/js/map.js
+++ b/js/map.js
@@ -131,6 +131,58 @@ jspb.Map.prototype.toArray = function() {
/**
+ * Returns the map formatted as an array of key-value pairs, suitable for the
+ * toObject() form of a message.
+ *
+ * @param {boolean=} includeInstance Whether to include the JSPB instance for
+ * transitional soy proto support: http://goto/soy-param-migration
+ * @param {!function((boolean|undefined),!V):!Object=} valueToObject
+ * The static toObject() method, if V is a message type.
+ * @return {!Array<!Array<!Object>>}
+ */
+jspb.Map.prototype.toObject = function(includeInstance, valueToObject) {
+ var rawArray = this.toArray();
+ var entries = [];
+ for (var i = 0; i < rawArray.length; i++) {
+ var entry = this.map_[rawArray[i][0].toString()];
+ this.wrapEntry_(entry);
+ var valueWrapper = /** @type {!V|undefined} */ (entry.valueWrapper);
+ if (valueWrapper) {
+ goog.asserts.assert(valueToObject);
+ entries.push([entry.key, valueToObject(includeInstance, valueWrapper)]);
+ } else {
+ entries.push([entry.key, entry.value]);
+ }
+ }
+ return entries;
+};
+
+
+/**
+ * Returns a Map from the given array of key-value pairs when the values are of
+ * message type. The values in the array must match the format returned by their
+ * message type's toObject() method.
+ *
+ * @template K, V
+ * @param {!Array<!Array<!Object>>} entries
+ * @param {!function(new:V)|function(new:V,?)} valueCtor
+ * The constructor for type V.
+ * @param {!function(!Object):V} valueFromObject
+ * The fromObject function for type V.
+ * @return {!jspb.Map<K, V>}
+ */
+jspb.Map.fromObject = function(entries, valueCtor, valueFromObject) {
+ var result = new jspb.Map([], valueCtor);
+ for (var i = 0; i < entries.length; i++) {
+ var key = entries[i][0];
+ var value = valueFromObject(entries[i][1]);
+ result.set(key, value);
+ }
+ return result;
+};
+
+
+/**
* Helper: return an iterator over an array.
* @template T
* @param {!Array<T>} arr the array
@@ -193,7 +245,7 @@ jspb.Map.prototype.del = function(key) {
* to help out Angular 1.x users. Still evaluating whether this is the best
* option.
*
- * @return {!Array<K|V>}
+ * @return {!Array<!Array<K|V>>}
*/
jspb.Map.prototype.getEntryList = function() {
var entries = [];