Files
zgty-mas-m/nx/helper/parseSafeArgs.js
2026-01-30 21:01:30 +08:00

28 lines
761 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export default function parseSafeArgs(argsStr) {
if (!argsStr.trim()) return []
// 将 ', ' 或 '(' 后的单引号字符串转换为合法 JSON 字符串
// 思路:匹配所有 '...' 并替换为 "..."
let jsonLike =
'[' +
argsStr
.replace(/\s*,\s*/g, ',') // 去掉参数间空格
.replace(/'/g, '"') + // 单引号 → 双引号
']'
try {
const parsed = JSON.parse(jsonLike)
// 确保数字不被转成字符串JSON 会自动处理)
return parsed.map(item => {
if (typeof item === 'string') {
// 如果是 '<' 或 '>',保留字符串
return item
}
return item // number
})
} catch (e) {
console.warn('Failed to parse args:', argsStr, e)
return []
}
}