alapa
Version:
A cutting-edge web development framework designed to revolutionize the way developers build modern web applications.
91 lines (90 loc) • 2.45 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.Map = void 0;
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* A generic map class that maps keys of type K to values of type V.
*
* @template K - The type of keys (string, number, or symbol).
* @template V - The type of values.
*/
class Map {
items = {};
/**
* Creates a new Map with optional initial key-value pairs.
*
* @param initialItems - An optional object of initial key-value pairs.
*/
constructor(initialItems) {
if (initialItems) {
this.items = { ...initialItems };
}
}
/**
* Adds a key-value pair to the map.
*
* @param key - The key to add.
* @param value - The value associated with the key.
*/
add(key, value) {
this.items[key] = value;
}
/**
* Retrieves the value associated with the given key.
*
* @param key - The key whose value to retrieve.
* @returns The value associated with the key, or undefined if the key does not exist.
*/
get(key) {
return this.items[key];
}
/**
* Removes a key-value pair from the map.
*
* @param key - The key to remove.
* @returns True if the key was removed, false if the key was not found.
*/
remove(key) {
if (key in this.items) {
delete this.items[key];
return true;
}
return false;
}
/**
* Retrieves all keys in the map.
*
* @returns An array of keys.
*/
keys() {
return Object.keys(this.items);
}
/**
* Retrieves all values in the map.
*
* @returns An array of values.
*/
values() {
return Object.values(this.items);
}
/**
* Retrieves all key-value pairs in the map as an array of tuples.
*
* @returns An array of tuples where each tuple contains a key and its corresponding value.
*/
getAll() {
return Object.entries(this.items);
}
/**
* Merges another map into this map.
*
* @param otherMap - The map or object to merge into this map.
*/
addAll(otherMap) {
const entries = otherMap instanceof Map ? otherMap.getAll() : Object.entries(otherMap);
for (const [key, value] of entries) {
this.add(key, value); // Use `add` to ensure type safety
}
}
}
exports.Map = Map;
;