Get the headers from a CSV file (and only that line) - javascript

How do I read the first non-empty line from a text file, in plain javascript, possibly by using a new FileReader()?
I wish to get only the first non-empty line, not the whole text in the file (which might be enormous). By "non-empty line" I mean a line ending with \r\n and containing some non-blank char.
One of my practical goals could be to pick the first "good" line in a huge CSV file, to possibly get the "headers" (names of variables of the dataset).
The file I wish to read is now locally on my hard disk, but when the script is ready, I would also like to put it online on my domain along with the script to process it.
Thank you!

I think you want something kinda like this:
fs = require('fs')
fs.readFile('/testFile.csv', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var firstLineRegEx = /^(.*)$/m
var csvHeader = firstLineRegEx.exec(data)
});

Assuming you want to stick to client-side/in-browser JavaScript (as opposed to node.js), there's not really a way to open a file and read it line-by-line; that would require a level of direct filesystem access that's simply not available. You can certainly process it line-by-line, once it's fully-loaded and you've split it into lines.
If you want to be sure that you only ever load the first line in the browser, you'll need some server-side code for that. Node.js may or may not be a good option, depending on your existing web site.
The FileReader API you mentioned looks like it's geared towards handling arbitrary, user-provided files, which is probably the wrong direction if you'll always be in control of what file to load.
You've actually got two, distinct problems here: how do I load only part of a file in client-side JS, and how can I find the header line in a string containing CSV formatted data? (You could probably break this down differently, but it's multiple parts regardless.)

Related

How to append data to txt in javascript?

I'm writing a program, which needs to take some JSON from text file, then modify it and put it back to the same file, something like overwriting. I don't have problem with reading from file and modifying data, but I was searching long and found nothing about writing into files. If it's important - it's server-side file.
Ex. text file: {"name":"John","age":20}
Result which I need: {"name":"John","age":21}
You can use the fs module in node to write to a file!
information is located here on it https://www.w3schools.com/nodejs/nodejs_filesystem.asp
The two methods you will want to look at is .writeFile or .appendFile depending on your specific need.

SyntaxError: Unterminated template literal when attempting to insert variable inside of path

I am attempting to have a Discord bot re-add all items from existing lists upon reinitialization, such that the titles of each list are saved in a preexisting local .txt file (e.g. AAA_filenames.txt), with items separated by semicolons (which the code successfully detects as individual parameters). Each title parameter is saved as another .txt file, and its items are appended to that file, once again, separating themselves by semicolons such that it can be read.
To my knowledge, all of this is completely successful and works perfectly, but the only problem I have is that I can't seem to put a template literal inside of a path (see paragraph 4 for an example path).
I've tried many things, most of which I've forgotten, but I can tell you that I've tried everything that both made sense to me and was on StackOverflow (to my knowledge).
Since I am using Discord Bot Maker (along with DBM-Mods) as my tool to format my code due to its efficiency and overall ease, I will be reluctant to release that code due to the fact that very few legitimate Discord bot creators use it (to my knowledge, of course), so for now, I will give you an example of the path that just doesn't work:
.\TXTSTORAGE\${tempVars("param-x")}.txt\
While I am aware that I am not supposed to simply post one line or an entire file, the startup initiative event is very long and its functions are spread throughout the entire file, so... my hands are tied here. Instead, I will tell you that all variables are stored correctly.
I've had the bot send back messages to the console and to a private Discord channel confirming that all variables do indeed exist and their values match up perfectly with what they should be.
I've also had the path point directly to the name of an existing .txt file that works the same way as the others in the same directory, that's all well and good, but it stops at one point because not all of the variables are in play when I do that; it must point at the variable-dubbed .txt file, which is fully dependent on which parameter AAA_filenames.txt has selected. So, that does confirm that the path itself can be read - however, it will refuse to read from a template literal, despite every other command on Discord Bot Maker allowing full access to template literals in this context.
I fear that it may have something to do with the raw code involved, but I couldn't find any issues with it. Maybe I've been working for too long at a time... but here's the raw text:
# ... rest of code
{
"filename": "C:\\Users\\Owner\\Desktop\\Sans Undertale\\Undewtale\\TXTSTORAGE\\${tempVars(\"param-x\")}.txt\\",
"storage": "1",
"varName2": "file-x",
"name": "Read File"
},
# ... rest of code
My expectations are listed in the first paragraph... actual results listed in title. Function refuses to read from path and carries on throughout the function until it's failed too many consecutive times. **So, here's what I'm looking for:
A vastly simpler way to force lists and their items to persist throughout resets without changing variable names
OR
A solution to this simple-looking, but quite possibly impossible issue.
My only condition: Discord Bot Maker-compatible, or at least compatible with a Node.js-exclusive export of the bot.**

Reverse a Javascript Build

I have a javascript build file, that is unminified; it contains prototype class definitions. Is there any tool i can use to break the build file into separate files, assuming one file per prototype/class definition?
This build file looks like the following, with many objects defined in the following way, all concatenated into the single file. I would like to break this file apart. I don't have access to the original source code, I was basically just given a dump in the form of this build file but its unmanageable in this form as its 10k+ lines of code.
MyClass = function(){
}
MyClass.prototype.foo = function(){
}
I would go with the suggested (in comments) do it manually manner, but if you insist to automate it some way, you can always use javaScript for the job.
One way would be to look up for constructors throw a regex like ([^\.=\s]+[^=]*=[\s]*function[\s]*[(]+[^)]*[)][\s]*[{]*) and extract all 'class'.prototype.'something' from the file, parse the entire file then write each group in separate files after doing any kind of ordering you would prefer.
Another manner would be to use a javaScript parser and group relevant function definitions throw token examination (this one is overkill, but might be interesting for learning purposes).

Generate a Word document in JavaScript with Docx.js?

I am trying to use docx.js to generate a Word document but I can't seem to get it to work.
I copied the raw code into the Google Chrome console after amending line 247 to fix a "'textAlign' undefined error"
if (inNode.style && inNode.style.textAlign){..}
Which makes the function convertContent available. The result of which is an Object e.g.
JSON.stringify( convertContent($('<p>Word!</p>)[0]) )
Results in -
"{"string":
"<w:body>
<w:p>
<w:r>
<w:t xml:space=\"preserve\">Word!</w:t>
</w:r>
</w:p>
</w:body>"
,"charSpaceCount":5
,"charCount":5,
"pCount":1}"
I copied
<w:body>
<w:p>
<w:r>
<w:t xml:space="preserve">Word!</w:t>
</w:r>
</w:p>
</w:body>
into Notepad++ and saved it as a file with an extension of 'docx' but when I open it in MS Word but it says 'cannot be opened because there is a problem with the contents'.
Am I missing some attribute or XML tags or something?
You can generate a Docx Document from a template using docxtemplater (library I have created).
It can replace tags by their values (like a template engine), and also replace images in a paid version.
Here is a demo of the templating engine: https://docxtemplater.com/demo/
This code can't work on a JSFiddle because of the ajaxCalls to local files (everything that is in the blankfolder), or you should enter all files in ByteArray format and use the jsFiddle echo API: http://doc.jsfiddle.net/use/echo.html
I know this is an older question and you already have an answer, but I struggled getting this to work for a day, so I thought I'd share my results.
Like you, I had to fix the textAlign bug by changing the line to this:
if (inNode.style && inNode.style.textAlign)
Also, it didn't handle HTML comments. So, I had to add the following line above the check for a "#text" node in the for loop:
if (inNodeChild.nodeName === '#comment') continue;
To create the docx was tricky since there is absolutely no documentation on this thing as of yet. But looking through the code, I see that it is expecting the HTML to be in a File object. For my purposes, I wanted to use the HTML I rendered, not some HTML file the user has to select to upload. So I had to trick it by making my own object with the same property that it was looking for and pass it in. To save it to the client, I use FileSaver.js, which requires a blob. I included this function that converts base64 into a blob. So my code to implement it is this:
var result = docx({ DOM: $('#myDiv')[0] });
var blob = b64toBlob(result.base64, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
saveAs(blob, "test.docx");
In the end, this would work for simple Word documents, but isn't nearly sophisticated for anything more. I couldn't get any of my styles to render and I didn't even attempt to get images working. I've since abandoned this approach and am now researching DocxgenJS or some server-side solution.
You may find this link useful,
http://evidenceprime.github.io/html-docx-js/
An online demo here:
http://evidenceprime.github.io/html-docx-js/test/sample.html
You are doing the correct thing codewise, but your file is not a valid docx file. If you look through the docx() function in docx.js, you will see that a docx file is actually a zip containing several xml files.
I am using Open Xml SDK for JavaScript.
http://ericwhite.com/blog/open-xml-sdk-for-javascript/
Basically, on web server, I have a empty docx file as new template.
when user in browser click new docx file, I will retrieve the empty docx file as template, convert it to BASE64 and return it as Ajax response.
in client scripts, you convert the BASE64 string to byte array and using openxmlsdk.js to load the byte array as an javascript OpenXmlPackage object.
once you have the package loaded, you can use regular OpenXmlPart to create a real document. (inserting image, creating table/row ).
the last step is stream it out to end user as a document. this part is security related. in my code I send it back to webserver and gets saved temporarily. and prepare a http response to notify end user to download it.
Check the URL above, there are useful samples of doing this in JavaScript.

What is the simplest way you can read from a file in JavaScript?

I know this question has asked before, but I'm still learning JavaScript, and I'm having trouble seeing through the complexity of other people's answers. I have a text file in the same directory as an HTML file that's reading JavaScript, and that text file literally has one line in it. I want to be able to grab that line out of the text file and put it in a string. What's a really simple way to do this that'll work in FF, IE, and Chrome, and is, aside from browser choice, is fairly universally valid? Again, I know this has been asked before, but I'm having trouble picking the real method here out of the complexity of example code I've seen elsewhere. Thanks!
Use jQuery get method http://api.jquery.com/jQuery.get/
$.get(url).success(function(data, status, response) {
var text = response.responseText;
// use your one line text stored in text variable here
}
The url variable can be relative, so you could put the text filename there. For example if your text file is called "mytext.txt" and in the same directory of the script accessing it you put:
$.get("mytext.txt").success(function(data, status, response) {
var text = response.responseText;
// use your one line text stored in text variable here
}
Note this answer assumes you are using http to access the text file and both script and text file is in the same domain.
This is tricky because most browsers by default do not permit JS to open files locally from the computer's file system. You can request the text file from a web server using ajax though. To do this I would recommend jQuery as it will be very "universally valid" as you put it. When doing ajax calls, the request must adhere to the same origin policy. In laymans terms, if you are at www.mysite.com you can request www.mysite.com/aTextFile.txt but you would not be able to request www.someothersite.com/aTextFile.txt
To do this with jquery, see momo's answer. I was going to type the same thing but he/she beat me to it.
The simplest way I can think of is to use a server-sided language to output the contents of the document into the page somewhere (such as an invisible textarea), and just have Javascript read that. No AJAX, no libraries, and it's really fast.
<textarea id="textarea"><?php include("test.txt") ?></textarea>
<script>
var str = document.getElementById("textarea").value;
</script>
Now, this isn't always the best way, but compared to using asynchronous Javascript everywhere with heavy frameworks at the expense of SEO performance...

Categories