d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Javascript Node Problem: Merge And Split Textfiles > 100fg
Add Reply New Topic New Poll
Member
Posts: 14,925
Joined: Jan 3 2008
Gold: 135,879.75
Nov 17 2019 04:55pm
Part 1:

Given a folder of .txt documents, merge them into a single .txt document.

Part 2:

Given a .txt document, split each truthy line into its own .txt document.





Please do not pm me the answer, just post it in here. Payout will go to the first working answer.


This post was edited by kdog3682 on Nov 17 2019 04:55pm
Member
Posts: 2,619
Joined: May 21 2004
Gold: 21,934.00
Nov 25 2019 09:48am
Do you need fully functional code or are you just looking for the fp needed for concatting/ splitting lines and docs?

Any restrictions for the nodejs sever setup?

Also what are the criteria for a "truthy" line?

This post was edited by frixionburne on Nov 25 2019 09:58am
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Nov 25 2019 05:25pm
Quote (frixionburne @ Nov 25 2019 10:48am)
Do you need fully functional code or are you just looking for the fp needed for concatting/ splitting lines and docs?

Any restrictions for the nodejs sever setup?

Also what are the criteria for a "truthy" line?


https://developer.mozilla.org/en-US/docs/Glossary/Truthy
Member
Posts: 2,619
Joined: May 21 2004
Gold: 21,934.00
Nov 25 2019 09:08pm
Quote (waraholic @ Nov 25 2019 07:25pm)


While correct for the term "truthy" in js, no one runs a text document of biz data that would ever evaluate to false because any string is "true", so every line is truthy.

So unless its consuming a text file of literally random shit to evaluate as true or false in js, as well as properly casting non quoted strings, this makes no sense.
Member
Posts: 14,925
Joined: Jan 3 2008
Gold: 135,879.75
Nov 26 2019 11:59am
Quote (frixionburne @ Nov 25 2019 10:08pm)
While correct for the term "truthy" in js, no one runs a text document of biz data that would ever evaluate to false because any string is "true", so every line is truthy.

So unless its consuming a text file of literally random shit to evaluate as true or false in js, as well as properly casting non quoted strings, this makes no sense.


Replace truthy with no blank lines.

So:

abc
def

ghi

The third line would be omitted.

This post was edited by kdog3682 on Nov 26 2019 12:00pm
Member
Posts: 2,619
Joined: May 21 2004
Gold: 21,934.00
Nov 26 2019 12:48pm
Quote (kdog3682 @ Nov 26 2019 01:59pm)
Replace truthy with no blank lines.

So:

abc
def

ghi

The third line would be omitted.


That makes more sense. Is this work still needed?

Also do you want two separate scripts the two parts, or one script with some command args to switch modes?

This post was edited by frixionburne on Nov 26 2019 12:49pm
Member
Posts: 2,619
Joined: May 21 2004
Gold: 21,934.00
Nov 26 2019 01:43pm
Got bored, pretty sure this is what you're asking for. If there are any issues its easy to fix, and are a result of the somewhat vague reqs.

Code

#!/usr/bin/env node

// USAGE

// document merge mode:
// node . -m 1 -s "/path/to/my/data/dir" -o "/path/to/my/out/dir"
// document truthy split mode:
// node . -m 2 -s "/path/to/my/data/document.txt" -o "/path/to/my/out/dir"

const yargs = require("yargs");
const fs = require("fs");

const options = yargs
.usage("Usage: -m <mode> -s <source> -o <output_dir>")
.option("m", {
alias: "mode",
describe: "Execution mode. 1 for doc merge, 2 for truthy split.",
type: "string",
demandOption: true
})
.option("s", {
alias: "source",
describe: "Source Directory or file",
type: "string",
demandOption: true
})
.option("o", {
alias: "outDir",
describe: "Output Directory",
type: "string",
demandOption: true
}).argv;

if (options.mode === "1") {
console.log("Reading from directory: " + options.source);
console.log("Starting Document Merge");

const files = fs.readdirSync(options.source);

let outputContents = "";
files.forEach(file => {
console.log("Merging document: " + file);
const content = fs.readFileSync(options.source + "/" + file, "utf8");
outputContents = outputContents.concat(content);
});

console.log(
"Writing output document: " + options.outDir + "/mode_1_result.txt"
);

fs.writeFileSync(options.outDir + "/mode_1_result.txt", outputContents);

console.log("Output written. Done.");
} else if (options.mode === "2") {
console.log("Reading from file: " + options.source);
console.log("Starting Truthy Split");

const lineReader = require("readline").createInterface({
input: require("fs").createReadStream(options.source)
});

console.log("Reading lines...");

let count = 0;
lineReader.on("line", function(line) {
if (line && line !== "") {
console.log(
"Writing output document: " +
options.outDir +
"/mode_2_" +
count +
"_result.txt"
);
fs.writeFileSync(
options.outDir + "/mode_2_" + count + "_result.txt",
line
);
count++;
console.log("Output written.");
}
});
} else {
console.error("Invalid Mode.");
}



Member
Posts: 14,925
Joined: Jan 3 2008
Gold: 135,879.75
Nov 27 2019 06:19am
Quote (frixionburne @ Nov 26 2019 02:43pm)
Got bored, pretty sure this is what you're asking for. If there are any issues its easy to fix, and are a result of the somewhat vague reqs.

Code
#!/usr/bin/env node

// USAGE

// document merge mode:
// node . -m 1 -s "/path/to/my/data/dir" -o "/path/to/my/out/dir"
// document truthy split mode:
// node . -m 2 -s "/path/to/my/data/document.txt" -o "/path/to/my/out/dir"

const yargs = require("yargs");
const fs = require("fs");

const options = yargs
.usage("Usage: -m <mode> -s <source> -o <output_dir>")
.option("m", {
alias: "mode",
describe: "Execution mode. 1 for doc merge, 2 for truthy split.",
type: "string",
demandOption: true
})
.option("s", {
alias: "source",
describe: "Source Directory or file",
type: "string",
demandOption: true
})
.option("o", {
alias: "outDir",
describe: "Output Directory",
type: "string",
demandOption: true
}).argv;

if (options.mode === "1") {
console.log("Reading from directory: " + options.source);
console.log("Starting Document Merge");

const files = fs.readdirSync(options.source);

let outputContents = "";
files.forEach(file => {
console.log("Merging document: " + file);
const content = fs.readFileSync(options.source + "/" + file, "utf8");
outputContents = outputContents.concat(content);
});

console.log(
"Writing output document: " + options.outDir + "/mode_1_result.txt"
);

fs.writeFileSync(options.outDir + "/mode_1_result.txt", outputContents);

console.log("Output written. Done.");
} else if (options.mode === "2") {
console.log("Reading from file: " + options.source);
console.log("Starting Truthy Split");

const lineReader = require("readline").createInterface({
input: require("fs").createReadStream(options.source)
});

console.log("Reading lines...");

let count = 0;
lineReader.on("line", function(line) {
if (line && line !== "") {
console.log(
"Writing output document: " +
options.outDir +
"/mode_2_" +
count +
"_result.txt"
);
fs.writeFileSync(
options.outDir + "/mode_2_" + count + "_result.txt",
line
);
count++;
console.log("Output written.");
}
});
} else {
console.error("Invalid Mode.");
}


Thanks.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll