Skip to content

Commit

Permalink
prettier . --write
Browse files Browse the repository at this point in the history
  • Loading branch information
SanichKotikov committed Oct 9, 2023
1 parent f63116b commit 1e73bdf
Show file tree
Hide file tree
Showing 36 changed files with 270 additions and 335 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "build & test"
name: 'build & test'

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/size-limit.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "size"
name: 'size'

on:
pull_request:
Expand Down
10 changes: 7 additions & 3 deletions benchmarks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ const DEFAULT_ROOT_ID = 'kuVISwh7w';
const SIMPLE_ROOT_ID = 'vRSjcaDGj';

function run(nodes) {
(new benchmark.Suite('relatives-tree benchmark'))
.add('Default', function() { getFamilyTree(nodes, { rootId: DEFAULT_ROOT_ID }); })
.add('Simple', function() { getFamilyTree(nodes, { rootId: SIMPLE_ROOT_ID }); })
new benchmark.Suite('relatives-tree benchmark')
.add('Default', function () {
getFamilyTree(nodes, { rootId: DEFAULT_ROOT_ID });
})
.add('Simple', function () {
getFamilyTree(nodes, { rootId: SIMPLE_ROOT_ID });
})
.on('cycle', (event) => console.log('> ' + String(event.target)))
.on('error', (event) => console.log(event.target.error.message))
.run();
Expand Down
8 changes: 4 additions & 4 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta charset="UTF-8" />
<title>relatives-tree</title>
<meta name="description" content="Calculates families and nodes positions for rendering a family tree">
<link rel="stylesheet" href="src/styles.css">
<meta name="description" content="Calculates families and nodes positions for rendering a family tree" />
<link rel="stylesheet" href="src/styles.css" />
</head>
<body>
<div id="content"></div>
Expand Down
2 changes: 1 addition & 1 deletion docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { draw } from './render';
const content = document.getElementById('content');
if (!content) throw new Error('There is no content element');

const data: readonly { id: string, nodes: any[] }[] = [
const data: readonly { id: string; nodes: any[] }[] = [
{ id: 'gRstruEr4', nodes: empty },
{ id: 'jsyRsE5sr', nodes: couple },
{ id: 'dyTpfj6sr', nodes: simple },
Expand Down
23 changes: 9 additions & 14 deletions docs/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface IRenderOptions {
}

function getRandomColor() {
return '#' + ((1 << 24) * Math.random() | 0).toString(16);
return '#' + (((1 << 24) * Math.random()) | 0).toString(16);
}

function setupCanvas(canvas: HTMLCanvasElement, size: Size) {
Expand Down Expand Up @@ -56,7 +56,7 @@ export function draw(
ctx.font = `${FONT_SIZE}px/1 serif`;

if (options.debug) {
tree.families.forEach(family => {
tree.families.forEach((family) => {
const x = family.X * X;
const y = family.Y * X;
const color = getRandomColor();
Expand All @@ -72,34 +72,29 @@ export function draw(
ctx.fillText(family.id.toString(10), x + uP, y + FONT_SIZE + uP);

if (family.type === 'root' || family.type === 'parent') {
family.parents.forEach(unit => {
family.parents.forEach((unit) => {
ctx.beginPath();
ctx.rect(
(family.X + unit.pos) * X + uP,
y + uP,
((unit.nodes.length * SIZE) * X) - (uP * 2),
(X * SIZE) - (uP * 2),
);
ctx.rect((family.X + unit.pos) * X + uP, y + uP, unit.nodes.length * SIZE * X - uP * 2, X * SIZE - uP * 2);
ctx.stroke();
});
}

if (family.type === 'root' || family.type === 'child') {
family.children.forEach(unit => {
family.children.forEach((unit) => {
ctx.beginPath();
ctx.rect(
(family.X + unit.pos) * X + uP,
y + uP + (family.parents.length ? X * SIZE : 0),
((unit.nodes.length * SIZE) * X) - (uP * 2),
(X * SIZE) - (uP * 2),
unit.nodes.length * SIZE * X - uP * 2,
X * SIZE - uP * 2,
);
ctx.stroke();
});
}
});
}

tree.nodes.forEach(node => {
tree.nodes.forEach((node) => {
const { left, top } = node;

const x = left * X;
Expand All @@ -120,7 +115,7 @@ export function draw(

if (node.hasSubTree) {
ctx.beginPath();
ctx.arc(x + (X * SIZE) - nP, y + nP, 4, 0, Math.PI * 2);
ctx.arc(x + X * SIZE - nP, y + nP, 4, 0, Math.PI * 2);
ctx.fill();
}
});
Expand Down
15 changes: 6 additions & 9 deletions src/children/arrange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ const arrangeNextFamily = (family: Family, nextFamily: Family, right: number): v
const index = nextFamily.children.findIndex(sameAs(unit));

index === 0
? nextFamily.X = getUnitX(family, unit) - nextFamily.children[index]!.pos
: nextFamily.children[index]!.pos = getUnitX(family, unit) - nextFamily.X;
? (nextFamily.X = getUnitX(family, unit) - nextFamily.children[index]!.pos)
: (nextFamily.children[index]!.pos = getUnitX(family, unit) - nextFamily.X);

const nextIdx: number = nextIndex(index);

if (nextFamily.children[nextIdx]) {
correctUnitsShift(
nextFamily.children.slice(nextIdx),
right - getUnitX(nextFamily, nextFamily.children[nextIdx]!),
);
correctUnitsShift(nextFamily.children.slice(nextIdx), right - getUnitX(nextFamily, nextFamily.children[nextIdx]!));
}
};

Expand All @@ -33,7 +30,8 @@ const arrangeMiddleFamilies = (families: readonly Family[], fid: number, startFr
}
};

export const arrangeFamiliesFunc = (store: Store) => (
export const arrangeFamiliesFunc =
(store: Store) =>
(family: Family): void => {
let right = 0;

Expand All @@ -49,5 +47,4 @@ export const arrangeFamiliesFunc = (store: Store) => (

family = nextFamily;
}
}
);
};
15 changes: 6 additions & 9 deletions src/children/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { setDefaultUnitShift } from '../utils/setDefaultUnitShift';
import { createChildUnitsFunc } from '../utils/createChildUnitsFunc';
import { Family, FamilyType, Node, Relation, Unit } from '../types';

const hasSameRelation = (node: Node | undefined) => (
(rel: Relation): boolean => !node || node.children.some(withId(rel.id))
);
const hasSameRelation =
(node: Node | undefined) =>
(rel: Relation): boolean =>
!node || node.children.some(withId(rel.id));

const getChildUnitsFunc = (store: Store) => {
const toNode = relToNode(store);
Expand All @@ -17,9 +18,7 @@ const getChildUnitsFunc = (store: Store) => {
return (familyId: number, parents: readonly Node[]): readonly Unit[] => {
const [first, second] = parents as [Node, Node | undefined];

return first.children
.filter(hasSameRelation(second))
.flatMap((rel) => createChildUnits(familyId, toNode(rel)));
return first.children.filter(hasSameRelation(second)).flatMap((rel) => createChildUnits(familyId, toNode(rel)));
};
};

Expand All @@ -29,9 +28,7 @@ export const createFamilyFunc = (store: Store) => {
return (parentIDs: readonly string[], type = FamilyType.root, isMain: boolean = false): Family => {
const family = newFamily(store.getNextId(), type, isMain);

const parents: Node[] = parentIDs
.map(id => store.getNode(id))
.sort(byGender(store.root.gender));
const parents: Node[] = parentIDs.map((id) => store.getNode(id)).sort(byGender(store.root.gender));

family.parents = [newUnit(family.id, parents)];
family.children = getChildUnits(family.id, parents);
Expand Down
28 changes: 12 additions & 16 deletions src/children/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,27 @@ import { createFamilyFunc } from './create';
import { updateFamilyFunc } from './update';
import { arrangeFamiliesFunc } from './arrange';

const getUnitsWithChildren = (family: Family): Unit[] => (
family.children.filter(hasChildren).reverse()
);
const getUnitsWithChildren = (family: Family): Unit[] => family.children.filter(hasChildren).reverse();

export const inChildDirection = (store: Store): Store => {
const createFamily = createFamilyFunc(store);
const updateFamily = updateFamilyFunc(store);
const arrangeFamilies = arrangeFamiliesFunc(store);

store.familiesArray
.filter(withType(FamilyType.root))
.forEach((rootFamily) => {
let stack: Unit[] = getUnitsWithChildren(rootFamily);
store.familiesArray.filter(withType(FamilyType.root)).forEach((rootFamily) => {
let stack: Unit[] = getUnitsWithChildren(rootFamily);

while (stack.length) {
const parentUnit = stack.pop()!;
const family = createFamily(nodeIds(parentUnit), FamilyType.child);
while (stack.length) {
const parentUnit = stack.pop()!;
const family = createFamily(nodeIds(parentUnit), FamilyType.child);

updateFamily(family, parentUnit);
arrangeFamilies(family);
updateFamily(family, parentUnit);
arrangeFamilies(family);

store.families.set(family.id, family);
stack = stack.concat(getUnitsWithChildren(family));
}
});
store.families.set(family.id, family);
stack = stack.concat(getUnitsWithChildren(family));
}
});

return store;
};
6 changes: 3 additions & 3 deletions src/children/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { heightOf } from '../utils/family';
import { getUnitX } from '../utils/units';
import type { Family, Unit } from '../types';

export const updateFamilyFunc = (store: Store) => (
export const updateFamilyFunc =
(store: Store) =>
(family: Family, parentUnit: Unit): void => {
const parentFamily = store.getFamily(parentUnit.fid);

family.pid = parentFamily.id;
family.Y = parentFamily.Y + heightOf(parentFamily) - SIZE;
family.X = getUnitX(parentFamily, parentUnit);
}
);
};
97 changes: 46 additions & 51 deletions src/connectors/children.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,55 @@ import { inAscOrder, max, min, withId, withIds } from '../utils';
import { HALF_SIZE, NODES_IN_COUPLE, SIZE } from '../constants';
import { Connector, Family, FamilyType, Unit } from '../types';

export const children = (families: readonly Family[]): readonly Connector[] => (
families
.filter(withType(FamilyType.root, FamilyType.child))
.reduce<Connector[]>((connectors, family) => {
const parent: Unit | undefined = family.parents[0]!;

const pX = getParentsX(family, parent);
const mY = family.Y + (parent ? SIZE : 0);

// from parent(s) to child
if (parent && parent.nodes.every(node => !!node.children.length)) {
const pY = family.Y + HALF_SIZE;
connectors.push([pX, pY, pX, mY]);
}

const parentIds = family.parents.map(nodeIds).flat();
const positions: number[] = [];
export const children = (families: readonly Family[]): readonly Connector[] =>
families.filter(withType(FamilyType.root, FamilyType.child)).reduce<Connector[]>((connectors, family) => {
const parent: Unit | undefined = family.parents[0]!;

const pX = getParentsX(family, parent);
const mY = family.Y + (parent ? SIZE : 0);

// from parent(s) to child
if (parent && parent.nodes.every((node) => !!node.children.length)) {
const pY = family.Y + HALF_SIZE;
connectors.push([pX, pY, pX, mY]);
}

const parentIds = family.parents.map(nodeIds).flat();
const positions: number[] = [];

family.children.forEach((unit) => {
const left = getUnitX(family, unit) + HALF_SIZE;

// from child to parent(s)
unit.nodes.forEach((node, index) => {
if (node.parents.some(withIds(parentIds))) {
const nX = left + index * SIZE;
positions.push(nX);
connectors.push([nX, mY, nX, mY + HALF_SIZE]);
}
});

family.children.forEach(unit => {
const left = getUnitX(family, unit) + HALF_SIZE;
// between child and child's spouse
if (nodeCount(unit) === NODES_IN_COUPLE) {
connectors.push([left, mY + HALF_SIZE, left + SIZE, mY + HALF_SIZE]);
}

// from child to parent(s)
unit.nodes.forEach((node, index) => {
if (node.parents.some(withIds(parentIds))) {
const nX = left + (index * SIZE);
positions.push(nX);
connectors.push([nX, mY, nX, mY + HALF_SIZE]);
// between child and child's side spouse
else if (nodeCount(unit) === 1 && unit.nodes[0]!.spouses.length) {
family.children.forEach((nUnit) => {
if (nUnit.nodes.some(withId(unit.nodes[0]!.spouses[0]!.id))) {
const xX = [left, getUnitX(family, nUnit) + HALF_SIZE].sort(inAscOrder);
connectors.push([xX[0]!, mY + HALF_SIZE, xX[1]!, mY + HALF_SIZE]);
}
});
}
});

// between child and child's spouse
if (nodeCount(unit) === NODES_IN_COUPLE) {
connectors.push([left, mY + HALF_SIZE, left + SIZE, mY + HALF_SIZE]);
}

// between child and child's side spouse
else if (nodeCount(unit) === 1 && unit.nodes[0]!.spouses.length) {
family.children.forEach(nUnit => {
if (nUnit.nodes.some(withId(unit.nodes[0]!.spouses[0]!.id))) {
const xX = [left, getUnitX(family, nUnit) + HALF_SIZE].sort(inAscOrder);
connectors.push([xX[0]!, mY + HALF_SIZE, xX[1]!, mY + HALF_SIZE]);
}
});
}
});

// horizontal above children
if (positions.length > 1)
connectors.push([min(positions), mY, max(positions), mY]);

// horizontal between parent(s) and child
else if (positions.length === 1 && pX !== positions[0])
connectors.push([Math.min(pX, positions[0]!), mY, Math.max(pX, positions[0]!), mY]);
// horizontal above children
if (positions.length > 1) connectors.push([min(positions), mY, max(positions), mY]);
// horizontal between parent(s) and child
else if (positions.length === 1 && pX !== positions[0])
connectors.push([Math.min(pX, positions[0]!), mY, Math.max(pX, positions[0]!), mY]);

return connectors;
}, [])
);
return connectors;
}, []);
5 changes: 2 additions & 3 deletions src/connectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ import type { Connector, Family } from '../types';

const toConnectors = (families: readonly Family[]) => (fn: Function) => fn(families);

export const connectors = (families: readonly Family[]): readonly Connector[] => (
[parents, middle, children].map(toConnectors(families)).flat()
);
export const connectors = (families: readonly Family[]): readonly Connector[] =>
[parents, middle, children].map(toConnectors(families)).flat();
Loading

0 comments on commit 1e73bdf

Please sign in to comment.