Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-He95 committed Dec 6, 2023
1 parent fb1284f commit c1a3ec6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 152 deletions.
176 changes: 24 additions & 152 deletions src/string/useJSONParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,160 +26,32 @@ export function useJSONParse(str: string) {
.replace(/(\/\*[\s\S]*?\*\/|\/\/.*)/g, '')
.replace(/,\n\s*}/g, '\n }')
if (str.endsWith(';')) str = str.slice(0, -1)

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
.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)
}
}

const data = `{
clickToRowSelect: { type: Boolean, default: true },
isTreeTable: Boolean,
tableSetting: propTypes.shape<TableSetting>({}),
inset: Boolean,
sortFn: {
type: Function as PropType<(sortInfo: SorterResult) => any>,
default: DEFAULT_SORT_FN,
},
filterFn: {
type: Function as PropType<(data: Partial<Recordable<string[]>>) => any>,
default: DEFAULT_FILTER_FN,
},
showTableSetting: Boolean,
autoCreateKey: { type: Boolean, default: true },
striped: { type: Boolean, default: true },
showSummary: Boolean,
summaryFunc: {
type: [Function, Array] as PropType<(...arg: any[]) => any[]>,
default: null,
},
summaryData: {
type: Array as PropType<Recordable[]>,
default: null,
},
indentSize: propTypes.number.def(24),
canColDrag: { type: Boolean, default: true },
api: {
type: Function as PropType<(...arg: any[]) => Promise<any>>,
default: null,
},
beforeFetch: {
type: Function as PropType<Fn>,
default: null,
},
afterFetch: {
type: Function as PropType<Fn>,
default: null,
},
handleSearchInfoFn: {
type: Function as PropType<Fn>,
default: null,
},
fetchSetting: {
type: Object as PropType<FetchSetting>,
default: () => {
return FETCH_SETTING;
},
},
immediate: { type: Boolean, default: true },
emptyDataIsShowTable: { type: Boolean, default: true },
// 额外的请求参数
searchInfo: {
type: Object as PropType<Recordable>,
default: null,
},
// 默认的排序参数
defSort: {
type: Object as PropType<Recordable>,
default: null,
},
// 使用搜索表单
useSearchForm: propTypes.bool,
// 表单配置
formConfig: {
type: Object as PropType<Partial<FormProps>>,
default: null,
},
columns: {
type: Array as PropType<BasicColumn[]>,
default: () => [],
},
showIndexColumn: { type: Boolean, default: true },
indexColumnProps: {
type: Object as PropType<BasicColumn>,
default: null,
},
actionColumn: {
type: Object as PropType<BasicColumn>,
default: null,
},
ellipsis: { type: Boolean, default: true },
isCanResizeParent: { type: Boolean, default: false },
canResize: { type: Boolean, default: true },
clearSelectOnPageChange: propTypes.bool,
resizeHeightOffset: propTypes.number.def(0),
rowSelection: {
type: Object as PropType<TableRowSelection | null>,
default: null,
},
title: {
type: [String, Function] as PropType<string | ((data: Recordable) => string)>,
default: null,
},
titleHelpMessage: {
type: [String, Array] as PropType<string | string[]>,
},
maxHeight: propTypes.number,
dataSource: {
type: Array as PropType<Recordable[]>,
default: null,
},
rowKey: {
type: [String, Function] as PropType<string | ((record: Recordable) => string)>,
default: '',
},
bordered: propTypes.bool,
pagination: {
type: [Object, Boolean] as PropType<PaginationProps | boolean>,
default: null,
},
loading: propTypes.bool,
rowClassName: {
type: Function as PropType<(record: TableCustomRecord<any>, index: number) => string>,
},
scroll: {
type: Object as PropType<{ x: number | string | true; y: number | string }>,
default: null,
},
beforeEditSubmit: {
type: Function as PropType<
(data: {
record: Recordable;
index: number;
key: string | number;
value: any;
}) => Promise<any>
>,
},
size: {
type: String as PropType<SizeType>,
default: DEFAULT_SIZE,
},
}`
// const data = `[{"age":"14","fn": ()=>"123"}]`

console.log(useJSONParse(data)) // { name: 'simon', age: '14' }
// console.log(useJSONParse(data)) // { name: 'simon', age: '14' }
2 changes: 2 additions & 0 deletions test/__snapshots__/basic.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// 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>"`;

0 comments on commit c1a3ec6

Please sign in to comment.