Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#225 refactor: return default object if defined and cache is empty #296

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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