node.js axios Turkish character converter problem - javascript

The result: "Öncü"
I want it like this: "Öncü"
I fixed my problem by writing custom function like this:

Related

Prevent escape sequence interpretation in Javascript

I want to escape correctly a windows path that I get from cmd command.
for example when I tap where node in cmd, I will get a path like this :
C:\Users\mypc\AppData\Roaming\npm\ts-node
This is not a correct escaped string That I can pass between variable and it is not json validated.
I want to write a function that escape correctly this string.
const input = "C:\Users\mypc\AppData\Roaming\npm\ts-node"
const output = "C:\\Users\\mypc\\AppData\\Roaming\\npm\\ts-node"
I tried spliting the string, and many tries, but did'nt works.
When I console log the input, It will console like this, converting \n (in \node) as linebreak and \t in (\ts-node) as tab.
console.log(input)
/*
C:UsersmypcAppDataRoaming
pm s-node
*/
I find a pretty solution using String.raw like this :
const solution_1 = String.raw`C:\Users\mypc\AppData\Roaming\npm\ts-node`
console.log(solution_1)
// C:\Users\mypc\AppData\Roaming\npm\ts-node
The problem is that I can't pass a variable to String raw
const input = "C:\Users\mypc\AppData\Roaming\npm\ts-node"
const solution_2 = String.raw`${input}`
console.log(solution_2)
/*
C:UsersmypcAppDataRoaming
pm s-node
*/
I need a solution to pass a variable to String.raw or other technique to escape a string by a function.
You are confusing code and values. Code is what you write in your text editor. Obviously since source code in various programming languages have syntax and that syntax use the same characters that can appear in data there are some data that must be written differently in source code.
For example, if you want to write the data "Hello' in code you must either write:
let data1 = "\"Hello'"
or
let data2 = '"Hello\''
This is because the javascript language interprets the characters " and ' as part of its syntax. But let's be VERY CLEAR about this. Both data1 and data2 contain exactly the same data. That is, they both contain:
"Hello'
They don't contain \"Hello' or "Hello\'. You must understand this completely in order to not confuse yourself. Source code is not the same as the value of the data. Source code is just a way we write the data so that our programming language understand it.
When you do:
const input = "C:\Users\mypc\AppData\Roaming\npm\ts-node"
The value of input is:
C:UsersmypcAppDataRoaming
pm s-node
You need to write that data in a different way so that it will have the right value. You need to write:
const input = "C:\\Users\\mypc\\AppData\\Roaming\\npm\\ts-node"
The value of input is now:
C:\Users\mypc\AppData\Roaming\npm\ts-node
Let's remind ourselves again. This is the VALUE of the variable. The variable DOES NOT actually contain C:\\Users\\mypc\\AppData\\Roaming\\npm\\ts-node.
Now, here's another example. I'm creating a text file called "path.txt" and in it I write:
C:\Users\mypc\AppData\Roaming\npm\ts-node
Very carefully note what I'm writing in my text file. This is not a javascript source code. This is just data. I'm not writing C:\\Users\\mypc\\AppData\\Roaming\\npm\\ts-node because I'm not writing javascript here. I'm just writing data.
Now if I do this:
let data3 = fs.readFileSync('path.txt');
The value of data3 is:
C:\Users\mypc\AppData\Roaming\npm\ts-node
That's right. Javascript DOES NOT interpret the escape sequence AT ALL. Let me remind you again. Javascript does not interpret string escape sequence in data (eg, data you entered from the terminal or a file). It only does that in source code.
So. The answer to
I want to escape correctly a windows path that I get from cmd command.
Is you need to do nothing at all. There is no escaping being done by javascript.
But note: command shells like cmd.com or PowerShell on Windows or bash, dash, tcsh, fish etc. on Linux and Mac OS also have their own syntax. So in order to write the command correctly so that your shell (eg. cmd.com) will pass the correct data to your node.js process you need to understand your shell's syntax.
I don't use Windows so I'm not sure what you need to write on cmd.com to get it right but on Linux/Mac OS, assuming I have this script:
// testing.js
let data = process.argv[2];
console.log(data);
I can execute the script like this:
node ./testing.js C:\Users\mypc\AppData\Roaming\npm\ts-node
This is because bash (the shell I'm using) does not escape the \ character.
I only know the
console.log(JSON.stringify(input)), and the
console.log(util.inspect(input)) solutions, but the first backlash will disappear.
'C:UsersmypcAppDataRoaming\npm\ts-node'

Discord.js DiscordAPI Error: Unknown Emoji - Reacting to an Embed

Here's my code:
client.on('message', message => {
if (message.content.startsWith("!embed")) {
const embed = new Discord.MessageEmbed()
.setColor(0xffffff)
.setFooter(`Page 1`)
message.channel.send(':rewind::fast_forward:'); //this works
//but this doesn't
message.channel.send(embed).then(embedMessage => {
embedMessage.react(":rewind:");
});
});
I may have a hidden issue or not provided the correct parameters. I have looked at a similar previously asked question and tried implementing it, but it didn't work, may be outdated.
Instead, I receive an error:
UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Emoji
The emoji I'm using is provided in the standard emoji library, :rewind:
Discord.Js does not handle emojis with the Discord User Side :rewind: stuff. Discord.js is using unicode to send and receive emojis, except for custom emojis, IDs are used there instead.
This means that you should use the following code instead.
embedMessage.react("⏪");
The Unicode emojis can be found by "escaping" the emoji in Discord or using something like: https://getemoji.com/.
The escaping works by using \ in front of the emoji, you might know this from \n. By putting \:rewind: the result will look as follows.
When copying the Unicode emoji make sure to delete any spaces off of it.
you have to use unicode emojis instead, Here is an example:
embedMessage.react("⏪");

How to convert JSON escaped string into plain HTML-compatible string

I am using an API to compile code, and when there is an error, the response containing the error message uses JSON escape characters, but when outputting it back into the HTML front-end, it just produces garbage characters. How can I either convert the escaped string to a plain text string using Javascript, or output it in HTML correctly?
This is what the string looks like properly outputted (in Powershell):
https://i.imgur.com/tv0BZFl.jpg
This is the escaped string:
\u001b[01m\u001b[K:\u001b[m\u001b[K In function '\u001b[01m\u001b[Kin...
This is what the string looks like if I directly output it in HTML:
[01m[K:[m[K In function '[01m[Kint main()[m[K':
[01m[K:9:1:[m[K [01;31m[Kerror: [m[Kexpected '[01m[K;[m[K' before '[01m[K}[m[K' token
}
[01;32m[K ^[m[K
Looks like you can use the strip-ansi package. Here's an example using your escaped string:
const stripAnsi = require('strip-ansi');
stripAnsi("\u001b[01m\u001b[K:\u001b[m\u001b[K In function '\u001b[01m\u001b[Kin...")
// result => ": In function 'in..."
If you aren't using node.js, or cannot use that package for whatever reason, this Stack Overflow answer has a regular expression you may be able to use instead.
Just found this tool too:
https://www.npmjs.com/package/ansi-to-html
which converts ANSI to html.

regex replace Blocks start and end

I'm trying have a source to generate two source, for web and phone, I'd like to write a source js like this:
{*Phone
import webfunction from ./utils
*/Phone}
...anothercode..
{*Web
import webfunction from ./utils
*/Web}
...anothercode..
I've heard that with regex is not easy to replace blocks, inside de blocks will no write comments or strange chars, but i'dont know what it's the better strings that I can use with regex to get easier this.
If I want to get the pure parte for Phone and save to a new file; i've just call regex function to delete Web part, and vice versa ...
anyone does any idea what is the best string that i can use to achieve this ?
Update 1:
In this case the regex is not working, with two blocks inside ddd
{*Web
dddd
}
ddd
{*Web
*}
You can use the following regex:
\{\*(\w+)[\s\S]*?\*\/\1\}
And test it here

Code not working as bookmarklet in chrome

here is my code
function ()({var dob=/\s*\d*\s*\.?\s*\d*\s*-\s*\d*\s*\.?\s*\d*\s*/;
var x=prompt("Enter data","");if (x.test(dob)){var y=x.split("-");
alert(parseFloat(y[0]));
alert(parseFloat(y[1]));}
else{alert("Not Matched");}})();
it takes input range as a-b(example 4-5) and splits its value to a and b.if a single decimal is entered it shows Not Matched.It works fine in Jsfiddle but not working in chrome as bookmarklet.Please help
Looks like the problem is with your regex test.
Instead of this:
x.test(dob)
Try this:
dob.test(x)
Edit: Also, you're missing the beginning opening parenthesis '(' before the function.

Categories

Resources