From 5c076614470faeeb114adb926440ce4d9813a287 Mon Sep 17 00:00:00 2001 From: Simon He <57086651+Simon-He95@users.noreply.github.com> Date: Fri, 26 Jan 2024 23:14:25 +0800 Subject: [PATCH] refactor: eval to new Function --- src/array/quickFilter.ts | 10 +++--- src/string/useJSONParse.ts | 46 ++++----------------------- test/__snapshots__/basic.test.ts.snap | 2 -- test/date/formateDate.test.ts | 2 +- 4 files changed, 12 insertions(+), 48 deletions(-) diff --git a/src/array/quickFilter.ts b/src/array/quickFilter.ts index 821535d..3868feb 100644 --- a/src/array/quickFilter.ts +++ b/src/array/quickFilter.ts @@ -11,17 +11,17 @@ import { isDef } from '../is/isDef' export function quickFilter(array: any[], key: string | Array) { const reg = /\/[\w._ $]+\/[gims]*/ return array.filter((item) => { - if (isArray(key)) - return key.some(k => findItem(item, k)) + if (isArray(key)) return key.some((k) => findItem(item, k)) else return findItem(item, key) }) function findItem(item: Record, key: string): boolean { const [_key, _value] = key.split('=') - if (isUndef(item[_key])) - return false + if (isUndef(item[_key])) return false return isDef(_value) ? reg.test(_value) - ? new RegExp(eval(_value)).test(item[_key]) + ? new RegExp(eval(new Function('return (' + _value + ')')())).test( + item[_key], + ) : _value === item[_key] : /.*/.test(item[key]) } diff --git a/src/string/useJSONParse.ts b/src/string/useJSONParse.ts index e394a83..426dc71 100644 --- a/src/string/useJSONParse.ts +++ b/src/string/useJSONParse.ts @@ -9,47 +9,13 @@ const moreCommaReg = /,(\s*})/gm * @returns */ export function useJSONParse(str: string) { - try { - return JSON.parse( - str - .replace(keyReg, (match, key) => match.replace(key, `"${key}"`)) - .replace(valueReg, ': "$1"') - .replace(commaLackReg, (match, value) => - match.replace(value, `${value},`), - ) - .replace(commaMoreReg, (match) => match.replace(',', '')) - .replace(moreCommaReg, (_, v) => v), - ) - } catch (_) { - str = str - .trim() - .replace(/(\/\*[\s\S]*?\*\/|\/\/.*)/g, '') - .replace(/,\n\s*}/g, '\n }') - if (str.endsWith(';')) str = str.slice(0, -1) + // 将字符串转换为对象 + let obj = new Function('return (' + str + ')')() - if (/^{/.test(str)) - return str - .slice(1, -1) - .replace(/\n+/g, '\n') - .replaceAll('\t', '') - .replaceAll('\r', '') - .replace(/\:\s*{([^\}]*)}/g, (_, v) => { - return _.replace(v, v.replace(/\n/g, '')) - }) - .split(',\n') - .reduce((result, item: string) => { - item = item.trim() - if (!item) return result - const items = item.split(':') as string[] - const [key, val] = [items[0], items.slice(1).join(':')] - const newVal = val.replace(/\n/g, '').replace(/\s+/g, ' ').trim() - result[key.trim()] = newVal.endsWith(',') - ? newVal.slice(0, -1) - : newVal - return result - }, {} as any) - return str - } + // 如果对象是正则表达式字符串,则将其转换为正则表达式对象 + if (typeof obj === 'string' && obj.startsWith('re')) + obj = new Function('return ' + obj.slice(2))() + return obj } // const data = `[{"age":"14","fn": ()=>"123"}]` diff --git a/test/__snapshots__/basic.test.ts.snap b/test/__snapshots__/basic.test.ts.snap index ddd69d1..e532670 100644 --- a/test/__snapshots__/basic.test.ts.snap +++ b/test/__snapshots__/basic.test.ts.snap @@ -1,5 +1,3 @@ // Vitest Snapshot v1 -exports[`Test htmlTransform > htmlTransform test 1`] = `"hi

hello

你好"`; - exports[`test htmlTransform > htmlTransform test 1`] = `"hi

hello

你好"`; diff --git a/test/date/formateDate.test.ts b/test/date/formateDate.test.ts index 73e7aa9..049b5a3 100644 --- a/test/date/formateDate.test.ts +++ b/test/date/formateDate.test.ts @@ -4,6 +4,6 @@ import { formateDate } from '../../src/date' describe('formateDate test', () => { it('test', () => { const d = formateDate(new Date(), 'yyyy-mm-dd') - expect(d).toMatchInlineSnapshot('"2023-05-30"') + expect(d).toMatchInlineSnapshot('"2024-09-26"') }) })