From 380435d0200782a951cbdde8eb57f41f75bb183c Mon Sep 17 00:00:00 2001 From: Aaron Hamid Date: Thu, 17 Dec 2015 18:54:51 -0500 Subject: [PATCH] Add `clear` method to `MapCache`. --- lodash.js | 12 ++++++++++++ test/test.js | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lodash.js b/lodash.js index 5db2f1147..a0e2be1c4 100644 --- a/lodash.js +++ b/lodash.js @@ -1939,6 +1939,17 @@ return this; } + /** + * Clears the map data + * + * @private + * @name clear + * @memberOf MapCache + */ + function mapClear() { + this.__data__ = { 'hash': new Hash, 'map': Map ? new Map : [], 'string': new Hash }; + } + /*------------------------------------------------------------------------*/ /** @@ -13737,6 +13748,7 @@ Hash.prototype = nativeCreate ? nativeCreate(null) : objectProto; // Add functions to the `MapCache`. + MapCache.prototype.clear = mapClear; MapCache.prototype['delete'] = mapDelete; MapCache.prototype.get = mapGet; MapCache.prototype.has = mapHas; diff --git a/test/test.js b/test/test.js index 4a70cbecf..93999ad1c 100644 --- a/test/test.js +++ b/test/test.js @@ -12485,6 +12485,28 @@ _.memoize.Cache = oldCache; }); + + QUnit.test('should allow clearing cache', function(assert) { + assert.expect(5); + + var memoized = _.memoize(_.identity); + + assert.strictEqual(memoized.cache.has('a'), false); + + memoized('a'); + + assert.strictEqual(memoized.cache.has('a'), true); + + memoized.cache.clear(); + + assert.strictEqual(memoized.cache.has('a'), false); + + memoized('a'); + memoized('b'); + + assert.strictEqual(memoized.cache.has('a'), true); + assert.strictEqual(memoized.cache.has('b'), true); + }); }()); /*--------------------------------------------------------------------------*/