diff --git a/index.js b/index.js index c4a84ef..99f0331 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ export default class Cookie /** * Make a new Cookie instance. * + * @param {string} namespace * @return {void} */ constructor(namespace = '') @@ -22,7 +23,7 @@ export default class Cookie */ set(key, value, expires = null, path = '/', options = {}) { - key = this.namespace + key; + key = this._qualify(key); const pairs = Object.assign({ [key]: value, @@ -44,7 +45,7 @@ export default class Cookie */ get(key, value = null) { - key = this.namespace + key; + key = this._escape(this._qualify(key)); const cookie = document.cookie.match(new RegExp('(^| )' + key + '=([^;]+)')); @@ -59,7 +60,7 @@ export default class Cookie */ isset(key) { - key = this.namespace + key; + key = this._escape(this._qualify(key)); return document.cookie.match(new RegExp('(^| )' + key + '=([^;]+)')) !== null; } @@ -72,8 +73,30 @@ export default class Cookie */ remove(key) { - key = this.namespace + key; + key = this._qualify(key); this.set(key, null, 'Thu, 01 Jan 1970 00:00:01 GMT'); } + + /** + * Qualify the given key. + * + * @param {string} key + * @return {string} + */ + _qualify(key) + { + return this.namespace + key; + } + + /** + * Esacpe the given key. + * + * @param {string} key + * @return {string} + */ + _escape(key) + { + return key = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + } } diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index e725d01..0000000 --- a/package-lock.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "@conedevelopment/qkie", - "version": "1.0.3", - "lockfileVersion": 3, - "requires": true, - "packages": { - "": { - "name": "@conedevelopment/qkie", - "version": "1.0.3", - "license": "MIT" - } - } -} diff --git a/package.json b/package.json index 90bcb8e..5adb10f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@conedevelopment/qkie", - "version": "1.0.3", + "version": "1.1.0", "description": "Simple cookie management.", "main": "index.js", "scripts": {