Perform a 3-way text merge in Node using javascript? - javascript

Does anyone know an efficient library or algorithm to automatically perform a best guess 3-way text merge in Node? I suppose that would be performing a rebase? (not sure if that is the right terminology).
I have two files modified from one root file -- a BASE a 1st File and a 2nd File, and I need to combine them together. It will throw an error if there is a conflict, or return the combined text if not. I looked around but could not locate a suitable library.

Related

How can I check for repeating lines in a text file with node.js?

I am creating a node.js program which scans through a log file and outputs information from it to a console.
Sometimes, the log file can contain errors which can repeat basically forever (I'm talking like 20000 times).
I need a way to check if any portion of text is repeated multiple times in the file.
Since I don't know what text I'm looking for, I can't use native JS functions, regex, or stuff like that.
Does anyone know how I could achieve this without using machine learning?
I have not tried anything yet because I have absolutely no clue how this could be achieved.
Break the problem up into multiple steps. Deal with one step at a time. So, for step one, your task is to figure out how to read a file from disk into a variable. Next step: turn that variable into an array. etc.
You can use an algorithm something like this:
Read the log file into memory. (If the log file is too large, or if step 2 will be too large, research breaking up this task into multiple parts)
Turn the log file into an array of discrete pieces of text (therefore, you need to know what separates the discrete pieces of text).
Now you need an (empty) output array.
Loop through your input array and, for each array element, check if it is already in the output array. If not, add it. If yes, do nothing.
At the end, you will have an output array consisting only of unique log entries. Write it out to a file.

Node.js / JS - direct csv data manipulation?

I know it can be done in other languages like Powershell, I did a lot of searching but couldn't find any how it can be done in node.js or javascript.
For example let's say I have:
carModel,price,color
"Audi",10000,"blue"
"BMW",15000,"red"
"Mercedes",20000,"yellow"
"Porsche",30000,"green"
and I want to append to line 3 something like:
carModel,price,color,errorcode,errormsg
"Audi",10000,"blue"
"BMW",15000,"red","05","wrong price"
"Mercedes",20000,"yellow"
"Porsche",30000,"green"
My question isn't really about how to solve the above, but:
Is there any way to manipulate a csv file in js directly without converting it to json objects and converting it back to csv?
Is there any way to manipulate a csv file in js directly...
Sure, you could read it all into memory as one big string, then splice content into the string. It probably wouldn't be a great idea, though, as compared to at least reading it in as an array of strings or an array of arrays.
Also note that reading a CSV can be more work than it seems thanks to line breaks within quoted fields, etc., so you're probably best off using one of the CSV parser npm modules...

i18n in React with .properties files

I am in the middle of (large)app rewrite into Reactjs-redux and internalization is next problem.
Ive been looking at some currently available libraries (redux-react-i18n , 18n-react) but none of them seems to fit.
Why ? Because my localized strings are stored in separate .properties files and this cannot be changed. But there is a possibility generate whatever format from this in compile time
Example en_US.properties:
key1=This is a constant string
key2=This is a string with {parameter}
....
and similar with de_de.properties file and so on
Also language can be change only on page refresh so this is making it little bit easier
My question is how to approach this problem. My first naive approach is generate some static constant js object available in app globaly but im feeling thats against javascript best practises also no idea how to deal with parametrized strings
As im fairly new to javascript id like to hear any ideas
In case somebody has same problem
I ended up writing script converting .properties files into json files
Then in React code i created HOC component wich gets keys(or namespaces depends on how you organize your json files) as parameter and fetches values from server
These keys are usually for whole page but sometimes also for single component if it makes sense
All it takes is one more HTTP request you can also cache result
Hop it helps

Alternative to Node for Merging External Properties of topojson

I have already looked here, here, and here:
D3.js: How to combine 2 datasets in order to create a map and show values on.mouseover?
How to add properties to topojson file?
https://github.com/topojson/topojson-1.x-api-reference/blob/master/Command-Line-Reference.md
They were a great help in getting me as close as I am now. However, I'm attempting to recursively parse several datasets in .tsv format and join them together with the json geometry data. I use the word join loosely; I know that topojson has some command line abilities to join .tsv to the json file. I think that would simplify things a lot, but if I'm not mistaken that requires Node. I tried many times to get Node working on my computer, it is either a compatibility issue or I'm not adept enough.
So, simply put, my question is:
Bearing in mind that I don't have Node, what changes do I need to make (data file related, or other) to get my block working (parsing dynamically)
I'm assuming that it's not working because of my chaotic data structure (as suggested by the post title), but if you think its something else, let me know.
My block:
http://bl.ocks.org/diggetybo/42f4534b2168a00694219888ebb1a2fb

ternJS - Generate JSON type definition file

ternJS have several. JSON files defs which contains the definition of librarys. Can someone explain to me how I can best generate my own to my javascript libraries / or only definition objects?
I can not see that there is no common procedure for this?
There's a tool for this included in Tern. See condense at http://ternjs.net/doc/manual.html#utils . It runs Tern on your file and tries to output the types that it finds. It's far from flawless, but for simple programs it works well. For files with a complicated structure or interface, you'll often have to hand-write the definitions.
There are three ways I have thought about to solve your problem:
Using Abstract Syntax Tree Parser and Visitor
One way to solve your problem would be to use abstract syntax tree parser and visitor in order to automate the task of scanning through the code and documenting it.
The resources here will be of help:
-http://ramkulkarni.com/blog/understanding-ast-created-by-mozilla-rhino-parser/
-What is JavaScript AST, how to play with it?
You usually use a parser to retrieve a tree, and then use a visitor to visit all the nodes and do your work within there.
You will essentially have a tree representing the specific library and then you must write the code to store this in the def format you link to.
Getting a Documentation Generator and Modifying
Another idea is to download the source code for a documentation generator, e.g. https://github.com/yui/yuidoc/
By modifying the styling/output format you can generate "documentation" in the appropriate json format.
Converting Existing Documentation (HTML doc) into JSON
You can make a parser that takes a standard documentation format (I'm sure as Javadoc is one for java there should be one for javascript), and write a converter that exctracts the relevant information and stores in a JSON definition.

Categories