From 6c4d4437acb6e70a4a487e786a407127daa85980 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Wed, 15 Jun 2022 23:08:27 -0400 Subject: [PATCH] tweak: reorganize tools --- packages/utility-tools/src/index.js | 59 ++------------------ packages/utility-tools/src/process-input.js | 60 +++++++++++++++++++++ 2 files changed, 63 insertions(+), 56 deletions(-) create mode 100644 packages/utility-tools/src/process-input.js diff --git a/packages/utility-tools/src/index.js b/packages/utility-tools/src/index.js index 4c1094c..e11aaae 100644 --- a/packages/utility-tools/src/index.js +++ b/packages/utility-tools/src/index.js @@ -1,60 +1,7 @@ -// IMPORTS -import * as fs from "fs"; import { mergeSort } from "./mergeSort.js"; import { findDistancesFromAllElementsToAllReferencePoints } from "./findPaths.js"; import { aggregatePaths } from "./aggregatePaths.js"; -// DEFS -const inputLinksFilePath = "./input/input-links.json"; -const inputListFilePath = "./input/input-list.json"; -const outputFilePath = "./output/output.json"; - -// HELPERS - -const findElementPosition = (name, orderedList) => { - let node = orderedList.find((node) => node.name == name); - return node.position; -}; - -// MAIN -async function main() { - // read file - const inputLinksAsString = fs.readFileSync(inputLinksFilePath); - const inputListAsString = fs.readFileSync(inputListFilePath); - const links = JSON.parse(inputLinksAsString); - const list = JSON.parse(inputListAsString); - - // process file - // const sources = links.map((link) => link.source); - // const targets = links.map((link) => link.target); - // const list = [...new Set([...sources, ...targets])]; - - // Merge sort - let mergeSortOutput = mergeSort({ list, links }); - // console.log("Output: "); - console.log("Sorted output: "); - console.group(); - console.log(mergeSortOutput.map((x) => x.name)); - console.groupEnd(); - console.log(""); - - // find Paths - let nodes = mergeSortOutput.map((element, i) => ({ - ...element, - position: i, - })); - const linksWithPosition = links.map((link) => ({ - ...link, - sourceElementPosition: findElementPosition(link.source, nodes), - targetElementPosition: findElementPosition(link.target, nodes), - })); - let paths = await findDistancesFromAllElementsToAllReferencePoints({ - nodes, - links: linksWithPosition, - }); - // console.log(JSON.stringify(paths, null, 4)); - - // Aggregate paths. - let aggregatedPaths = aggregatePaths(paths, nodes); -} -main(); +export const mergeSort = mergeSort; +export const findPaths = findDistancesFromAllElementsToAllReferencePoints; +export const aggregatePaths = aggregatePaths; diff --git a/packages/utility-tools/src/process-input.js b/packages/utility-tools/src/process-input.js new file mode 100644 index 0000000..4c1094c --- /dev/null +++ b/packages/utility-tools/src/process-input.js @@ -0,0 +1,60 @@ +// IMPORTS +import * as fs from "fs"; +import { mergeSort } from "./mergeSort.js"; +import { findDistancesFromAllElementsToAllReferencePoints } from "./findPaths.js"; +import { aggregatePaths } from "./aggregatePaths.js"; + +// DEFS +const inputLinksFilePath = "./input/input-links.json"; +const inputListFilePath = "./input/input-list.json"; +const outputFilePath = "./output/output.json"; + +// HELPERS + +const findElementPosition = (name, orderedList) => { + let node = orderedList.find((node) => node.name == name); + return node.position; +}; + +// MAIN +async function main() { + // read file + const inputLinksAsString = fs.readFileSync(inputLinksFilePath); + const inputListAsString = fs.readFileSync(inputListFilePath); + const links = JSON.parse(inputLinksAsString); + const list = JSON.parse(inputListAsString); + + // process file + // const sources = links.map((link) => link.source); + // const targets = links.map((link) => link.target); + // const list = [...new Set([...sources, ...targets])]; + + // Merge sort + let mergeSortOutput = mergeSort({ list, links }); + // console.log("Output: "); + console.log("Sorted output: "); + console.group(); + console.log(mergeSortOutput.map((x) => x.name)); + console.groupEnd(); + console.log(""); + + // find Paths + let nodes = mergeSortOutput.map((element, i) => ({ + ...element, + position: i, + })); + const linksWithPosition = links.map((link) => ({ + ...link, + sourceElementPosition: findElementPosition(link.source, nodes), + targetElementPosition: findElementPosition(link.target, nodes), + })); + let paths = await findDistancesFromAllElementsToAllReferencePoints({ + nodes, + links: linksWithPosition, + }); + // console.log(JSON.stringify(paths, null, 4)); + + // Aggregate paths. + let aggregatedPaths = aggregatePaths(paths, nodes); +} +main();