Skip to content

Commit

Permalink
Handle falsy arguments passed to getters
Browse files Browse the repository at this point in the history
In case `null` or `undefinded` is passed to one of the getter methods
we shouldn't treat it as if it was an attempt to get all cookies.

Fixes #399
  • Loading branch information
carhartl committed Sep 14, 2019
1 parent acc4954 commit 899118b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/js.cookie.mjs
Expand Up @@ -123,9 +123,15 @@ function init (converter) {

api.set = set
api.get = function (key) {
return get(key, false /* read as raw */)
if (arguments.length && !key) {
return
}
return get(key /* read as raw */)
}
api.getJSON = function (key) {
if (arguments.length && !key) {
return
}
return get(key, true /* read as json */)
}
api.remove = function (key, attributes) {
Expand Down
14 changes: 14 additions & 0 deletions test/tests.js
Expand Up @@ -106,6 +106,20 @@ QUnit.test('Call to read cookie when there is a window.json variable globally',
delete window.json
})

QUnit.test('Passing `undefined` first argument', function (assert) {
assert.expect(2)
Cookies.set('foo', 'bar')
assert.strictEqual(Cookies.get(undefined), undefined, 'should not attempt to retrieve all cookies')
assert.strictEqual(Cookies.getJSON(undefined), undefined, 'should not attempt to retrieve all JSON cookies')
})

QUnit.test('Passing `null` first argument', function (assert) {
assert.expect(2)
Cookies.set('foo', 'bar')
assert.strictEqual(Cookies.get(null), undefined, 'should not attempt to retrieve all cookies')
assert.strictEqual(Cookies.getJSON(null), undefined, 'should not attempt to retrieve all JSON cookies')
})

QUnit.module('write', lifecycle)

QUnit.test('String primitive', function (assert) {
Expand Down

0 comments on commit 899118b

Please sign in to comment.