Comments (15)
This would be completely unpredictable.
from qs.
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.
No. It will break my code because I always expect strings, not numbers.
from qs.
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.
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.
@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.
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.
Ok, seems legit.
Thanks.
from qs.
FYI, I published qs-numbers
: https://github.com/ngryman/qs-numbers.
It passes your tests with adjustments for number values.
from qs.
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.
@jerone because http query strings have nothing to do with json, so that'd be the wrong standard to follow?
from qs.
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.
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.
That's not a reasonable expectation; you can't round trip through JSON.parse and JSON.stringify either.
from qs.
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)
- qs.parse decoder option doesn't work on IOS web view environment HOT 1
- Unable to parse back a stringified object when too nested HOT 3
- `stringify` with `arrayFormat: 'comma'` does not encode commas in strings. HOT 13
- [spam]
- ESM & CJS - Built HOT 9
- Stringify null values HOT 9
- None. HOT 1
- Serialization for `Date` is not working when using `filter` option. HOT 3
- Issue using qs while using Express@5 HOT 7
- qs.stringify(json) and qs.parse(json) Results are inconsistent with expectations HOT 4
- Parsing the stringify result would lead to a different object HOT 1
- qs parse is not letting app load on production when imported from index.tsx HOT 1
- Feature request: export another endpoint that doesn't depend on function-bind and possibly other polyfills HOT 10
- Does not work in NextJS edge middleware HOT 3
- Stringify method with format: "RFC1738" does not encode parentheses -> ( ) HOT 3
- I'm appreciate with this library, however my new project use python. Are there any same packages in Python? HOT 1
- How can you keep the square brackets when using arrayFormat: comma HOT 4
- Add required key and type checking HOT 2
- qs.parse return different while the query is in different position HOT 1
- Problem with stringify in Vite targeting ES2020 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qs.