vasanthv / jsonbox

HTTP-based JSON storage.
https://jsonbox.io
MIT License
2.49k stars 173 forks source link

Find records contains a comma #28

Open kuy opened 4 years ago

kuy commented 4 years ago

Is there a way to find records contains a comma in string field?

Steps to reproduce.

curl -X POST 'https://jsonbox.io/kuy_326c183fded9e7bc28a2xx' \
    -H 'content-type: application/json' \
    -d '{"name": "Hello, Jsonbox"}'
curl -X GET 'https://jsonbox.io/kuy_326c183fded9e7bc28a2xx?q=name:Hello,%20Jsonbox'
curl -X GET 'https://jsonbox.io/kuy_326c183fded9e7bc28a2xx?q=name:Hello%2C%20Jsonbox'

Both GET operations return Cannot read property 'startsWith' of undefined error message.

tmokenc commented 4 years ago

Found the problem, this is a server-side problem, where the the string will be split into many substrings at any comma. https://github.com/vasanthv/jsonbox/blob/241f981183e8a425ee445005e3e063cc7438e885/src/helper.js#L50

@vasanthv By adding this reduce function between the split and the forEach will solve the problem

req_q
  .split(',')
  .reduce((v, x) => {
    x.includes(':') ? v.push(x) : v[v.length-1] += (',' + x)
    return v
  }, [])
  .forEach(i => (q[i.split(':')[0]] = i.split(':')[1]));

Or changing the code a little bit with regex (I don't know much about regex, so this may contains bugs, idk)

req_q
  .split(/,?(\w+):/)
  .slice(1)
  .reduce((v, x, i) => {
    i % 2 == 0 ? v.push([x]) : v[v.length-1].push(x)
    return v
  }, [])
  .forEach(([key, value]) => { ... })