Giter Site home page Giter Site logo

Number parsing about qs HOT 15 CLOSED

ljharb avatar ljharb commented on April 27, 2024
Number parsing

from qs.

Comments (15)

hueniverse avatar hueniverse commented on April 27, 2024

This would be completely unpredictable.

from qs.

ngryman avatar ngryman commented on April 27, 2024

Why that?

jQuery does it pretty well: https://github.com/jquery/jquery/blob/c869a1ef8a031342e817a2c063179a787ff57239/src/core.js#L214.

function isNumeric(obj) {
  return !Array.isArray(obj) && (obj - parseFloat(obj) + 1) >= 0;
}

from qs.

hueniverse avatar hueniverse commented on April 27, 2024

No. It will break my code because I always expect strings, not numbers.

from qs.

ngryman avatar ngryman commented on April 27, 2024

Which code, the parsing code?

If so leave it intact, and walk over the final object when you are about to return it, like here I guess: https://github.com/hapijs/qs/blob/master/lib/parse.js#L146.

Sorry, but I don't understand why this would be so complicated, perhaps I'm missing something... If so I'd really like to have more details :)

from qs.

gergoerdosi avatar gergoerdosi commented on April 27, 2024

It can break code which for example relies on concatenation. '2' + '2' returns 22, but 2 + 2 returns 4. It is easier and safer to write code if the type of the returned value is always a string.

from qs.

ngryman avatar ngryman commented on April 27, 2024

@gergoerdosi So if we are talking about user code, I totally can understand that.
We are talking about backward compatibility.
number parsing would be a new feature hidden behind a new option which is disabled by default.
This would not break any existing code.

from qs.

hueniverse avatar hueniverse commented on April 27, 2024

There is no point. If you want this as an option, write your own module on top of qs that checks for numbers and parses them. There is nothing wrong with layering your modules. If everyone starts using your module because it is a feature they really want, we can move it into qs.

from qs.

ngryman avatar ngryman commented on April 27, 2024

Ok, seems legit.
Thanks.

from qs.

ngryman avatar ngryman commented on April 27, 2024

FYI, I published qs-numbers: https://github.com/ngryman/qs-numbers.
It passes your tests with adjustments for number values.

from qs.

jerone avatar jerone commented on April 27, 2024

I'm wondering why the (official) JSON standard isn't followed from the beginning?

Now I have to server-side convert all string matching the number-pattern to real numbers. Maybe add an option strict to comply to the standards and keep backwards compatibility.

http://json.org/
https://www.ietf.org/rfc/rfc4627.txt
http://rfc7159.net/rfc7159#rfc.section.6
http://www.ecma-international.org/ecma-262/5.1/#sec-8.5
https://github.com/douglascrockford/JSON-js/blob/master/json.js#L294


@ngryman commented on 28 sep. 2014 17:32 CEST:

FYI, I published qs-numbers: https://github.com/ngryman/qs-numbers.

It passes your tests with adjustments for number values.

👍

from qs.

ljharb avatar ljharb commented on April 27, 2024

@jerone because http query strings have nothing to do with json, so that'd be the wrong standard to follow?

from qs.

faceyspacey avatar faceyspacey commented on April 27, 2024

it would force query URLs to look like this: `?foo='1'&bar=1' -- may or may not be a bad thing. If it was the path, you certainly wouldn't want: /foo/'1'/bar/1.

from qs.

mehmetaydogdu avatar mehmetaydogdu commented on April 27, 2024

Given the fact that parse and stringified are opposite of each others, I expect to find a way to get the same value once I stringified and then parsed a given number. But that is not the case as below. The default behaviour can be kept for compatibility but there can be an option to get number values explicitly.

const expected = {a:1}
const str = qs.stringify(expected)
const actual = qs.parse(str)
// => { a: '1' }

from qs.

ljharb avatar ljharb commented on April 27, 2024

That's not a reasonable expectation; you can't round trip through JSON.parse and JSON.stringify either.

from qs.

dragon-master-5892 avatar dragon-master-5892 commented on April 27, 2024

my custom number mapping here:

// add your numbers to 'Num(YOUR NUMBER)' like this: { price : 'Num(125.35)' }

const _ = require("lodash");
function deepMap(obj, iterator, context) {
return _.transform(obj, function (result, val, key) {
result[key] = .isObject(val) /*&& !.isDate(val)*/ ?
deepMap(val, iterator, context) :
iterator.call(context, val, key, obj);
});
}

_.mixin({
deepMap: deepMap
});

let myObject = {
'manufactured_year.id': {
'$gte': 'Num(1900.25)', '$lt': 'Num(125+(1933/2))', "depp2": {
'$gte': '+2400', '$lt': 'Num(-45)'
}
}
}

let query = qs.stringify(myObject );

let q = qs.parse(query);

deepMap(q, function (v, k, o) {
if (v) {
if (new RegExp("(Num)+([0-9]*)").test(v)) {
eval("o[k] = " + v.replace("Num", "Number") +";" );
}
}
});
console.log(q);

from qs.

Related Issues (20)

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.