From dab0f48c73aa5f839edd3e45b1c538d96b2ee239 Mon Sep 17 00:00:00 2001 From: Ioannis Mertzanis Date: Thu, 27 Apr 2023 13:46:36 +0300 Subject: [PATCH] Changed to webcrypto.randomUUID and fixed bug with spouse and parents --- .changeset/loud-mangos-smell.md | 5 ++++ index.ts | 51 ++++++++++++--------------------- 2 files changed, 23 insertions(+), 33 deletions(-) create mode 100644 .changeset/loud-mangos-smell.md diff --git a/.changeset/loud-mangos-smell.md b/.changeset/loud-mangos-smell.md new file mode 100644 index 0000000..daaa1b0 --- /dev/null +++ b/.changeset/loud-mangos-smell.md @@ -0,0 +1,5 @@ +--- +"klironomiko": patch +--- + +Changed to webcrypto.randomUUID and fixed bug with spouse and parents diff --git a/index.ts b/index.ts index 3b67a64..66b40e6 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,4 @@ -import { nanoid } from "nanoid"; +import { webcrypto } from "crypto"; type Relatives = { spouse: Spouse; @@ -60,7 +60,7 @@ export function calculateHeirPercentage(relatives: Relatives) { return; } - relative.uuid = nanoid().replace(/-/g, ""); + relative.uuid = webcrypto.randomUUID().replace(/-/g, ""); if (relative.descendants) { relative.descendants.forEach((descendant) => { @@ -87,6 +87,8 @@ export function calculateHeirPercentage(relatives: Relatives) { (relative: DescendantWithUuid) => relative.relation === "child" ); const childrenCount = children.length; + console.log(childrenCount); + if (childrenCount > 0) { const childrenPercentage = childrenCount > 0 ? descendantPercentage / childrenCount : 0; @@ -193,7 +195,7 @@ export function calculateHeirPercentage(relatives: Relatives) { } if ( childrenCount === 0 && - (relativesWithUuid.spouse.alive || !relativesWithUuid.spouse.apodochi) + (!relativesWithUuid.spouse.alive || !relativesWithUuid.spouse.apodochi) ) { percentages["parents"] = 1; return relativesWithUuid; @@ -202,7 +204,7 @@ export function calculateHeirPercentage(relatives: Relatives) { childrenCount === 0 && relativesWithUuid.spouse.alive && relativesWithUuid.spouse.apodochi && - relativesWithUuid.parents === 0 + relativesWithUuid.parents !== 0 ) { relativesWithUuid.spouse = { ...relativesWithUuid.spouse, @@ -213,35 +215,18 @@ export function calculateHeirPercentage(relatives: Relatives) { } } -// Testing my package -const heirPercentage = calculateHeirPercentage({ - spouse: { - alive: true, - apodochi: true, - }, - parents: 2, - descendants: [ - { - fname: "John", - relation: "child", - descendants: [ - { - fname: "John's child", - relation: "grand-child", - descendants: [ - { - fname: "John's grandchild", - relation: "great-grand-child", - }, - ], - }, - ], - }, - ], -}); - -// Pretty print the heir percentage -console.log(JSON.stringify(heirPercentage, null, 2)); +// // Testing my package +// const heirPercentage = calculateHeirPercentage({ +// spouse: { +// alive: true, +// apodochi: true, +// }, +// parents: 2, +// descendants: [], +// }); + +// // Pretty print the heir percentage +// console.log(JSON.stringify(heirPercentage, null, 2)); function toFraction(decimal: number) { const tolerance = 1.0e-10; // set a tolerance for floating-point comparison