@newdash/newdash
Version:
javascript/typescript utility library
90 lines (89 loc) • 2.67 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// @ts-nocheck
const ListCache_1 = __importDefault(require("./ListCache"));
const MapCache_1 = __importDefault(require("./MapCache"));
/** Used as the size to enable large array optimizations. */
const LARGE_ARRAY_SIZE = 200;
class Stack {
/**
* Creates a stack cache object to store key-value pairs.
*
* @private
* @constructor
* @param {Array} [entries] The key-value pairs to cache.
*/
constructor(entries) {
const data = this.__data__ = new ListCache_1.default(entries);
this.size = data.size;
}
/**
* Removes all key-value entries from the stack.
*
* @memberOf Stack
*/
clear() {
this.__data__ = new ListCache_1.default;
this.size = 0;
}
/**
* Removes `key` and its value from the stack.
*
* @memberOf Stack
* @param {string} key The key of the value to remove.
* @returns {boolean} Returns `true` if the entry was removed, else `false`.
*/
delete(key) {
const data = this.__data__;
const result = data['delete'](key);
this.size = data.size;
return result;
}
/**
* Gets the stack value for `key`.
*
* @memberOf Stack
* @param {string} key The key of the value to get.
* @returns {*} Returns the entry value.
*/
get(key) {
return this.__data__.get(key);
}
/**
* Checks if a stack value for `key` exists.
*
* @memberOf Stack
* @param {string} key The key of the entry to check.
* @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
*/
has(key) {
return this.__data__.has(key);
}
/**
* Sets the stack `key` to `value`.
*
* @memberOf Stack
* @param {string} key The key of the value to set.
* @param {*} value The value to set.
* @returns {Object} Returns the stack cache instance.
*/
set(key, value) {
let data = this.__data__;
if (data instanceof ListCache_1.default) {
const pairs = data.__data__;
if (pairs.length < LARGE_ARRAY_SIZE - 1) {
pairs.push([key, value]);
this.size = ++data.size;
return this;
}
data = this.__data__ = new MapCache_1.default(pairs);
}
data.set(key, value);
this.size = data.size;
return this;
}
}
exports.default = Stack;