diff --git a/LICENSE b/LICENSE index d16e29f..07fe756 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,7 @@ The MIT License (MIT) Copyright (c) 2014 Jonathan Ong me@jongleberry.com -Copyright (c) 2015 koajs and other contributors +Copyright (c) 2015-present koajs and other contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/index.js b/index.js index 76e63bd..b02f4d1 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ const merge = require('merge-descriptors'); -module.exports = function (app, mode) { +module.exports = function (app, mode, options) { mode = mode || 'extended'; const qs = (mode === 'extended') ? require('qs') : require('querystring'); @@ -25,7 +25,7 @@ module.exports = function (app, mode) { let c = this._querycache = this._querycache || {}; let query = c[str]; if (!query) { - c[str] = query = qs.parse(str); + c[str] = query = qs.parse(str, options); if (converter) { for (let key in query) { query[key] = converter(query[key]); diff --git a/package.json b/package.json index 68ad442..d88b612 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,18 @@ "url": "http://jongleberry.com", "twitter": "https://twitter.com/jongleberry" }, + "contributors": [ + { + "name": "Nick Baugh", + "email": "niftylettuce@gmail.com", + "url": "http://niftylettuce.com/" + }, + { + "name": "Imed Jaberi", + "email": "imed_jebari@hotmail.fr", + "url": "https://www.3imed-jaberi.com/" + } + ], "license": "MIT", "repository": "koajs/qs", "dependencies": { diff --git a/test/test.spec.js b/test/test.spec.js index 68adbe7..000aa4b 100644 --- a/test/test.spec.js +++ b/test/test.spec.js @@ -114,4 +114,51 @@ describe('Koa Querystring', function () { }) }) }) + + describe('custom qs options', function () { + it('should correctly parse numbers and booleans', function (done) { + let app = qs(new Koa(), null, { + decoder: function (str) { + switch (str) { + case 'true': return true; + case 'false': return false; + case 'null': return null; + } + + if (/^[\d.]+$/.test(str)) { + return Number(str); + } + + return str; + } + }) + + app.use(convert(function* (next) { + try { + yield* next + } catch (err) { + console.error(err.stack) + } + })) + + app.use(convert(function* () { + this.body = this.query; + })) + + request(app.listen()) + .get('/?a=str&b=1&c=3.1415&d=true&e=false&f=null&g=2.7182e') + .expect(function (res) { + res.body.should.eql({ + a: 'str', + b: 1, + c: 3.1415, + d: true, + e: false, + f: null, + g: '2.7182e', + }) + }) + .expect(200, done); + }) + }) })