wireit
Version:
Upgrade your npm scripts to make them smarter and more efficient
41 lines • 1.15 kB
JavaScript
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
// To prevent using the global console accidentally, we shadow it with
// undefined
const console = undefined;
function markAsUsed(_) { }
markAsUsed(console);
/**
* A map that can also efficiently return the most recently added entry.
*/
export class StackMap extends Map {
#stack = [];
set(key, value) {
if (!this.has(key)) {
this.#stack.push([key, value]);
}
return super.set(key, value);
}
// Surprisingly, we don't need to override delete, because we expect peek()
// to be called frequently, and it will remove any trailing deleted entries.
/**
* Returns the most recently added entry that's still in the map, or
* undefined if the map is empty.
*/
peek() {
while (true) {
const last = this.#stack[this.#stack.length - 1];
if (!last) {
return;
}
if (this.has(last[0])) {
return last;
}
this.#stack.pop();
}
}
}
//# sourceMappingURL=stack-map.js.map