From 9b8cf09cc633c83f98ef6b6ae7894e7f6468bace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Mon, 4 Mar 2024 08:28:48 +0100 Subject: [PATCH] escape regex --- index.js | 31 +++++++++++++++++++++++++++---- package-lock.json | 13 ------------- package.json | 2 +- 3 files changed, 28 insertions(+), 18 deletions(-) delete mode 100644 package-lock.json 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": {