Data transform in Node.js, best practices? [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have a need of transforming various message formats into JSON and v.v., e.g. XML to JSON and then JSON to XML or EDI to JSON and JSON to EDI.
I've looked at several different XML-to-JSON modules and they seem mostly to be into a direct conversion into their own JSON and/or XML format and not into my required XML (e.g. UBL 2.1).
One easy straight forward way of going about it is to just use a String variable:
let myXML = '<root><hdr>' + jsonIn.hdr + '</hdr>\r\n';
myXML += '<itm>' + jsonIn.item[0] + '</hdr></root>';
The myXML variable will be quite big though. Up to 200 kB currently but can grow bigger in the future.
Obviously this is the quickest and easiest way of creating the outbound formats but it doesn't really feel right to create a massive String variable...
In Java I'd use StringBuilder and there is a npm for Node: https://www.npmjs.com/package/stringbuilder
Which approach would you consider the "best practice" approach?

In Java I'd use StringBuilder...
If that's the case and so you do in the end need to end up with a single string containing the result, a fairly normal pattern is to build up the individual strings in an array, and then use Array#join when you're done to produce that one big final string:
let myXML = [];
myXML.push('<root><hdr>' + jsonIn.hdr + '</hdr>\r\n');
myXML.push('<itm>' + jsonIn.item[0] + '</hdr></root>');
// ...
// When you're ready for the big string:
myXML = myXML.join("");
If you don't need a big string at the end, but are writing to a file, etc., writing as you go would tend to be a good solution.

Related

JS vs JSON for config in Node js [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
What should I use for configuration ?
Some module like KrakenJS highly supports configuration through JSON and the blog https://blog.risingstack.com/node-js-best-practices-part-2/ say JS should be preferred over JSON.
Can you tell me how they differ and what's the best way to manage them ?
You should accept both.
JavaScript configuration files have many advantages:
programmable of course, which allows for interesting extensions and for generated parts
lighter and less awkward to type (less quotes, trailing commas, etc.)
lets you have comments (a big limitation of JSON, here's why they're needed)
more value types (NaN & infinites, regular expressions for example)
In this case the JS file exports a plain JS object, similar to what you would have had as a result of a parsed JSON file.
JSON is more socially accepted, because JS isn't often thought as a configuration format. Letting people use JSON will prevent a "JS is for logic" sterile debate and there's no problem if people are happy with it.
Accepting both formats:
Here's how reading the JS/JSON configuration can be done:
try {
config = require('./config.js');
} catch(err) {
if (err.code==='MODULE_NOT_FOUND') {
config = require('./config.json');
} else {
console.error('config.js loading failed');
throw err;
}
}
In any case, the JSON configuration file can be turned to a JS one by just prefixing it with
module.exports =
so nobody's locked in that format.

Return join vs return string [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I saw this example and wondered why anyone would do it:
function a(str) {
return [
'<div>',
str,
'</div>'
].join('');
}
Isn't it an equivalent to the following code and what's the advantages / disadvantages of using just:
function a(str) {
return '<div>' +
str +
'</div>;
}
Thank you.
They're the same thing. Some experiments have shown using the + operator is faster but this will vary between browsers. Not to mention, these kinds of micro-optimizations don't tend to contribute much.
So which one is better? Whichever one you like the most.
My preference would be to use the "string concatenation" version, in other words, your second one.
They both return the same thing. In the case of the first example however your combining string elements of an array together. In the second one, you're simply adding the strings together.
There's no need to use an array to solve this problem, and the second one is much more concise too. It's a better web development practice.
Performance wise, there will be a slight improvement with the second one. Since it has less space, that means the JavaScript file that loads it will be smaller, taking up fewer resources on a user's browser.
It will also make it more easily maintainable with fewer lines of code.

Implementing a JSON parser [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
This question is independent of implementation language.
In a recent interview I was asked to write a JSON parser :
The input I was given was something like below:
{
'key1': 'value1',
'key2': [key21, key22]
}
As simple as it may sound, I was stumped as I did not know how to write the parser (btw I am aware of JSON.parse() methods).
The question was to write your own parser.
It matters little that the above JSON is not in the right format. The parse should throw an error if it is not.
Can someone point me towards some technique I could have used to solve this problem.
Basically, you'll first have to scan through your string character by character in order to separate it into tokens.
{ and } are tokens. Let's call them 'START_OBJECT' and 'END_OBJECT';
[ and ] are also tokens. Let's call them 'START_ARRAY' and 'END_ARRAY';
: and , are tokens, too. Let's call them 'COLON' and 'COMMA';
'key1', 'key2' and 'value1' are 'STRING_CONSTANT' tokens, whose values are the strings themselves;
key21 and key22 are IDENTIFIER tokens, whose values are 'key21' and 'key22'
That initial part of the parsing process, where you break your source into tokens is called 'lexical analisis', and is the first step in the parsing process.
The next step would be the 'syntactical analysis', where you would figure out the actual structure of the source from the sequence of tokens.

Concatenated Domain Name in JavaScript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've taken over the development of a project and have noticed a weird snippet in the JavaScript where the developer wrote the following:
/* ... code */
var el = document.getElementById('foo');
el.href = "http://" + "w" + "w" + "w" + "." + "d" + "o" + "main.com/foobar/";
/* ... code */
I have some hunches as to what the purpose is, but will refrain from expressing it so as to not misguide, probably better, answers ...
What is the purpose of concatenating the domain?
In terms of JavaScript itself, this has practically no effect - the result is the same.
But the reason may be different than to accomplish some task in JavaScript. I guess there are two possibilities that are most likely to be the case here:
To mislead other programmers (so the domain name is not easily found by simple text search). Similar (but a lot more complex) ways are used by worms to insert code into the website without showing what it contains, unless you will put a lot more effort to analyse it.
To try to mislead crawlers, which probably assumes they are not parsing the JavaScript and getting actual result. It may be the case for example if the programmer feared that the code will be eg. indexed and by searching this domain name in the search engine, anyone can find out it was mentioned in the code of the site you are describing.
Well normally you would be concatenating this because you would use variables within it...
el.href = "http://www." + domain + "." + ext "/" + additionalUrl;
Otherwise separating letter by letter like that serves no purpose. I'm assuming the previous programmer was just bored :)

How to check which part of page is an article? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I would like to create a similar tool to Instapaper or Readability and I wonder what is the best way to find and get text from a web page. Do you have any ideas?
The question is too broad to give a concrete answer to, but you can separate this question into three concerns:
A way to grab web resources. libcurl for example, or just about anything able to talk HTTP.
A DOM parser. Python has xml.dom.minidom, for example.
An algorithm for traversing the DOM tree and extracting text. Be it scanning for elements with class=article, or <div>s with more than 1024 characters etc., is entirely up to you. You will need experimentation to get this right.
I suggest asking separate questions for each of these concerns. After doing research on each, of course. :)
Here is an idea to get you started in Ruby. Just tested the code below and it is working fine for me. Have a look it might help you.
require 'open-uri'
require 'cgi'
require 'nokogiri'
$url='http://www.stackoverflow.com'
$txt_file = open($url)
$raw_contents = $txt_file.read
$html = Nokogiri::HTML(CGI.unescapeHTML($raw_contents)).content
#strip the web page fetched out of all hmtl tags and encoded chars
$txt_file = File.new('c:\ruby193\bin\web-content\stack.txt', "w")
#stack.txt now contains a stripped, pure txt file which you can manipulate further
$txt_file.write($html)
$txt_file.close
puts 'Here is the stripped text of your webpage\n'+$html

Categories

Resources