Skip to content

Commit e276635

Browse files
committed
Breaking: Convert 'null' and 'undefined' to null
1 parent 7838090 commit e276635

File tree

5 files changed

+15
-5
lines changed

5 files changed

+15
-5
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313
> A tiny URL param parser, suitable for server or browser
1414
1515
## highlights
16-
- it's tiny (< 20 sloc)
16+
- it's tiny (< 25 sloc)
1717
- it's highly browser compatible (IE6 and everything else)
1818
- it ships with both ES6 and CommonJS modules
19-
- it's competitively fast (use memoization in front of it to get really fast)
2019
- it makes repeated keys an array `?a=1&a=2` -> `{ a: [1, 2] }`
2120
- it handles explicit arrays `?a[]=1` -> `{ a: [1] }`
2221
- it handles flags (key with no value) `?a=b&flag` -> `{ a: 'b', flag: true }`
2322
- it decodes `encodeURIComponent` encoded items
2423
- it converts booleans and numbers from strings
24+
- it converts the values `'null'` and `'undefined'` to `null`
25+
- it respects the hash (as per [RFC 3986](https://tools.ietf.org/html/rfc3986#section-3.4))
26+
- it's competitively fast (not the fastest, not far from it)
2527

2628
## lowlights
2729
- it looks like code golf, kind of is

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
function decode (s) { return s.indexOf('%') === -1 ? s : decodeURIComponent(s) }
44
function num (v) { return Number.isNaN(+v) ? v : +v }
5+
function nil (v) { return v === 'null' || v === 'undefined' ? null : v }
56
function bool (v) { return v === 'false' ? false : (v === 'true' ? true : v) }
6-
function conv (v) { return bool(num(v)) }
7+
function conv (v) { return bool(nil(num(v))) }
78

89
module.exports = function (u) {
910
var qI, qU, hI
1011
if (!u || (qI = u.indexOf('?')) === -1 || !(qU = u.slice(qI + 1))) return {}
1112
if ((hI = qU.indexOf('#')) !== -1 && !(qU = qU.slice(0, hI))) return {}
1213
var obj = {}
1314
qU.split('&').forEach(function (q) {
15+
if (!q) return
1416
q = ((q = q.split('=')) && q.length === 2 ? q : [q[0], 'true']).map(decode)
1517
if (q[0].slice(-2) === '[]') obj[q[0] = q[0].slice(0, -2)] = obj[q[0]] || []
1618
if (!obj[q[0]]) return (obj[q[0]] = conv(q[1]))

module.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
function decode (s) { return s.indexOf('%') === -1 ? s : decodeURIComponent(s) }
44
function num (v) { return Number.isNaN(+v) ? v : +v }
5+
function nil (v) { return v === 'null' || v === 'undefined' ? null : v }
56
function bool (v) { return v === 'false' ? false : (v === 'true' ? true : v) }
6-
function conv (v) { return bool(num(v)) }
7+
function conv (v) { return bool(nil(num(v))) }
78

89
export default function (u) {
910
var qI, qU, hI
1011
if (!u || (qI = u.indexOf('?')) === -1 || !(qU = u.slice(qI + 1))) return {}
1112
if ((hI = qU.indexOf('#')) !== -1 && !(qU = qU.slice(0, hI))) return {}
1213
var obj = {}
1314
qU.split('&').forEach(function (q) {
15+
if (!q) return
1416
q = ((q = q.split('=')) && q.length === 2 ? q : [q[0], 'true']).map(decode)
1517
if (q[0].slice(-2) === '[]') obj[q[0] = q[0].slice(0, -2)] = obj[q[0]] || []
1618
if (!obj[q[0]]) return (obj[q[0]] = conv(q[1]))

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tiny-params",
3-
"version": "3.0.1",
3+
"version": "4.0.0",
44
"description": "A tiny URL param parser, suitable for server or browser",
55
"main": "index.js",
66
"module": "module.mjs",

test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const q = [
1111
`zip=37615`,
1212
`last=carpenter`,
1313
`zip=37601&q=%3F`,
14+
`number=22.55`,
15+
`nil=undefined`,
1416
`flag`,
1517
`amp=%26`,
1618
`eq=%3D`,
@@ -25,6 +27,8 @@ runTests('Testing tiny-params CommonJS module', () => {
2527
test('Encoded question mark is correctly parsed', p.q, '?')
2628
test('Encoded ampersand is correctly parsed', p.amp, '&')
2729
test('Encoded equal sign is correctly parsed', p.eq, '=')
30+
test('Numeric value parses as such', p.number, 22.55)
31+
test('Null and undefined stings parse as null', p.nil, null)
2832
test('Boolean value parses as such', p.boolval, false)
2933
test('Flag (key with no value) parses to true', p.flag, true)
3034

0 commit comments

Comments
 (0)