From 6a3371a5368e36b59b34f078ee81263f5f907887 Mon Sep 17 00:00:00 2001 From: renxia Date: Mon, 15 Jan 2024 12:00:34 +0800 Subject: [PATCH] =?UTF-8?q?perf(objects):=20=E9=87=8D=E5=86=99=20objectFil?= =?UTF-8?q?terByKeys=20=E6=96=B9=E6=B3=95=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=AD=A3=E5=88=99=E6=96=B9=E5=BC=8F=E8=BF=87=E6=BB=A4key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/cookie.ts | 23 ++++++++++++----------- src/common/objects.ts | 14 +++++++------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/common/cookie.ts b/src/common/cookie.ts index 81ebc4b..1b8d586 100644 --- a/src/common/cookie.ts +++ b/src/common/cookie.ts @@ -1,7 +1,13 @@ /* - * cookie 相关处理工具方法 + * @Author: renxia + * @Date: 2024-01-15 11:26:52 + * @LastEditors: renxia + * @LastEditTime: 2024-01-15 11:53:38 + * @Description: cookie 相关处理工具方法 */ +import { objectFilterByKeys } from './objects'; + export function cookieParse(cookie = '', filterNilValue = false) { const obj: Record = {}; if (!cookie && typeof document !== 'undefined') cookie = document.cookie; @@ -26,16 +32,11 @@ export function cookieParse(cookie = '', filterNilValue = false) { } export function cookieStringfiy( - cookieObj: Record, - options: { removeKeys?: string[]; onlyKeys?: string[]; removeNil?: boolean } = {} + cookieObj: Record, + options: { removeKeys?: (string | RegExp)[]; onlyKeys?: (string | RegExp)[]; removeNil?: boolean } = {} ) { - return Object.keys(cookieObj) - .filter(key => { - if (options.removeKeys?.includes(key)) return false; - if (options.onlyKeys?.includes(key)) return false; - if (options.removeNil && (cookieObj[key] == null || cookieObj[key] === '')) return false; - return true; - }) - .map(key => `${key}=${cookieObj[key] ? encodeURIComponent(cookieObj[key]) : ''}`) + cookieObj = objectFilterByKeys(cookieObj, options); + return Object.values(cookieObj) + .map((key, value) => `${key}=${value ? encodeURIComponent(value) : ''}`) .join('; '); } diff --git a/src/common/objects.ts b/src/common/objects.ts index 3729b55..ed14efa 100644 --- a/src/common/objects.ts +++ b/src/common/objects.ts @@ -204,17 +204,17 @@ export function toLowcaseKeyObject>(info: T = } /** 过滤指定的key或空值,返回新的对象 */ -export function getFilteredObject( - obj: Record, - options: { removeKeys?: string[]; onlyKeys?: string[]; removeNil?: boolean } = {} +export function objectFilterByKeys>( + obj: T, + options: { removeKeys?: (string | RegExp)[]; onlyKeys?: (string | RegExp)[]; removeNil?: boolean } = {} ) { const keys = Object.keys(obj).filter(key => { - if (options.removeKeys?.includes(key)) return false; - if (options.onlyKeys?.includes(key)) return false; + if (options.removeKeys?.some(d => (d instanceof RegExp ? d.test(key) : d === key))) return false; + if (!options.onlyKeys?.some(d => (d instanceof RegExp ? d.test(key) : d === key))) return false; if (options.removeNil && (obj[key] == null || obj[key] === '')) return false; return true; }); - const newObj = {} as typeof obj; + const newObj: Record = {}; for (const k of keys) newObj[k] = obj[k]; - return newObj; + return newObj as Partial; }