nodeJS for each new line in a variable, add a hyphen? - javascript

I have a single string variable that when logged to console looks like the following with line breaks:
index
project.json
extras
I would like to be able to print it like:
-index
-project.json
-extras
what would be a good way to start on this? I am still in the process of learning JS, so I really have nothing to show for what I've tried, sorry.
I have tried some of the methods(console.log('-' + files_var)), but this does:
that does this:
- index
project.json
extras

You can replace all the line breaks with a line break and a - using .replace() passing a regex /...regex.../ and giving it a global so it replaces all instances. Remember the first string would not have a line break so we will need to add it manually.
console.log("-" + files_var.replace(/\n\r/g, '\n\r -'))
DEMO

Related

How to remove "debugger eval code" at the end of the output in console

I'm using Firefox 66.0.3.
When I execute the following snippet
[1,1,5,76,7,8,8,85,8,5,85,5,5,55].forEach(x => console.log(x))
or whatever code that has console.log() the browser shows debugger eval code at the end of the line in the output.
How to remove "debugger eval code" from output?
Unfortunately there is no way (at this date) to disable that but there
is a way for solving your problem
Define an array with one "\n" element:
temp = ["\n"]
Add each of your message with "\n" at end to array
temp.push(message+"\n")
Then log the array with spread operator
console.log(...temp)
now you log your whole messages with just ONE eval code
you can now easily select all the log without eval code because of first "\n" element
Join your list with line breaks:
console.log([1,1,5,76,7,8,8,85,8,5,85,5,5,55].join('\n'))
You can then safely copy the result without the debugger eval code strings.
I ran across this when trying to scrape some data I didn't want to try to copy and paste due to the other garbage that would have come with the data I wanted, so I will presume your case is similar.
If so, just use window.alert() instead of console.log() to prevent the extra characters.
The new code would become:
window.alert([1,1,5,76,7,8,8,85,8,5,85,5,5,55])
or
collection = [1,1,5,76,7,8,8,85,8,5,85,5,5,55]; window.alert(collection);

Replacing comma by plus in javascript for entire project

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.

How to add single quote "'" to javascript string

So many related questions out there, but none satisfyingly answered using javascript (no jQuery).
I wish to add quotes around a variable in a string I'm building. My string itself should contain single quotes within it like so:
'{'a'}'
I can get:
'{a}'
When I try to add single quotes around the key a I get:
'{\'a\'}'
I've used both of the following syntax with the same result.
Any suggestions??
concat('\'','a','\'')
'\''+'a'+'\''
See line 39 of this code: https://repl.it/#mike_butak/LiveForHisPleasureHeReallyIsThere
Thanks!
Like this?
console.log("'{'a'}'")
To expand on this, when you are building the string, just use " around the string, and ' within the string.
Having a hard time replicating your issue! See:
var temp = '\'{\'a\'}\'';
console.log('s' + temp + 's');
I'd definitely recommend demonstrating the issue you are asking about in a console print or in a readily available editor online before posting a question!
As per your comment, updating to make it part of a variable assignment. Still unclear what the issue is!

JQuery String Concatenation within a String

I'd like to preface this by saying I'm new to JQuery and this may be a simple question, but I was unable to find a solution after searching to the best of my ability.
I am trying to build a path to an image, where I am working with an API which returns an object that gives part of the path but not the base path.
Ex:
Base path = Youtube.com/watch/?
Path piece from API: /gdsrhab
On line 29 you can see I am trying to perform string concatenation within trying to build the "results" string. I understand why this is not working the way I've set it up, but am not sure how to syntactically perform this (if possible).
I've also tried to create two variables: baseURL and apiURL, concatenate them and save the result into completeURL then substitute it in, but it JQuery takes the string literal "completeURL" instead of substituting the value of the variable. Could someone point me in the right direction for how to get the full path within the tag? Thanks in advance for your help.
Picture of my JQuery code
In the following picture you can see the second half of the path is missing
The error message I receive
You are using ES6 string templates at start, so you only need to set your variable inside the ${}, you dont need to concate it, the string template will do it for you. So instead of
<img src = 'http...../' + '${movie.poster_path}'}>
that will output something like:
<img src="'http://yoururl.com/'+'mypath'"
you only need to do
<img src = 'http://yoururl.com/${movie.poster_path}'>
inside your string template

Vim errorformat multiline string

I'm trying to parse this error output for Flow. In the first error, it's the second file and line number that really matters, not the first. I would like to tell vim to use the second file entry. (So in this case, the location list should jump to source.js line 94, not line 20)
/Users/asdf/source.js:20:22,34:1: property hidden
Property not found in
/Users/asdf/source.js:94:10,106:3: object literal
/Users/asdf/source.js:25:14,18: identifier strin
Could not resolve name
Here's the error format currently defined for flow: It works except that it uses the first filename instead of the second.
let errorformat =
\ '%f:%l:%c:%n: %m,' .
\ '%f:%l:%c: %m'
Attempted Solution
I've been trying to use multi-line error formats, but I don't know what I'm doing. After reading :help errorformat, it seems like something like this should work, but it just loads the whole error into the message, with no file / line information. It also matches the second error using the first entry rather than the third.
let errorformat =
\ '%E%m,%C%m,%Z%f:%l:%c:%n: %m,' .
\ '%f:%l:%c:%n: %m,' .
\ '%f:%l:%c: %m'
Update
Thanks to lcd047, who pointed out that I'm not matching the correct output. Syntastic formats the error as follows, all on one line:
/Users/seanhess/projects/serials/web/app/model/source.js:20:22: property hidden Property not found in object literal (/Users/seanhess/projects/serials/web/app/model/source.js:94:10,106:3)
So, new question, how can I match the second location instead of the first? I think I'd prefer to ignore the first location.
Update Answer - this worked:
let errorformat =
\ '%.%#: %m (%f:%l:%c\,%.%#),' .
\ '%f:%l:%c:%n: %m,' .
\ '%f:%l:%c: %m'
You are not getting the file/line information because the %E specifies to use the rest of the line as the beginning of a multi-line message.
Try changing the "%Em" to "%E%f:%l:%c:%n: %m" for starters, since you basically want to keep all the original data being grabbed from the first version of errorformat.
You may want to change the "%Cm" to "%+Cm" to capture all of the lines in your error output.
The "%Z%f:%l:%c:%n: %m" looks good and should be used on the second line, since the %E will catch the first one.
Try that.

Categories