Dear Javascript experts
I was wondering if there was a way of determining wether or not a javascript string is in utf-8?
So i could do something like this:
if(myString == checkutf8())
{
// do something
}
Is there any library out there that can do such things?
Thank you for your help.
Related
Let's assume that my template is like a following
string1=${obj.firstString}
string2=${obj.secondString}
number1=${obj.firstNumber}
I'm looking for some automatic way to wrap all my string parameters with single quotas? The expected output is
string1='A'
string2='B'
number1=42
I understand that I can write string1=${"'" + obj.firstString + "'"} , but maybe there is some more conventional way for this requirement...
Thanks a lot!
I would just do this:
string1='${obj.firstString}'
string2='${obj.secondString}'
number1=${obj.firstNumber}
It's a template language, so the basic idea is to make your program look similar to its own output.
I see requests to socket.io containing parameter t to be like LZywzeV, LZz5lk7 and similar.
All examples that i found so far used second- or millisecond-based UNIX timestamps.
Has anyone ever seen a timestamp format like this? (It is not base64-encoded).
I started looking a site that uses Socket.io today, and got the same problem, trying to look for the protocol definition was useless.
I figured this format is something called yeast
TBH, really don't know why people invent this sort of things instead of use
base64(timestamp.getBytes())
pseudocode instead.
A yeast decode algorithm in Python is as follow:
from datetime import datetime
a='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_'
b={a[i]: i for i in range(len(a))}
c=0
for d in "LZywzeV":
c=c*64+b[d]
print(c)
print(datetime.fromtimestamp(c/1000))
The output of that code is:
1481712065055
2016-12-14 07:41:05
to #jeremoquai:
It is easy, is matter of invert the algorithm:
def yeast(d):
r=""
while d!=0:
r='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_'[d&63]+r
d>>=6
return r
so, if you run
yeast(1481712065055)
it returns LZywzeV
I have a gem in ruby that does translations using google API and I am "translating" it to Elixir.
For example, I get from API some like this:
api-data
And in Ruby today I do this:
encoded = rawdata.force_encoding("UTF-8")
I would like to know if there is a way to "force_encode" (like Ruby does) but with Elixir?
UPDATE SOLUTION
I reached a solution based in your answers guys thanks a lot!
As Elixir handle it as binaries then that is the trick: I get the response body: body |> IO.iodata_to_binary ...
defmodule Request do
alias Extract
use HTTPotion.Base
def process_url(url) do
"https://translate.google.com/translate_a/" <> url
end
def process_response_body(body) do
body |> IO.iodata_to_binary |> Extract.extract
end
end
Here is entire code
You use force encoding in Ruby when the data is tagged as binary but really is UTF-8. In Elixir, they are both at the same time, because all strings are binaries, we don't tag them in anyway. In other words, you shouldn't need to force encoding.
However, if the data is not in UTF-8, then you need to find a way to convert it to UTF-8 in the first place.
I'm building the new version of a website and I have to take charge of older urls looks like :
http://website.com/article_title_rewrited-article_id.html
Actually, I try to work about something like that :
app.get('/:title\-:id([0-9]).html', function...);
But of course it fails !
Can I do this type of rewriting using expressjs, or have I to use another method to port the url rewriting ?
Thanks by advance !
I see you are trying to use the Express url route parser as well as REGEX expressions, unfortunately this doesn't work, you have to use one or the other.
Remove the string and place with a REGEX pattern. Then the groups in the REGEX expressions will be available at req.params[0] and req.params[1].
app.get(/(.+)\.html/, function(req, res, next) {
res.redirect(req.params[0].substring(0, req.params[0].length - 5)); // -5 for length of '.html'
});
I believe that should (untested) sort it generically for all .html extensions if that can be a helpful guide :)
I want to modify my function that strips the tag from the value of a database record in the title of French pages:
Function removeSup(strToRemove)
strToRemove = Replace(strToRemove,"<sup>","(")
strToRemove = Replace(strToRemove,"</sup>",")")
response.Write(strToRemove)
End Function
so that it can read the contents of the tag. If the contents is a ® or a ™, then do nothing, but if it's MD or MC, then do the replace.
I just haven't figured out how to read strings with VBScript so is this something that I should do with VBScript or have it passed to jQuery to do it?
You could work with Regular Expressions but if i understand what you mean the you can do it as below.
Jquery is javascript only, if this is for web, best not to mix vbscript and javascript unless realy necessary.
Function removeSup(strToRemove)
strToRemove = Replace(strToRemove,"<sup>MD</sup>","(MD)")
strToRemove = Replace(strToRemove,"<sup>MC</sup>","(MD)")
response.Write(strToRemove)
End Function