Skip to content

Commit

Permalink
perf(objects): 重写 objectFilterByKeys 方法,支持正则方式过滤key
Browse files Browse the repository at this point in the history
  • Loading branch information
renxia committed Jan 15, 2024
1 parent eddf027 commit 6a3371a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
23 changes: 12 additions & 11 deletions src/common/cookie.ts
Original file line number Diff line number Diff line change
@@ -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<string, string> = {};
if (!cookie && typeof document !== 'undefined') cookie = document.cookie;
Expand All @@ -26,16 +32,11 @@ export function cookieParse(cookie = '', filterNilValue = false) {
}

export function cookieStringfiy(
cookieObj: Record<string, string | number | boolean>,
options: { removeKeys?: string[]; onlyKeys?: string[]; removeNil?: boolean } = {}
cookieObj: Record<string, string | number | boolean | undefined>,
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('; ');
}
14 changes: 7 additions & 7 deletions src/common/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,17 @@ export function toLowcaseKeyObject<T extends Record<string, unknown>>(info: T =
}

/** 过滤指定的key或空值,返回新的对象 */
export function getFilteredObject(
obj: Record<string, string | number | boolean>,
options: { removeKeys?: string[]; onlyKeys?: string[]; removeNil?: boolean } = {}
export function objectFilterByKeys<T extends Record<string, unknown>>(
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<string, any> = {};
for (const k of keys) newObj[k] = obj[k];
return newObj;
return newObj as Partial<typeof obj>;
}

0 comments on commit 6a3371a

Please sign in to comment.