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

Allow appendAsEAVs to also handle nested sub-records and sub-record sets #861

Merged
merged 1 commit into from Jun 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/watchers/watcher.ts
Expand Up @@ -203,6 +203,17 @@ export function isRawEAVArray(x:any): x is RawEAV[] {
return false;
}

export function isRawEAVArraySet(x:any): x is RawEAV[][] {
return x && x.constructor === Array && isRawEAVArray(x[0]);
}

export function isRecord(x:any): x is Attrs {
return x && typeof x === "object" && x.constructor !== Array;
}

export function isRecordSet(x:any): x is Attrs[] {
return x && x.constructor === Array && isRecord(x[0]);
}

export interface Attrs extends RawMap<RawValue|RawValue[]|RawEAV[]|RawEAV[][]> {}
export function appendAsEAVs(eavs:any[], record: Attrs, id = createId()) {
Expand All @@ -222,13 +233,25 @@ export function appendAsEAVs(eavs:any[], record: Attrs, id = createId()) {
eavs.push([id, attr, childId]);
for(let childEAV of childEAVs) eavs.push(childEAV);

} else {
} else if(isRawEAVArraySet(value)) {
// We have a set of nested sub-objects.
for(let childEAVs of value) {
let [childId] = childEAVs[0];
eavs.push([id, attr, childId]);
for(let childEAV of childEAVs) eavs.push(childEAV);
}
} else if(isRecord(value)) {
let ix = eavs.length;
appendAsEAVs(eavs, value);
let [childId] = eavs[ix];
eavs.push([id, attr, childId]);
} else if(isRecordSet(value)) {
for(let record of value) {
let ix = eavs.length;
appendAsEAVs(eavs, record);
let [childId] = eavs[ix];
eavs.push([id, attr, childId]);
}
}
}

Expand Down