diff --git a/src/utils/index.ts b/src/utils/index.ts index 47ac26a..1cbe1c6 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -76,7 +76,7 @@ export function jsonFlatten( keys.map((objKey, idx, arr) => jsonFlatten( (data as Record)[objKey], - objKey.includes('.') ? `${path}["${objKey}"]` : `${path}.${objKey}`, + /^[a-zA-Z_]\w*$/.test(objKey) ? `${path}.${objKey}` : `${path}["${objKey}"]`, level + 1, { key: objKey, @@ -171,3 +171,79 @@ export function stringToAutoType(source: string): unknown { } return value; } + +// function jsonFlattenNext(data: JSONDataType, path = 'root'): JSONFlattenReturnType[] { +// function test(content: unknown, path: string, level: number, options: JSONFlattenOptions) { +// const { type = 'content', key, index, showComma, length } = options || {}; +// return { +// content, +// level, +// key, +// index, +// path, +// showComma, +// length, +// type, +// }; +// } + +// let endObj = {} as Record; +// let i = 0; +// let output: any = [data]; +// let startArr = []; +// while (output.length || endObj[i]) { +// if (i && endObj[i]) { +// const k = endObj[i]; +// delete endObj[i]; +// startArr.push(k); +// i++; +// } else { +// const now = output.shift(); +// if (Array.isArray(now)) { +// startArr.push( +// test('[', path, 0, { +// type: 'arrayStart', +// showComma: false, +// length: now.length, +// }), +// ); +// output = now.concat(output); +// Object.keys(endObj) +// .sort((a, b) => Number(b) - Number(a)) +// .forEach(id => { +// endObj[Number(id) + now.length + 1] = endObj[id]; +// delete endObj[id]; +// }); +// endObj[i + now.length] = ']'; +// } else if (typeof now === 'object') { +// startArr.push( +// test('{', path, 0, { +// type: 'objectStart', +// showComma: false, +// length: Object.keys(now).length, +// }), +// ); +// output = Object.keys(now) +// .map(key => now[key]) +// .concat(output); +// Object.keys(endObj) +// .sort((a, b) => Number(b) - Number(a)) +// .forEach(id => { +// endObj[Number(id) + Object.keys(now).length + 1] = endObj[id]; +// delete endObj[id]; +// }); +// endObj[i + Object.keys(now).length] = '}'; +// } else { +// startArr.push( +// test(now, path, 0, { +// type: 'content', +// showComma: false, +// length: 0, +// }), +// ); +// } +// } +// } + +// return startArr as any; +// }