I'm a beginner in Node.js and also in programming. Perhaps I miss something very basic. I have two scripts which have to talk with each other in the command line. The first script gives numbers and the other makes some math and returns the answer. The first script was written by someone else and I don't know what is there. Te second script is mine. It reads the numbers in stdout and writes the answer in stdin. The first script "sees" the answer only after process.stdin.end(). But I have to read and write multiple times. What should I do?
My code looks like this:
var process = shell.exec('node first.js', {async:true});
process.stdout.on('data', function(data) {
process.stdin.write("" + evalStr(data) + "\n");
process.stdin.end();
});
Use process.stdin.write(data + "\n") instead of using evalStr().
Related
I use winston to perform logging currently and have written a common method for it to be used all over project. Problem is, many of logging statements are like, logger.info("here is the data" , data)
With comma as concatenator, i couldn't log data in console. data can also be a content containing comma so I wouldn't be able to just use replace ',' by '+' .
My idea is regex can be like, if text starts with ' or " and its next character is ',' at end of quotes, replace with '+'
Ain't sure if it would be right but still, please help with your suggestions.
Perhaps you can monkey-patch it with something like
logger.info = ((infoFunc) => {
// create the patched info that concatenates all arguments
// before calling the original logger.info
let patch = () => {
// put any logic here that you need to achieve the desired result.
var message = args.join('');
infoFunc(message);
};
return patch;
})(logger.info);
Not tested
Just make sure it's run right after the logger is set up.
This will work as a quick fix to get things running but I wouldn't recommend leaving it in your code and should be removed once all calls to logger.info have been cleaned up.
Is there any way to make js-beautify handle the javascript "//" comments?
Example
socket.on('user_online', function(data){
document.getElementById("online").innerHTML = "ONLINE USERS (" + data + ")";
});
//Add the online users to the list one by one
chatCount = 0;
socket.on('user-list-append', function(data){
will become this
socket.on('user_online', function(data){
document.getElementById("online").innerHTML = "ONLINE USERS (" + data + ")";
});
//Add the online users to the list one by one chatCount = socket.on('user-list-append', function(data){
They don't automatically make a new line after the comment since maybe because they don't know where the comments ends.
I could simply replace "//" with "/* ... */", But i'm just curious if there is a better way to solve this problem like maybe they have some options to apply
Update #1:
lib: https://github.com/beautify-web/js-beautify/tree/master/js/lib
Try put copy paste the code below in http://jsbeautifier.org/ and you can see what I mean.
socket.on('user_online', function(data){ document.getElementById("online").innerHTML = "ONLINE USERS (" + data + ")"; }); //Add the online users to the list one by one chatCount = 0; socket.on('user-list-append', function(data){
Is there any options to use to let the lib know where to make a new line after "//" comments for the JavaScript that are Word Warped or Minified?
Your first code block and the one you suggest we copy from to test (the third one), do not contain the same code. The carriage return is a very important character when interpretting comments that start with '//'.
This isn't really a question about beautify-js. The real question here is how did you create that third block of code? You can't just run all the lines together and expect it to work.
The third block of code is NOT valid javascript. If you had taken the first block of code and used, say uglify-js, on it, it would not have comments in it anymore. There is quite a bit of interpretation that needs to happen to be able to run js altogether on one or a few lines. Other things it would consider would be lines that end only with a carriage return would need to have a semicolon inserted, etc.
What beautify-js did in this particular case is exactly right. Specifically, when '//' is encountered all the rest of the characters to the end of the line must be interpretted as a comment. This is exactly what it has done and that is how javascript is supposed to be interpretted.
Mongodb is cool enough to create the database/collection on the fly, if we run a code similar to
db.store.save({a: 789});
It automatically creates store collection and add a document to it.
My javascript understanding says it is not possible to call a method on an undefined property of db object. It should have resulted in some kind of error/exception.
I am curious to understand the happenings behind the scene and if there is any helpful link please point me to those. Googling did not help me much.
In JavaScript there is a way to define a function that will be executed when an undefined method is called.
Example:
var o = {
__noSuchMethod__: function(id, args) { console.log(id, '(' + args.join(', ') + ')'); }
};
o.foo(1, 2, 3);
o.bar(4, 5);
o.baz();
// Output
// foo (1, 2, 3)
// bar (4, 5)
// baz ()
Note this is a non-standard feature and today only works in Firefox.
I do not know how MongoDB implemented this feature, but I'm just responding in order to report that can be done this way.
Fot more details see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/noSuchMethod
As I recall in a NodeJS environment you must do something like this to actually create a record: db.get('collectionName').insert({..something...}); or db.get('collectionName').save({...something...}); but you don't get to use the collection name as a property of db.
The line you're mentioning is only used in MongoDB shell, which is not Javascript.
I guess you're misunderstanding what's MongoDB shell and what's a MongoDB driver.
So long story short MongoDB (driver) is not able to access an undefined property.
EDIT
In response to your comment..
MongoDB JS driver's GitHub page pretty much points out how to insert a field and always uses the syntax I mentioned: https://github.com/mongodb/node-mongodb-native
As for what you're using in the shell it's pretty clear that you can't just use Javascript in a command shell. So I guess I'll point you to a place in which you can see in what language was MongoDB developed: http://www.mongodb.org/ pretty much the first line says it's written in C++.
Hope this helps clarify your question
i am facing the problem that i have to translate a larger html and javascript project into several languages. The html content was no problem, but the numerous javascript files are problematic, since i was a bit lazy during the development process. For instance, if i needed a message text, i just added it in the concerning position.
My approach now is, that i am using a build-in file search (Eclipse) for every occurrence of " and ', which i am getting line-wise. This would be extremely time consuming and errors are unavoidable.
Here are some examples that occur in the files:
var d = "Datum: " + d.getDate()+"."+(d.getMonth()+1)+"."+d.getFullYear();
showYesNoDialog("heading text","Are you sure?",function(){};
Sometimes i am mixing " and ', sometimes a string goes over several lines:
var list="";
list+='<li data-role="list-divider">';
list+='Text To Translate';
list+='</li>';
Things i don't want to get, are jquery selectors, e.g.:
$("input[name^=checkbox]").each(function () {};
Do you see any time saving method to get all of the strings that i would like to translate?
Regex? A java interpreter? Grep?
I know, that is a bit unusual question.
So any suggestion would be great.
Thanks in advance!
It is better to use some kind of the lexical scanner that converts the code into the tokens and then walk over the list of tokens (or syntax tree). There is a number of such tools (I even created one of them myself - here you can find some of the examples https://github.com/AlexAtNet/spelljs/blob/master/test/scan.js).
With it you can scan the JS file and just iterate over the tokens:
var scan = require('./..../scan.js');
scan(text).forEach(function (item) {
if (item.str) {
console.log(item);
}
});
Since this is for a homework assignment I do not want to post my code because it essentially gives a solution. I can post some generic snippets. I am going to start off by saying I am new to javascript and Mongo, and basically learned them in a few hours last night.
Basically I have code that when I paste into the shell, it works perfectly, but when I save it into the database and try to execute it does not work. Here is a basic example.
db.system.js.save(
{
_id: "istrue",
value: function (x){
if(x == true)
print("true");
else
print("false");
}
})
So if I copy and paste this code and set var x = true or var x = false first then it works, but if I do this:
db.eval("istrue(true);");
Then it doesn't work.
Any ideas?
I can't find any documentation on this behavior, but a little testing reveals that your code is running just fine, but either stored functions can't use print or the output of print goes somewhere other than stdout. If you return strings instead of printing them, you'll see what you would expect.
I suspect the output is going to the MongoDB logs, but I'm not sure where my install put them.