Skip to content

Instantly share code, notes, and snippets.

@mikemaccana
Forked from DMeechan/manipulating-arrays.js
Created February 26, 2020 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikemaccana/c77622b23d5c48ac79e325ae3dcbf858 to your computer and use it in GitHub Desktop.
Save mikemaccana/c77622b23d5c48ac79e325ae3dcbf858 to your computer and use it in GitHub Desktop.
Reading, manipulating, and writing arrays in JavaScript
// runTests();
const fs = require("fs");
const fileOne = "../exports/someA.csv";
const fileTwo = "../exports/someB.csv";
const fileThree = "../exports/someC.csv";
const outputFile = "../exports/someOut.csv";
const arrayOne = readFile(fileOne);
const arrayTwo = readFile(fileTwo);
const arrayThree = readFile(fileThree);
const combined = combine(arrayOne, arrayTwo);
// const duplicates = getDuplicates(arrayOne, arrayTwo);
// const uniques = getUniques(arrayOne, arrayTwo);
// const subtracted = subtractArray(arrayTwo, arrayOne);
writeFile(outputFile, combined);
/*
FUNCTIONS
*/
// Get everything in GroupA except GroupB items
function subtractArray(arrayOne, arrayTwo, silent) {
const lookup = getArrayLookup(arrayTwo);
const output = arrayOne.filter(value => typeof lookup[value] === "undefined");
const shouldPrint = !silent;
if (shouldPrint) {
console.log(
`✂️ Subtracted ${arrayTwo.length} users from ${arrayOne.length} and ended up with ${output.length}`
);
}
return output;
}
// Get everything in GroupA OR GroupB
function combine(arrayOne, arrayTwo, silent) {
const merged = [...arrayOne, ...arrayTwo];
const uniques = [...new Set(merged)];
const shouldPrint = !silent;
if (shouldPrint) {
console.log(
`🌍 Combined the two files (${arrayOne.length} and ${arrayTwo.length} items) to get ${uniques.length} items`
);
}
return uniques;
}
// Get everything in GroupA AND GroupB
function getDuplicates(arrayOne, arrayTwo, silent) {
const lookup = getArrayLookup(arrayOne);
const duplicates = arrayTwo.filter(item => !!lookup[item]);
const shouldPrint = !silent;
if (shouldPrint) {
console.log(
`💯 Compared ${arrayOne.length} vs ${arrayTwo.length} items and found ${duplicates.length} duplicates`
);
}
return duplicates;
}
// Get everything in GroupA XOR GroupB
function getUniques(arrayOne, arrayTwo, silent) {
const everything = combine(arrayOne, arrayTwo, true);
const duplicates = getDuplicates(arrayOne, arrayTwo, true);
const uniques = subtractArray(everything, duplicates, true);
const shouldPrint = !silent;
if (shouldPrint) {
console.log(
`🧬 Compared ${arrayOne.length} vs ${arrayTwo.length} items and found ${uniques.length} uniques`
);
}
return uniques;
// const lookup = getArrayLookup(arrayOne);
// const uniques = arrayTwo.filter(item => !lookup[item]);
}
// Convert an array into a map of value => count
function getArrayLookup(array) {
let lookup = {};
array.forEach(item => {
const exists = lookup[item] || 0;
lookup[item] = exists + 1;
});
return lookup;
}
function readFile(path) {
console.log("🤓 Reading file", path);
return fs
.readFileSync(path)
.toString()
.split("\n")
.map(
line =>
line
.trim() // remove white spaces from each line
.split(",") // split into columns
.map(e => e.trim()) // remove white spaces from each column
)
.map(line => line[0]) // get first column
.filter(line => line);
}
function writeFile(path, array) {
console.log("✍️ Writing file to", path);
let writeStream = fs.createWriteStream(path);
array.forEach(line => {
writeStream.write(line + "\n");
});
writeStream.end();
writeStream
.on("finish", () => console.log(`🚀 Done! Wrote ${array.length} rows`))
.on("error", error => console.error("🚨", error));
}
function compare(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
return [...new Set(arr1.filter(el => arr2.includes(el)))];
}
function runTests() {
const groupA = ["a", "b", "c", "d"];
const groupB = ["c", "d", "e", "f"];
const combined = combine(groupA, groupB); // expect a to f
const duplicates = getDuplicates(groupA, groupB); // expect c, d
const uniques = getUniques(groupA, groupB); // expect a, b, e, f
const subtracted = subtractArray(groupA, groupB); // expect a, b
console.log("");
assert("combined", ["a", "b", "c", "d", "e", "f"], combined);
assert("duplicates", ["c", "d"], duplicates);
assert("uniques", ["a", "b", "e", "f"], uniques);
assert("subtracted", ["a", "b"], subtracted);
console.log("\n⚡️ Done. Quitting...");
process.exit(0);
}
function assert(testName, expected, actual) {
const pass = compare(expected, actual);
if (pass) {
console.log(`✓ Test passed: ${testName}`);
} else {
console.warn(`⨯ Test failed: ${testName}`);
console.log(" Expected", expected, " but found ", actual);
}
return pass;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment