Skip to content

Commit

Permalink
Changed to webcrypto.randomUUID and fixed bug with spouse and parents
Browse files Browse the repository at this point in the history
  • Loading branch information
imertz committed Apr 27, 2023
1 parent afed6ef commit dab0f48
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-mangos-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"klironomiko": patch
---

Changed to webcrypto.randomUUID and fixed bug with spouse and parents
51 changes: 18 additions & 33 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nanoid } from "nanoid";
import { webcrypto } from "crypto";

type Relatives = {
spouse: Spouse;
Expand Down Expand Up @@ -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) => {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit dab0f48

Please sign in to comment.