Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: node path parsing does not consider special characters. #198

Merged
merged 1 commit into from
Aug 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function jsonFlatten(
keys.map((objKey, idx, arr) =>
jsonFlatten(
(data as Record<string, JSONDataType>)[objKey],
objKey.includes('.') ? `${path}["${objKey}"]` : `${path}.${objKey}`,
/^[a-zA-Z_]\w*$/.test(objKey) ? `${path}.${objKey}` : `${path}["${objKey}"]`,
level + 1,
{
key: objKey,
Expand Down Expand Up @@ -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<string, unknown>;
// 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;
// }