Files
shaka-player/lib/util/multi_map.js
T
Joey Parrish f539147d48 fix: Correct license headers in compiled output
This fixes all the license headers in the main library, which corrects
the appearance of the main license in the compiled output.

It seems that the `!` in the header forces the compiler to keep it in
the output.  I believe older compiler releases did this purely based
on `@license`.

Issue #2638

Change-Id: I7f0e918caad10c9af689c9d07672b7fe9be7b2f3
2020-06-09 16:05:09 -07:00

91 lines
1.6 KiB
JavaScript

/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.util.MultiMap');
/**
* @summary A simple multimap template.
* @template T
*/
shaka.util.MultiMap = class {
constructor() {
/** @private {!Object.<string, !Array.<T>>} */
this.map_ = {};
}
/**
* Add a key, value pair to the map.
* @param {string} key
* @param {T} value
*/
push(key, value) {
if (this.map_.hasOwnProperty(key)) {
this.map_[key].push(value);
} else {
this.map_[key] = [value];
}
}
/**
* Get a list of values by key.
* @param {string} key
* @return {Array.<T>} or null if no such key exists.
*/
get(key) {
const list = this.map_[key];
// slice() clones the list so that it and the map can each be modified
// without affecting the other.
return list ? list.slice() : null;
}
/**
* Get a list of all values.
* @return {!Array.<T>}
*/
getAll() {
const list = [];
for (const key in this.map_) {
list.push(...this.map_[key]);
}
return list;
}
/**
* Remove a specific value, if it exists.
* @param {string} key
* @param {T} value
*/
remove(key, value) {
if (!(key in this.map_)) {
return;
}
this.map_[key] = this.map_[key].filter((i) => i != value);
}
/**
* Clear all keys and values from the multimap.
*/
clear() {
this.map_ = {};
}
/**
* @param {function(string, !Array.<T>)} callback
*/
forEach(callback) {
for (const key in this.map_) {
callback(key, this.map_[key]);
}
}
};