From ee1cf35bc99687d8e2eec32ac4a81d32e5b19145 Mon Sep 17 00:00:00 2001 From: Klaus Hartl Date: Mon, 16 Sep 2019 12:06:06 +0200 Subject: [PATCH] Expose default path via api So far we had been applying the default `path: '/'` as sort of a "hidden" default, and it wasn't exposed in the defaults object accessible as `Cookies.defaults`. This is rather unexpected and we start exposing the path as part of the defaults to avoid surprises. This change will also make it slightly more intuitive to change the behavior to a browser's default behavior where a cookie is only valid at the current page: instead of having to specify `path: ''` in the defaults we can now do `delete Cookies.defaults.path`. The former is rather obfuscating the intent, and a leaking abstraction. Fixes #541 --- src/js.cookie.mjs | 13 ++++--------- test/tests.js | 9 ++++----- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/js.cookie.mjs b/src/js.cookie.mjs index cd93d315..071aaf21 100644 --- a/src/js.cookie.mjs +++ b/src/js.cookie.mjs @@ -15,20 +15,15 @@ function decode (s) { } function init (converter) { - var defaults = {} + var defaults = { + path: '/' + } function set (key, value, attributes) { if (typeof document === 'undefined') { return } - - attributes = extend( - { - path: '/' - }, - defaults, - attributes - ) + attributes = extend(defaults, attributes) if (typeof attributes.expires === 'number') { attributes.expires = new Date(new Date() * 1 + attributes.expires * 864e5) diff --git a/test/tests.js b/test/tests.js index b8b5fadd..e623809f 100644 --- a/test/tests.js +++ b/test/tests.js @@ -318,7 +318,7 @@ QUnit.test('API for changing defaults', function (assert) { Cookies.set('c', 'v').match(/path=\/foo/), 'should use attributes from defaults' ) - Cookies.remove('c', { path: '/foo' }) + Cookies.remove('c') assert.ok( Cookies.set('c', 'v', { path: '/bar' }).match(/path=\/bar/), @@ -327,10 +327,9 @@ QUnit.test('API for changing defaults', function (assert) { Cookies.remove('c', { path: '/bar' }) delete Cookies.defaults.path - assert.ok( - Cookies.set('c', 'v').match(/path=\//), - 'should roll back to the default path' - ) + assert.notOk(Cookies.set('c', 'v').match(/path=/), 'should not set any path') + Cookies.remove('c') + Cookies.defaults.path = '/' }) QUnit.test('true secure value', function (assert) {