feat: refactoring project

This commit is contained in:
Carlos
2024-11-23 14:56:07 -05:00
parent f0c2a50c18
commit 1c6db5818d
2351 changed files with 39323 additions and 60326 deletions

View File

@@ -25,7 +25,6 @@
// 3.2% that 5 or more groups are invalidated
/**
*
* @param {string} a key
* @param {string} b key
* @returns {number} the similarity as number
@@ -116,9 +115,7 @@ const isTooBig = (size, maxSize) => {
const s = size[key];
if (s === 0) continue;
const maxSizeValue = maxSize[key];
if (typeof maxSizeValue === "number") {
if (s > maxSizeValue) return true;
}
if (typeof maxSizeValue === "number" && s > maxSizeValue) return true;
}
return false;
};
@@ -133,9 +130,7 @@ const isTooSmall = (size, minSize) => {
const s = size[key];
if (s === 0) continue;
const minSizeValue = minSize[key];
if (typeof minSizeValue === "number") {
if (s < minSizeValue) return true;
}
if (typeof minSizeValue === "number" && s < minSizeValue) return true;
}
return false;
};
@@ -151,9 +146,7 @@ const getTooSmallTypes = (size, minSize) => {
const s = size[key];
if (s === 0) continue;
const minSizeValue = minSize[key];
if (typeof minSizeValue === "number") {
if (s < minSizeValue) types.add(key);
}
if (typeof minSizeValue === "number" && s < minSizeValue) types.add(key);
}
return types;
};
@@ -236,14 +229,14 @@ class Group {
newSimilarities.push(
lastNode === this.nodes[i - 1]
? /** @type {number[]} */ (this.similarities)[i - 1]
: similarity(lastNode.key, node.key)
: similarity(/** @type {Node<T>} */ (lastNode).key, node.key)
);
}
newNodes.push(node);
lastNode = node;
}
}
if (resultNodes.length === this.nodes.length) return undefined;
if (resultNodes.length === this.nodes.length) return;
this.nodes = newNodes;
this.similarities = newSimilarities;
this.size = sumSize(newNodes);
@@ -260,7 +253,7 @@ const getSimilarities = nodes => {
// calculate similarities between lexically adjacent nodes
/** @type {number[]} */
const similarities = [];
let last = undefined;
let last;
for (const node of nodes) {
if (last !== undefined) {
similarities.push(similarity(last.key, node.key));
@@ -371,9 +364,8 @@ module.exports = ({ maxSize, minSize, items, getSize, getKey }) => {
result.push(new Group(problemNodes, null));
}
return true;
} else {
return false;
}
return false;
};
if (initialGroup.nodes.length > 0) {
@@ -398,14 +390,14 @@ module.exports = ({ maxSize, minSize, items, getSize, getKey }) => {
// going minSize from left and right
// at least one node need to be included otherwise we get stuck
let left = 1;
let leftSize = Object.create(null);
const leftSize = Object.create(null);
addSizeTo(leftSize, group.nodes[0].size);
while (left < group.nodes.length && isTooSmall(leftSize, minSize)) {
addSizeTo(leftSize, group.nodes[left].size);
left++;
}
let right = group.nodes.length - 2;
let rightSize = Object.create(null);
const rightSize = Object.create(null);
addSizeTo(rightSize, group.nodes[group.nodes.length - 1].size);
while (right >= 0 && isTooSmall(rightSize, minSize)) {
addSizeTo(rightSize, group.nodes[right].size);
@@ -453,7 +445,7 @@ module.exports = ({ maxSize, minSize, items, getSize, getKey }) => {
let best = -1;
let bestSimilarity = Infinity;
let pos = left;
let rightSize = sumSize(group.nodes.slice(pos));
const rightSize = sumSize(group.nodes.slice(pos));
// pos v v right
// [ O O O ] O O O [ O O O ]
@@ -536,12 +528,13 @@ module.exports = ({ maxSize, minSize, items, getSize, getKey }) => {
}
// return the results
return result.map(group => {
/** @type {GroupedItems<T>} */
return {
key: group.key,
items: group.nodes.map(node => node.item),
size: group.size
};
});
return result.map(
group =>
/** @type {GroupedItems<T>} */
({
key: group.key,
items: group.nodes.map(node => node.item),
size: group.size
})
);
};