Communication Tips
CSV to array
Often when you receive a file from an external server it is in the CSV format (comma seperated values). To work with the data in such a file it is easiest to put the data into an array. This is how you can do that:
var csvstring = "arduino, ESP8266, ESP32, Microbit, Raspberry";
var array = csvstring.split(",");
alert(array);
Encode a string for sending data
In many cases when we want tom send data over the internet to another computer it is necesary to replace spaces in a text by the code %20
If we want, for example, to send the following text: "the temperature is 15 degrees" we need to encode it as follows: "the%20temperature%20is%2015%20degrees"
We can achieve this easily with the following lines of code:
var normal = "the temperature is 15 degrees";
var coded = encodeURI(normal);
Decode a received string
When we receive data over the internet from a server or other computer many times in the text spaces are replaced by the code %20. Apparently the internet does not like spaces. So if we receive a text like: the temperature is 15 degrees it wil be received as follows: the%20temperature%20is%2015%20degrees This can be easily converted to the original form with the next lines of code:
var coded = "the%20temperature%20is%2015%20degrees";
var normal = decodeURI(coded);
alert(normal)
Mask an email adres
Sometimes you want to mask an e-mail adres on a webpage. You can do that with the next snippet.
var email = "[email protected]";
var [name, provider] = email.split('@');
var helpname = name[0] + '...' + name[name.length -1];
var hidden = helpname + '@' + provider;
console.log(hidden);
In this example the console will show: [email protected]
Sometimes you want to mask an e-mail adres on a webpage. You can do that with the next snippet.
var email = "[email protected]";
var [name, provider] = email.split('@');
var helpname = name[0] + '...' + name[name.length -1];
var hidden = helpname + '@' + provider;
console.log(hidden);
In this example the console will show: [email protected]
What is my ip adress
If you need to know the users ip adress you can use the next snippet
fetch('https://api.ipify.org/?format=json').
then(results => results.json()).
then(data => console.log(data.ip))
If you need to know the users ip adress you can use the next snippet
fetch('https://api.ipify.org/?format=json').
then(results => results.json()).
then(data => console.log(data.ip))