Skip to content

Commit

Permalink
refactor: eval to new Function
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-He95 committed Jan 26, 2024
1 parent 4ebb8a9 commit 5c07661
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 48 deletions.
10 changes: 5 additions & 5 deletions src/array/quickFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import { isDef } from '../is/isDef'
export function quickFilter(array: any[], key: string | Array<string>) {
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<any, any>, 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])
}
Expand Down
46 changes: 6 additions & 40 deletions src/string/useJSONParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"}]`
Expand Down
2 changes: 0 additions & 2 deletions test/__snapshots__/basic.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Vitest Snapshot v1

exports[`Test htmlTransform > htmlTransform test 1`] = `"<span>hi</span><p class=\\"_ee\\" age=\\"19\\">hello</p><span>你好</span><view onTap=\\"xx\\"></view>"`;
exports[`test htmlTransform > htmlTransform test 1`] = `"<span>hi</span><p class=\\"_ee\\" age=\\"19\\">hello</p><span>你好</span><view onTap=\\"xx\\"></view>"`;
2 changes: 1 addition & 1 deletion test/date/formateDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"')
})
})

0 comments on commit 5c07661

Please sign in to comment.