mutation-summary
Version:
Makes observing the DOM fast and easy
82 lines • 2.45 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeMap = void 0;
/**
* A helper class that maps from a DOM Node to an arbitrary value.
*/
var NodeMap = /** @class */ (function () {
/**
* Constructs a new and empty NodeMap.
*/
function NodeMap() {
this._nodes = [];
this._values = [];
}
NodeMap._isIndex = function (s) {
return +s === s >>> 0;
};
NodeMap._nodeId = function (node) {
var id = node[NodeMap._ID_PROP];
if (!id)
id = node[NodeMap._ID_PROP] = NodeMap._NEXT_ID++;
return id;
};
/**
* Sets the value of a node within the map.
* @param node The node to set the value for.
* @param value the value to associate with the node.
*/
NodeMap.prototype.set = function (node, value) {
var id = NodeMap._nodeId(node);
this._nodes[id] = node;
this._values[id] = value;
};
/**
* Gets the value for the given node.
*
* @param node The node to get the value of.
* @returns The value for the given node, or undefined if the node is not
* present in the map.
*/
NodeMap.prototype.get = function (node) {
var id = NodeMap._nodeId(node);
return id !== undefined ? this._values[id] : undefined;
};
/**
* Determines if a given node is in the NodeMap.
*
* @param node The node to determine if it is in the map.
*
* @returns true if the Node is contained in the map, false otherwise.
*/
NodeMap.prototype.has = function (node) {
return NodeMap._nodeId(node) in this._nodes;
};
/**
* Deletes a node from the NodeMap.
*
* @param node The node to delete.
*/
NodeMap.prototype.delete = function (node) {
var id = NodeMap._nodeId(node);
delete this._nodes[id];
this._values[id] = undefined;
};
/**
* @returns an array that holds the nodes that are the keys of the map.
*/
NodeMap.prototype.keys = function () {
var nodes = [];
for (var id in this._nodes) {
if (!NodeMap._isIndex(id))
continue;
nodes.push(this._nodes[id]);
}
return nodes;
};
NodeMap._ID_PROP = '__mutation_summary_node_map_id__';
NodeMap._NEXT_ID = 1;
return NodeMap;
}());
exports.NodeMap = NodeMap;
//# sourceMappingURL=NodeMap.js.map
;