From 5a76e633ea9b5adb215e93fdc11e1c0c08b3fc74 Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Thu, 17 Nov 2016 16:48:38 -0800 Subject: Integrated internal changes from Google --- js/map.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) (limited to 'js/map.js') diff --git a/js/map.js b/js/map.js index 0f8401c4..93f08e03 100644 --- a/js/map.js +++ b/js/map.js @@ -130,6 +130,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>} + */ +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>} 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} + */ +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 @@ -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} + * @return {!Array>} */ jspb.Map.prototype.getEntryList = function() { var entries = []; -- cgit v1.2.3