vladzadvorny / http-build-query Goto Github PK
View Code? Open in Web Editor NEWphp's http_build_query() in js
License: MIT License
php's http_build_query() in js
License: MIT License
I encountered php's http_build_query transfer in development is not the same as js transfer*
test values
let params = {
string_value: 'test_value',
null_value: null,
empty_string: '',
number_string: 196645,
zero_value: 0,
params: {
string_value: 'test_value',
null_value: null,
empty_string: '',
number_string: 196645,
zero_value: 0,
}
};
TypeError: Cannot convert undefined or null to object
at Function.keys
я немного подправил код, до рабочего состояние fix1
var httpBuildQuery = function(queryData, numericPrefix, argSeparator, tempKey) {
numericPrefix = numericPrefix || null;
argSeparator = argSeparator || '&';
tempKey = tempKey || null;
// fix 1 start
if(queryData === null){
return '';
}
// fix 1 end
var query = Object.keys(queryData).map(k => {
var res;
var key = k;
if (tempKey) {
key = tempKey + '[' + key + ']';
}
if (typeof queryData[k] === 'object') {
res = httpBuildQuery(queryData[k], null, argSeparator, key);
} else {
if (numericPrefix) {
key = isNumeric(key) ? numericPrefix + Number(key) : key;
}
// fix 2 not push empty string
if(queryData[k]!==''){
var val = queryData[k];
if (val === true) {
val = '1';
} else if (val === false) {
val = '0';
}
res = esc(key) + '=' + esc(val);
}
}
return res;
});
return query.join(argSeparator).replace(/[!'()*]/g, '');
};
``
Также я считаю если пушить пустое значение - это также ошибка. fix2
Your module is not compatible with es5 !!!
Running [email protected]
, when a parameter has an empty value (''
or null
), the function will not provide the expected key=
value, and instead return the equivalent of an empty string for the key/value pair.
For example, if I pass {key1: '', key2: ''}
to httpBuildQuery
, I will get back &
as the parsed query string instead of the expected key1=&key2=
query string.
This is contrary to how PHP works:
Psy Shell v0.9.3 (PHP 7.2.4 — cli) by Justin Hileman
>>> http_build_query(['key1' => '', 'key2' => '']);
=> "key1=&key2="
>>>
In PHP, keys with empty arrays or objects are not added to the query string.
For example in PHP:
http_build_query([ 'dog' => 1, 'cat' => [], 'mouse' => new StdClass(), 'rabbit' => 2 ]);
// Returns: "dog=1&rabbit=2"
Interactive PHP test case: https://3v4l.org/l1haG
http-build-query
doesn't add these to the query string, which is correct, but does add extraneous &
symbols:
hbq( { 'dog': 1, 'cat': [], 'mouse': {}, 'rabbit': 2 } );
// Returns: "dog=1&&&rabbit=2"
Interactive JS test case: https://runkit.com/cvp/5b265e43c28df80012c7edd7
These extra &
symbols don't hurt anything, but don't match the PHP implementation.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.