Path: blob/trunk/third_party/closure/goog/collections/maps.js
4062 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Helper methods that operate on Map-like objects (e.g. ES68* Maps).9*/1011goog.module('goog.collections.maps');12goog.module.declareLegacyNamespace();1314/**15* A MapLike implements the same public interface as an ES6 Map, without tying16* the underlying code directly to the implementation. Any additions to this17* type should also be present on ES6 Maps.18* @template K,V19* @record20*/21class MapLike {22constructor() {23/** @const {number} The number of items in this map. */24this.size;25}2627/**28* @param {K} key The key to set in the map.29* @param {V} val The value to set for the given key in the map.30*/31set(key, val) {};3233/**34* @param {K} key The key to retrieve from the map.35* @return {V|undefined} The value for this key, or undefined if the key is36* not present in the map.37*/38get(key) {};3940/**41* @return {!IteratorIterable<K>} An ES6 Iterator that iterates over the keys42* in the map.43*/44keys() {};4546/**47* @return {!IteratorIterable<V>} An ES6 Iterator that iterates over the48* values in the map.49*/50values() {};5152/**53* @param {K} key The key to check.54* @return {boolean} True iff this key is present in the map.55*/56has(key) {};57}58exports.MapLike = MapLike;5960/**61* Iterates over each entry in the given entries and sets the entry in62* the map, overwriting any existing entries for the key.63* @param {!MapLike<K,V>} map The map to set entries on.64* @param {?Iterable<!Array<K|V>>} entries The iterable of entries. This65* iterable should really be of type Iterable<Array<[K,V]>>, but the tuple66* type is not representable in the Closure Type System.67* @template K,V68*/69function setAll(map, entries) {70if (!entries) return;71for (const [k, v] of entries) {72map.set(k, v);73}74}75exports.setAll = setAll;7677/**78* Determines if a given map contains the given value, optionally using79* a custom comparison function.80* @param {!MapLike<?,V1>} map The map whose values to check.81* @param {V2} val The value to check for.82* @param {(function(V1,V2): boolean)=} valueEqualityFn The comparison function83* used to determine if the given value is equivalent to any of the values84* in the map. If no function is provided, defaults to strict equality85* (===).86* @return {boolean} True iff the given map contains the given value according87* to the comparison function.88* @template V1,V289*/90function hasValue(map, val, valueEqualityFn = defaultEqualityFn) {91for (const v of map.values()) {92if (valueEqualityFn(v, val)) return true;93}94return false;95}96exports.hasValue = hasValue;9798/** @const {function(?,?): boolean} */99const defaultEqualityFn = (a, b) => a === b;100101/**102* Compares two maps using their public APIs to determine if they have103* equal contents, optionally using a custom comparison function when comaring104* values.105* @param {!MapLike<K,V1>} map The first map106* @param {!MapLike<K,V2>} otherMap The other map107* @param {(function(V1,V2): boolean)=} valueEqualityFn The comparison function108* used to determine if the values obtained from each map are equivalent. If109* no function is provided, defaults to strict equality (===).110* @return {boolean}111* @template K,V1,V2112*/113function equals(map, otherMap, valueEqualityFn = defaultEqualityFn) {114if (map === otherMap) return true;115if (map.size !== otherMap.size) return false;116for (const key of map.keys()) {117if (!otherMap.has(key)) return false;118if (!valueEqualityFn(map.get(key), otherMap.get(key))) return false;119}120return true;121}122exports.equals = equals;123124/**125* Returns a new ES6 Map in which all the keys and values from the126* given map are interchanged (keys become values and values become keys). If127* multiple keys in the given map to the same value, the resulting value in the128* transposed map is implementation-dependent.129*130* It acts very similarly to {goog.object.transpose(Object)}.131* @param {!MapLike<K,V>} map The map to transpose.132* @return {!Map<V,K>} A transposed version of the given map.133* @template K,V134*/135function transpose(map) {136const /** !Map<V,K> */ transposed = new Map();137for (const key of map.keys()) {138const val = map.get(key);139transposed.set(val, key);140}141return transposed;142}143exports.transpose = transpose;144145/**146* ToObject returns a new object whose properties are the keys from the Map.147* @param {!MapLike<K,V>} map The map to convert into an object.148* @return {!Object<K,V>} An object representation of the Map.149* @template K,V150*/151function toObject(map) {152const /** !Object<K,V> */ obj = {};153for (const key of map.keys()) {154obj[key] = map.get(key);155}156return obj;157}158exports.toObject = toObject;159160161