Add: rmCookie util

This commit is contained in:
surunzi
2018-02-22 14:27:41 +08:00
parent 8e79b97938
commit 22c2bbc2a7
4 changed files with 88 additions and 46 deletions

View File

@@ -1967,6 +1967,18 @@ var paramArr = _.restArgs(function (rest) { return rest });
paramArr(1, 2, 3, 4); // -> [1, 2, 3, 4]
```
## rmCookie
Loop through all possible path and domain to remove cookie.
|Name|Type |Desc |
|----|------|----------|
|key |string|Cookie key|
```javascript
rmCookie('test');
```
## root
Root object reference, `global` in nodeJs, `window` in browser.

View File

@@ -16,7 +16,7 @@ import {
isErudaEl,
toArr,
concat,
cookie
rmCookie
} from '../lib/util';
export default class Resources extends Tool
@@ -269,7 +269,7 @@ export default class Resources extends Tool
{
let key = $(this).data('key');
delCookie(key);
rmCookie(key);
self.refreshCookie()._render();
})
.on('click', '.eruda-clear-storage', function ()
@@ -288,7 +288,7 @@ export default class Resources extends Tool
})
.on('click', '.eruda-clear-cookie', () =>
{
each(this._cookieData, val => delCookie(val.key));
each(this._cookieData, val => rmCookie(val.key));
this.refreshCookie()._render();
})
.on('click', '.eruda-storage-val', function ()
@@ -491,48 +491,6 @@ function getState(type, len)
return 'eruda-ok';
}
let {hostname, pathname} = window.location;
function delCookie(key)
{
let hostNames = hostname.split('.'),
pathNames = pathname.split('/'),
domain = '',
pathLen = pathNames.length,
path;
if (del()) return;
for (let i = hostNames.length - 1; i >= 0; i--)
{
let hostName = hostNames[i];
if (hostName === '') continue;
domain = (domain === '') ? hostName : hostName + '.' + domain ;
path = '/';
if (del({domain, path}) || del({domain})) return;
for (let j = 0; j < pathLen; j++)
{
let pathName = pathNames[j];
if (pathName === '') continue;
path += pathName;
if (del({domain, path}) || del({path})) return;
path += '/';
if (del({domain, path}) || del({path})) return;
}
}
function del(options = {})
{
cookie.remove(key, options);
return !cookie.get(key);
}
}
function getLowerCaseTagName(el)
{
if (!el.tagName) return '';

View File

@@ -5973,6 +5973,78 @@ export var detectBrowser = _.detectBrowser = (function ()
return exports;
})();
/* ------------------------------ rmCookie ------------------------------ */
export var rmCookie = _.rmCookie = (function ()
{
/* Loop through all possible path and domain to remove cookie.
*
* |Name|Type |Desc |
* |----|------|----------|
* |key |string|Cookie key|
*
* ```javascript
* rmCookie('test');
* ```
*/
/* module
* env: browser
* test: browser
*/
/* dependencies
* cookie
*/
function exports(key)
{
var location = window.location,
hostname = location.hostname,
pathname = location.pathname,
hostNames = hostname.split('.'),
pathNames = pathname.split('/'),
domain = '',
pathLen = pathNames.length,
path;
if (del()) return;
for (var i = hostNames.length - 1; i >= 0; i--)
{
var hostName = hostNames[i];
if (hostName === '') continue;
domain = (domain === '') ? hostName : hostName + '.' + domain ;
path = '/';
if (del({domain: domain, path: path}) || del({domain: domain})) return;
for (var j = 0; j < pathLen; j++)
{
var pathName = pathNames[j];
if (pathName === '') continue;
path += pathName;
if (del({domain: domain, path: path}) || del({path: path})) return;
path += '/';
if (del({domain: domain, path: path}) || del({path: path})) return;
}
}
function del(options)
{
options = options || {};
cookie.remove(key, options);
return !cookie.get(key);
}
}
return exports;
})();
/* ------------------------------ rtrim ------------------------------ */
export var rtrim = _.rtrim = (function ()

View File

@@ -784,7 +784,7 @@
*
* |Name |Type |Desc |
* |------|-------|----------------------------------|
* |val |* |The value to check |
* |val |* |Value to check |
* |return|boolean|True if value is an `Array` object|
*
* ```javascript