Skip to content

Commit

Permalink
fix flatten/unflatten
Browse files Browse the repository at this point in the history
  • Loading branch information
0x6563 committed May 29, 2023
1 parent 34273b9 commit 0bbe3a0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 39 deletions.
2 changes: 1 addition & 1 deletion build/utility/general.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Dictionary, GrammarRuleSymbol } from "../typings";
export declare class Collection<T> {
categorized: Dictionary<Dictionary<number>>;
private uncategorized;
uncategorized: Map<T, number>;
items: T[];
constructor(ref?: T[]);
encode(ref: T): number;
Expand Down
46 changes: 28 additions & 18 deletions build/utility/general.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/utility/general.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 31 additions & 19 deletions src/utility/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Dictionary, GrammarRuleSymbol } from "../typings";

export class Collection<T> {
categorized: Dictionary<Dictionary<number>> = {};
private uncategorized = new Map<T, number>();
uncategorized = new Map<T, number>();
items: T[] = [];

constructor(ref: T[] = []) {
Expand Down Expand Up @@ -128,26 +128,33 @@ export class Matrix<T> {
}
}


export function Flatten(obj: any[] | { [key: string]: any }): FlatObject {
const collection = new Collection();
function Traverse(ref: any) {
if (collection.has(ref)) {
return collection.encode(ref)
const $null = Symbol();
function Traverse(src: any) {
if (src == null) {
src = $null;
}
if (collection.has(src)) {
return collection.encode(src)
}
if (Array.isArray(ref)) {
collection.redirect(ref, ref.map(v => Traverse(v)));
} else if (typeof ref === 'object') {
collection.encode(src);
if (Array.isArray(src)) {
collection.redirect(src, src.map(v => Traverse(v)));
} else if (typeof src === 'object') {
const o = {};
for (const k in ref) {
o[k] = Traverse(ref[k])
for (const k in src) {
o[k] = Traverse(src[k])
}
collection.redirect(ref, o);
} else if (typeof ref === 'function') {
return collection.encode(ref.toString());
collection.redirect(src, o);
} else if (typeof src === 'function') {
return collection.redirect(src, src.toString());
}
return collection.encode(ref);
return collection.encode(src);
}
Traverse(obj);
collection.redirect($null, null);
return collection.items as any;
}

Expand All @@ -158,14 +165,19 @@ export function Unflatten(items: FlatObject) {
return items[id];
}
visited.add(id);
if (Array.isArray(items[id])) {
return (items[id] as any[]).map(v => Traverse(id));
} else if (typeof items[id] === 'object') {
for (const k in items[id] as { [key: string]: any }) {
items[id][k] = Traverse(id[k])
const obj: any = items[id];
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
const ii = obj[i];
obj[i] = Traverse(ii);

}
} else if (typeof obj === 'object') {
for (const k in obj as { [key: string]: any }) {
obj[k] = Traverse(obj[k])
}
}
return items[id];
return obj;
}
return Traverse(0);
}
Expand Down

0 comments on commit 0bbe3a0

Please sign in to comment.