Skip to content

Commit

Permalink
build(cc): improving cognitive complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Feb 24, 2021
1 parent 8aa394a commit ce4f448
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
34 changes: 21 additions & 13 deletions libs/ng-mocks/src/lib/mock-helper/crawl/el-def-get-parent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,32 @@ const getVcr = (node: any, child: any): undefined | ViewContainerRef => {
return coreInjector(ViewContainerRef, child.injector);
};

const getScanViewRefRootNodes = (node: any, child: any): Array<[number, any]> => {
const vcr = getVcr(node, child);
if (!vcr) {
return [];
}

const result: Array<[number, any]> = [];
for (let vrIndex = 0; vrIndex < vcr.length; vrIndex += 1) {
const vr = vcr.get(vrIndex);
for (let rnIndex = 0; rnIndex < (vr as any).rootNodes.length; rnIndex += 1) {
result.push([rnIndex, (vr as any).rootNodes[rnIndex]]);
}
}

return result;
};

const scanViewRef = (node: DebugElement) => {
let result: any;
let index: any;

for (const child of node.parent?.childNodes || []) {
const vcr = getVcr(node, child);
if (!vcr) {
continue;
}

for (let vrIndex = 0; vrIndex < vcr.length; vrIndex += 1) {
const vr = vcr.get(vrIndex);
for (let rnIndex = 0; rnIndex < (vr as any).rootNodes.length; rnIndex += 1) {
const rootNode = (vr as any).rootNodes[rnIndex];
if (rootNode === node.nativeNode && (index === undefined || rnIndex < index)) {
result = elDefGetNode(child);
index = rnIndex;
}
for (const [rnIndex, rootNode] of getScanViewRefRootNodes(node, child)) {
if (rootNode === node.nativeNode && (index === undefined || rnIndex < index)) {
result = elDefGetNode(child);
index = rnIndex;
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions libs/ng-mocks/src/lib/mock-helper/mock-helper.format-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ const handleArray = (formatCallback: any, html: any) => {
};

const handlePrimitives = (formatCallback: any, html: any, outer: boolean) => {
if (typeof html === 'string' || html === undefined) {
return normalizeHtml(html);
}
if (Array.isArray(html)) {
return handleArray(formatCallback, html);
}
Expand All @@ -61,6 +58,7 @@ const handlePrimitives = (formatCallback: any, html: any, outer: boolean) => {

const format = (
html:
| undefined
| string
| HTMLElement
| Text
Expand All @@ -73,6 +71,10 @@ const format = (
| Array<{ nativeNode: any } | { nativeElement: any } | { debugElement: any }>,
outer = false,
): string => {
if (typeof html === 'string' || html === undefined) {
return normalizeHtml(html);
}

const result = handlePrimitives(format, html, outer);
if (result !== undefined) {
return result;
Expand Down

0 comments on commit ce4f448

Please sign in to comment.