Skip to content

Commit

Permalink
node-cache#225 refactor: return default object if defined and cache i…
Browse files Browse the repository at this point in the history
…s empty
  • Loading branch information
thepylot committed Sep 28, 2022
1 parent 1e5c75e commit 7b5cc1e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions _src/lib/node_cache.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module.exports = class NodeCache extends EventEmitter
#
# myCache.get "myKey", ( err, val )
#
get: ( key )=>
get: ( key, defaultObj = null )=>
# handle invalid key types
if (err = @_isInvalidKey( key ))?
throw err
Expand All @@ -101,9 +101,9 @@ module.exports = class NodeCache extends EventEmitter
# return data
return _ret
else
# if not found return undefined
# if not found return defaultObj otherwise undefined
@stats.misses++
return undefined
return defaultObj || undefined

# ## mget
#
Expand Down
14 changes: 14 additions & 0 deletions _src/test/mocha_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1326,5 +1326,19 @@ describe "`#{pkg.name}@#{pkg.version}` on `node@#{process.version}`", () ->
return
)

describe("#255 - get with default value", () ->
cache = null
before(() ->
cache = new nodeCache()
return
)

it("should return default value if cache is empty and default object defined", () ->
should(cache.get("test", defaultObj={test: true})).eql({test: true})
return
)
return
)

return
return

0 comments on commit 7b5cc1e

Please sign in to comment.