Porting a JavaScript conversion. Seems to be close.