From 22c2bbc2a74f7379ed8b03c27584bdeddd48bad1 Mon Sep 17 00:00:00 2001 From: surunzi Date: Thu, 22 Feb 2018 14:27:41 +0800 Subject: [PATCH] Add: rmCookie util --- doc/UTIL_API.md | 12 +++++++ src/Resources/Resources.js | 48 ++----------------------- src/lib/util.js | 72 ++++++++++++++++++++++++++++++++++++++ test/util.js | 2 +- 4 files changed, 88 insertions(+), 46 deletions(-) diff --git a/doc/UTIL_API.md b/doc/UTIL_API.md index 055fc31..6d830cf 100644 --- a/doc/UTIL_API.md +++ b/doc/UTIL_API.md @@ -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. diff --git a/src/Resources/Resources.js b/src/Resources/Resources.js index 5a81203..82ca33b 100644 --- a/src/Resources/Resources.js +++ b/src/Resources/Resources.js @@ -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 ''; diff --git a/src/lib/util.js b/src/lib/util.js index e8645ba..a2533b1 100644 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -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 () diff --git a/test/util.js b/test/util.js index 89b2c32..5f0c454 100644 --- a/test/util.js +++ b/test/util.js @@ -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