mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-15 16:06:41 +03:00
7544670ee5
The new eslint found many style errors which have now been corrected. It also complains a lot about atomic update issues that do not seem to exist, so that rule has been disabled. This upgrade will allow us to adopt eslint's "id-denylist" instead of the older "id-blacklist" rule, the name of which violates new Google guidelines about respectful language. Bug: 178203011 Change-Id: Ia65581b96e4dd1331f720fa396183dca020b9caf
93 lines
1.7 KiB
JavaScript
93 lines
1.7 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) {
|
|
// eslint-disable-next-line no-prototype-builtins
|
|
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]);
|
|
}
|
|
}
|
|
};
|