Giter Club home page Giter Club logo

http-build-query's People

Contributors

vladzadvorny avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

http-build-query's Issues

你在吗

I encountered php's http_build_query transfer in development is not the same as js transfer*

error is value null

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

Empty values aren't parsed correctly

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="
>>> 

Empty arrays/objects add unnecessary `&` symbols

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.