My New Finest Buddy • furbo.org


A buddy of mine not too long ago pointed me at a effectively hidden command line instrument. Within the JavaScript framework utilized by Safari and different components of Apple’s merchandise, there’s a instrument referred to as jsc. It’s a command line interface for JavaScript that makes use of the identical code as the remainder of the system.

You could find the binary at /System/Library/Frameworks/JavaScriptCore.framework/Variations/Present/Helpers/jsc. That path is unwieldy, so I’ve an alias arrange that lets my simply kind jsc within the Terminal.

So what are you able to do with jsc? Just about something you are able to do with JavaScript in a browser with the caveat that there aren’t doc and window cases.

In the event you run jsc -h, you’ll see lots of choices for testing and profiling JavaScript. It’s clear that the WebKit crew makes use of this internally for operating exams. However we are able to additionally use it for attempting out concepts and operating easy utilities.

An image is price a thousand phrases, so let me present you ways it may be used to unravel a easy drawback. I not too long ago wanted to transform some strings in our Turkish localization of Frenzic to uppercase: the lowercase “i” was getting transformed to the dotless model.

JavaScript’s toLocaleUpperCase() operate is the proper method to do that, so I pulled jsc out of my instrument bag and started working. The primary problem was getting enter.

Fortunately, there’s a readline() operate that takes keyboard enter and returns a price. Unluckily, that enter isn’t within the encoding you’d anticipate it to be: characters are returned in ISO-8859-1 (Latin-1), not UTF-8. Bear in mind, there’s no doc occasion so the default encoding is used.

To workaround this limitation, you may % escape the characters to UTF-16 after which decode them again into UTF-8 with this method:

var textual content = decodeURIComponent(escape(readline()));

(If any WebKit engineers are studying this, it might be good to have a command line choice like --encoding=utf-8.)

Producing output is a bit totally different than a browser, too. You’ll be utilizing print() as a substitute of console.log(). To transform the textual content enter and show it, I used this:

print(textual content.toLocaleUpperCase('tr-TR'));

There are a couple of extra built-in capabilities which will show helpful, however up to now, I’ve solely wanted to learn and write textual content. It’s undocumented, however jsc additionally takes customary enter and can be utilized as a shebang:

$ echo "print(1+2);" | jsc
3

Since that is probably code I’ll have to make use of once more, I created a Turkish.js file:

whereas (true) {
    print('Turkish textual content?');
    var textual content = decodeURIComponent(escape(readline()));
    print(textual content.toLocaleUpperCase('tr-TR'));
    print('-------------');
}

I can now run this any time with jsc Turkish.js. And also you additionally get to see how having JavaScript in a command line may be useful. Get pleasure from!



Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles