Skip to content

Commit

Permalink
Added several-spouses.json & Fixed issue #2 (overlapping spouses)
Browse files Browse the repository at this point in the history
  • Loading branch information
SanichKotikov committed Jul 8, 2020
1 parent fe6f76d commit 8fb3094
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# relatives-tree

A tiny library (~3.2kb gz) for calculating specific JSON data to family tree nodes and connectors.
A tiny library (~3.23 KB gz) for calculating specific JSON data to family tree nodes and connectors.

🖥 [Here is a demo](https://sanichkotikov.github.io/react-family-tree-example/) app with React rendering.

Expand Down
14 changes: 14 additions & 0 deletions __tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { Family, IFamilyExtNode } from '../src/types';
import empty from '../samples/empty.json';
import couple from '../samples/couple.json';
import simple from '../samples/simple-family.json';
import spouses from '../samples/several-spouses.json';
import average from '../samples/average-tree.json';

type ResultObj = { [id: string]: boolean | string };

const emptyTree = calcTree(empty as any, { rootId: 'gRstruEr4' });
const coupleTree = calcTree(couple as any, { rootId: 'jsyRsE5sr' });
const simpleTree = calcTree(simple as any, { rootId: 'dyTpfj6sr' });
const spousesTree = calcTree(spouses as any, { rootId: 'js2RsE5sr' });
const average1 = calcTree(average as any, { rootId: 'kuVISwh7w' });
const average2 = calcTree(average as any, { rootId: 'PXACjDxmR' });
const average3 = calcTree(average as any, { rootId: 'UIEjvLJMd' });
Expand All @@ -21,6 +23,7 @@ test('Canvas size', () => {
expect(emptyTree.canvas).toEqual({ width: 2, height: 2 });
expect(coupleTree.canvas).toEqual({ width: 4, height: 2 });
expect(simpleTree.canvas).toEqual({ width: 4, height: 4 });
expect(spousesTree.canvas).toEqual({ width: 6, height: 2 });
expect(average1.canvas).toEqual({ width: 18, height: 12 });
expect(average2.canvas).toEqual({ width: 20, height: 10 });
expect(average3.canvas).toEqual({ width: 18, height: 12 });
Expand All @@ -37,6 +40,7 @@ test('Family size & position', () => {
expect(convert(emptyTree.families)).toEqual(['1|0|0|2|2']);
expect(convert(coupleTree.families)).toEqual(['1|0|0|4|4']);
expect(convert(simpleTree.families)).toEqual(['1|0|1|3|2', '2|0|0|4|4']);
expect(convert(spousesTree.families)).toEqual(['1|0|0|6|6']);
expect(convert(average1.families)).toEqual([
'1|4|0|18|18',
'2|2|5|13|8',
Expand Down Expand Up @@ -86,6 +90,11 @@ test('Node position', () => {
'ahfR5lR2s': '2|0',
'aoF9dn5Ew': '2|2',
});
expect(convert(spousesTree.nodes)).toEqual({
'js2RsE5sr': '0|2',
'pdRwdtR54': '0|4',
'tdRwdtR54': '0|0',
});
expect(convert(average1.nodes)).toEqual({
'011jVS4rb': '4|7',
'PXACjDxmR': '4|9',
Expand Down Expand Up @@ -219,6 +228,11 @@ test('Node sub tree', () => {
'ahfR5lR2s': false,
'aoF9dn5Ew': false,
});
expect(convert(spousesTree.nodes)).toEqual({
'js2RsE5sr': false,
'pdRwdtR54': false,
'tdRwdtR54': false,
});
expect(convert(average1.nodes)).toEqual({
'011jVS4rb': false,
'PXACjDxmR': false,
Expand Down
6 changes: 4 additions & 2 deletions docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IFamilyNode } from '../../src/types';
import empty from '../../samples/empty.json';
import couple from '../../samples/couple.json';
import simple from '../../samples/simple-family.json';
import spouses from '../../samples/several-spouses.json';
import average from '../../samples/average-tree.json';
import { draw } from './render';

Expand All @@ -13,6 +14,7 @@ const data: { [key: string]: any[] } = {
'gRstruEr4': empty,
'jsyRsE5sr': couple,
'dyTpfj6sr': simple,
'js2RsE5sr': spouses,
'kuVISwh7w': average,
'PXACjDxmR': average,
'UIEjvLJMd': average,
Expand All @@ -21,7 +23,7 @@ const data: { [key: string]: any[] } = {
'2DlrR0fK8': average,
};

function test(el: HTMLElement, nodes: IFamilyNode[], rootId: string) {
function drawTree(el: HTMLElement, nodes: IFamilyNode[], rootId: string) {
const canvas = document.createElement('canvas');
const tree = calcTree(nodes, { rootId });

Expand All @@ -30,5 +32,5 @@ function test(el: HTMLElement, nodes: IFamilyNode[], rootId: string) {
}

Object.keys(data).forEach(id => {
test(content, data[id], id);
drawTree(content, data[id], id);
});
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "relatives-tree",
"version": "2.0.1",
"version": "2.0.2",
"description": "Calculates families and nodes positions for rendering a family tree",
"main": "lib/index.js",
"module": "lib/index.js",
Expand Down
45 changes: 45 additions & 0 deletions samples/several-spouses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[
{
"id": "js2RsE5sr",
"gender": "male",
"parents": [],
"siblings": [],
"spouses": [
{
"id": "pdRwdtR54",
"type": "married"
},
{
"id": "tdRwdtR54",
"type": "divorced"
}
],
"children": []
},
{
"id": "pdRwdtR54",
"gender": "female",
"parents": [],
"siblings": [],
"spouses": [
{
"id": "js2RsE5sr",
"type": "married"
}
],
"children": []
},
{
"id": "tdRwdtR54",
"gender": "female",
"parents": [],
"siblings": [],
"spouses": [
{
"id": "js2RsE5sr",
"type": "divorced"
}
],
"children": []
}
]
8 changes: 4 additions & 4 deletions src/connectors/children.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export default (families: Family[]): IConnector[] => {
if (family.pUnits.length === 1) {
const pUnit = family.pUnits[0];
pX = family.left + pUnit.shift + (pUnit.size); // TODO
const pY = family.top + 1;

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

const parentIds = family.pUnits
Expand Down
2 changes: 2 additions & 0 deletions src/middle/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import getChildUnits from '../children/getChildUnits';
import byParents from '../children/byParents';
import getSpouses from '../utils/getSpouses';
import fixOverlaps from './fixOverlaps';
import setUnitDefShifts from '../utils/setUnitDefShifts';
import { flat, hasDiffParents, prop, withType } from '../utils';
import Store from '../store';
import Family from '../models/family';
Expand All @@ -12,6 +13,7 @@ export default (store: Store): Store => {
if (!store.rootNode.parents.length) {
const family = new Family(store.getNextId(), 'root', true);
getChildUnits(store, family.id, store.rootNode).forEach(unit => family.cUnits.push(unit));
setUnitDefShifts(family);
families.push(family);
}
else {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getExtendedNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function getChildNodes(family: Family) {
}

function mapFamily(family: Family) {
return [...getParentNodes(family), ...getChildNodes(family)].reduce(flat);
return [...getParentNodes(family), ...getChildNodes(family)].reduce(flat, []);
}

export default (families: ReadonlyArray<Family>): ReadonlyArray<IFamilyExtNode> => {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/hasHiddenRelatives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ function inUnits(units: ReadonlyArray<Unit>, nodeId: string) {
export default (family: Family, node: IFamilyNode): boolean => {
if (family.type !== 'child' && inUnits(family.pUnits, node.id)) {
return (
(!node.parents.length && !!node.siblings.length) ||
(family.type === 'parent' && node.children.length > 1)
(family.type === 'parent' && (node.children.length > 1 || node.spouses.length > 1)) ||
(!node.parents.length && !!node.siblings.length)
);
}

Expand All @@ -23,4 +23,4 @@ export default (family: Family, node: IFamilyNode): boolean => {
}

return false;
};
};
6 changes: 4 additions & 2 deletions src/utils/setUnitDefShifts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ const shiftUnits = (units: Unit[], shift: number): void => {
));
};

export default (family: Family): void => {
function setUnitDefShifts(family: Family): void {
const diff = family.cCount - family.pCount;
shiftUnits(family.pUnits, diff > 0 ? Math.abs(diff) : 0);
shiftUnits(family.cUnits, diff < 0 ? Math.abs(diff) : 0);
};
}

export default setUnitDefShifts;

0 comments on commit 8fb3094

Please sign in to comment.