locutusjs / locutus

Bringing stdlibs of other programming languages to JavaScript for educational purposes

Home Page:https://locutus.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

parse_str

jarodium opened this issue · comments

parse_str leaves ? in the first parameter when used in the following form:

parse_str(window.location.search);

The same way behaves PHP, at least version 5.2.5 that I could test on codepad.org http://codepad.org/DK2Lzvkk

You are correct, assuming that the user uses ?var=1 on the parse_str, but in PHP, I would do the following: parse_str($_SERVER["QUERY_STRING"]) and get the vars without the ? parsed.

I know there is no equivalent query_string in JS, so I have to rely on location.search and remove the ? myself, before providing it to the function...

I got your point, it's a valid one, but since - afaik - the aim of this project is to be as much php-compliant as possible, this is a won't fix. But let's @kvz make the final decision.

Not sure if I follow. What I understand is that PHP accepts both:

?var=1

and

var=1

if so, php.js should aim to support the same inputs. a rule of thumb is that test cases written for a php function (if reasonably possible) should also work on a php.js version.

In php if you pass ?a=b&c=d then the result array will be array('?a' => 'b', 'c' => 'd') and phpjs' implementation also follows this behaviour. What jarodium is asking for is to return {'a': 'b', 'c': 'd'} (without the question mark in property name)

I see, in that case you are right @kukawski, it's not php.js' job to stray from php's behavior even if that make life easier for developers.

There has been some work done by @brettz9 in the past to also create php-like globals in js here: https://github.com/kvz/phpjs/blob/master/experimental/language/%24_GET.js to smoothen these things over - but that remains experimental as there doesn't seem to be a clean way to do it.

http://output.jsbin.com/wironegivo?aaa=bbb&ccc=ddd ? Not very optimal, because of parsing after every access, but doable ;-)

Nice work :) and definitely worth saving/documenting/replacing in experimental :) but (as I expect you agree) too invasive to give a place in main imho

Yeah, Object.defineProperty had not existed at the time of writing, but if one is not using functions anyways, one could just adapt your code, @kukawski's , like this:

window.$_GET = {};
(location.search.substr(1) + '&').replace(/([\w%]+)=?([^&]*)&/g, function (ignore, name, value){
    window.$_GET[decodeURIComponent(name)] = decodeURIComponent(value);
});

FYI, the URLSearchParams object could make this even more elegant once implemented across browsers (currently in Firefox: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams ).

Off topic somewhat, but in case it interests people, I've built a fork of a project at https://github.com/brettz9/form-serialize which lets you serialize forms into URL strings, and if one uses this with onhashchange (say by changing the hash after "submitting" a form), one can allow for retrieving of $_GET-like parameters by the app but after the hash ('#'), so one doesn't need a page refresh.

Closing this for now gents, as with this projects renewed goals (http://locutus.io/2016/05/announcing-locutus/) touching globals is a bit out of scope