Ty Kroll Ty Kroll - 22 days ago 16
Javascript Question

String gets mangled when passed from Grunt to CasperJS

I have a Gruntfile that can be executed from the command line with a string in Thai.


grunt --data=นี่คือการทดสอบ


When I echo the string, I can see, that the string is intact:


นี่คือการทดสอบ


I then spawn a CasperJS script with the string

grunt.util.spawn({
cmd: '/usr/local/bin/casperjs',
args: [
'--engine=slimerjs',
'--ssl-protocol=any',
'--text=' + str,
'test.js'
],
},
function(error, result, code)
{
grunt.log.writeln(error + ' | ' + result + ' | ' + code);
done();
});


But echoing it now gives me a mess:


à¸<99>ีà¹<88>à¸<84>ือà¸<81>ารà¸<97>à¸<94>สอà¸<9a>


The plot thickens: I'm running this all from cloud-init via userdata on an AWS instance.
However, when I run the same Grunt command via the shell, the scripts work fine. - Any ideas?

Answer

Figured this out.

The bytes encoding changed coming into Casper from Grunt.

Ran them through utf8to16 from http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt

All good to go.

Comments