Manual Porting PHP to JavaScript - Automatic outlook


Many situations may arise wherein basic data types like Integers, Decimals (Floats) etc.. are reduced to a character sequence known as Strings, - a coerced conversion process which comes with the burden of loosing specific methods or functions associated with each of those data types. Information is basically reduced and redundancy increased. Situations where such string type coercions occur are in Inputs fields of Graphical User Interfaces (GUIs), in general Human-Machine-Interface (HMI) situations, in interoperability data transmission, conversion, intercommunication scenarios and is not to be confused with Reduction in complexity or 'type reductions' of fields other than Computer Science. A good article, (in light of the sparsity of literature on this subject) mentioning concrete strategies, can be found here (L. Koninck et al).

Aforementioned, Type dependent methods may be the change of the significant figures of a Decimal. You might also want to find the optimal Container type for a given set of entries.


Prior to a specific implementation in PHP, named parseType, to facilitates type determination , I dug around the web ensuring that there is in fact no straightforward solution available that would convert PHP to JavaScript or any of the major scripting languages (Python, Ruby, ...). For a Ruby to PHP compiler see here. With PHP being the dominant server-side scripting language for web-sites, I won't have to argue about the vast, global PHP codebase amassed over almost two decades.

parseType is supposed to determine  the optimal matching type of a provided Source String, or Binary String- with a specific Bioinformatic focus. Regardless of the size, only the header or footer will be used for processing depending on the nature of file. In rare cases sparse checks may be performed at given percentage points of the string's content (e.g. for PDB files).
Another difficulty to asses the correct type arises for very short strings, - strings on the order of 10 characters or less (e.g. varchar(10) which complicate figuring out whether an input is RNA or DNA, when the provided Subsequence does in fact not contain a characteristic U, which would brand it as RNA. The programmatic processing order thus determines the type in such instances e.g. the default in this case would be set to DNA. Contextual information has to be considered in such cases.


Since my initial implementation was in PHP, and I couldn't find any scripts or applications for porting PHP to JavaScript (even half-way) I thought it would be a good idea to write down the salient points of inter-conversion that I spotted. Overall the transition between these two languages is very easy. Additionally you will be interested in the PHPJS project which devoted itself to the task of implementing almost all basic PHP functions in JS, - and as it stands at the moment, this goal it is almost reached.

ECMAScript has reserved the keyword class but doesn't use it (but will in JS 2.0). Hence the first order of business is changing PHP classes into a JS compatible structure:

For type checking - which in PHP is typically done via a function like 'is_TYPE' (e.g. is_array(..) ), - I have compiled my own list of functions which I included in islib. I generally copy and paste functions from islib which I need. (islib will become an independent library with a proper hierarchy in Q2/2012)


All in all, speaking for three small projects ported so far, porting is generally extremely straightforward, amenable to automatization (will be shared on gist) and occasionally requires some manual input. Naturally it is much easier to auto-convert to Dart, Javascript 1.8 or Coffeescript and let those trans-compilers do the rest, which is the approach I am taking.