mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
53 lines
1.1 KiB
JavaScript
53 lines
1.1 KiB
JavaScript
import MapCache from './MapCache.js'
|
|
|
|
/** Used to stand-in for `undefined` hash values. */
|
|
const HASH_UNDEFINED = '__lodash_hash_undefined__'
|
|
|
|
class SetCache {
|
|
|
|
/**
|
|
* Creates an array cache object to store unique values.
|
|
*
|
|
* @private
|
|
* @constructor
|
|
* @param {Array} [values] The values to cache.
|
|
*/
|
|
constructor(values) {
|
|
let index = -1
|
|
const length = values == null ? 0 : values.length
|
|
|
|
this.__data__ = new MapCache
|
|
while (++index < length) {
|
|
this.add(values[index])
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Adds `value` to the array cache.
|
|
*
|
|
* @memberOf SetCache
|
|
* @alias push
|
|
* @param {*} value The value to cache.
|
|
* @returns {Object} Returns the cache instance.
|
|
*/
|
|
add(value) {
|
|
this.__data__.set(value, HASH_UNDEFINED)
|
|
return this
|
|
}
|
|
|
|
/**
|
|
* Checks if `value` is in the array cache.
|
|
*
|
|
* @memberOf SetCache
|
|
* @param {*} value The value to search for.
|
|
* @returns {boolean} Returns `true` if `value` is found, else `false`.
|
|
*/
|
|
has(value) {
|
|
return this.__data__.has(value)
|
|
}
|
|
}
|
|
|
|
SetCache.prototype.push = SetCache.prototype.add
|
|
|
|
export default SetCache
|