Skip to content

Commit

Permalink
escape regex
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Mar 4, 2024
1 parent 78bfc11 commit 9b8cf09
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
31 changes: 27 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export default class Cookie
/**
* Make a new Cookie instance.
*
* @param {string} namespace
* @return {void}
*/
constructor(namespace = '')
Expand All @@ -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,
Expand All @@ -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 + '=([^;]+)'));

Expand All @@ -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;
}
Expand All @@ -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.

This comment has been minimized.

Copy link
@szepeviktor

szepeviktor Dec 1, 2024

Contributor

@adamlaki Here is a typo.

*
* @param {string} key
* @return {string}
*/
_escape(key)
{
return key = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
}
13 changes: 0 additions & 13 deletions package-lock.json

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@conedevelopment/qkie",
"version": "1.0.3",
"version": "1.1.0",
"description": "Simple cookie management.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 9b8cf09

Please sign in to comment.