My New Greatest Pal • furbo.org


A buddy of mine just lately pointed me at a nicely hidden command line device. Within the JavaScript framework utilized by Safari and different components of Apple’s merchandise, there’s a device 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.

Yow will discover 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 situations.

If you happen to run jsc -h, you’ll see plenty of choices for testing and profiling JavaScript. It’s clear that the WebKit staff makes use of this internally for operating exams. However we will additionally use it for attempting out concepts and operating easy utilities.

An image is value a thousand phrases, so let me present you ways it may be used to resolve a easy downside. I just lately 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 right means to do that, so I pulled jsc out of my device bag and set to work. The primary problem was getting enter.

Fortunately, there’s a readline() operate that takes keyboard enter and returns a worth. 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 possibly can % escape the characters to UTF-16 after which decode them again into UTF-8 with this system:

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

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

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

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

There are just a few extra built-in capabilities that will show helpful, however thus far, I’ve solely wanted to learn and write textual content. It’s undocumented, however jsc additionally takes commonplace enter and can be utilized as a shebang:

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

Since that is doubtless 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'));

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!



Leave a Comment