├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Xpepermint (Kristijan Sedlak) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # query-types 2 | 3 | > Handles numeric and boolean values for Express req.query object. 4 | 5 | ## Setup 6 | 7 | ``` 8 | $ npm install --save query-types 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | var queryType = require('query-types'); 15 | var express = require('express'); 16 | 17 | app.use(queryType.middleware()) 18 | 19 | app.get('/', function(req, res) { 20 | res.json(req.query); // Response example: { enabled: true, price: 123.13, name: 'John Smith', origin: [31.412, 41.213] } 21 | }); 22 | ``` 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports.middleware = middleware; 2 | module.exports.isObject = isObject; 3 | module.exports.isNumber = isNumber; 4 | module.exports.isBoolean = isBoolean; 5 | module.exports.isArray = isArray; 6 | module.exports.parseValue = parseValue; 7 | module.exports.parseObject = parseObject; 8 | module.exports.parseArray = parseArray; 9 | module.exports.parseNumber = parseNumber; 10 | module.exports.parseBoolean = parseBoolean; 11 | 12 | function isObject(val) { 13 | return val.constructor === Object; 14 | } 15 | 16 | function isNumber(val) { 17 | return !isNaN(parseFloat(val)) && isFinite(val); 18 | } 19 | 20 | function isBoolean(val) { 21 | return val === 'false' || val === 'true'; 22 | } 23 | 24 | function isArray(val) { 25 | return Array.isArray(val); 26 | } 27 | 28 | function parseValue(val) { 29 | if (typeof val == 'undefined' || val == '') { 30 | return null; 31 | } else if (isBoolean(val)) { 32 | return parseBoolean(val); 33 | } else if (isArray(val)) { 34 | return parseArray(val); 35 | } else if (isObject(val)) { 36 | return parseObject(val); 37 | } else if (isNumber(val)) { 38 | return parseNumber(val); 39 | } else { 40 | return val; 41 | } 42 | } 43 | 44 | function parseObject(obj) { 45 | var result = {}; 46 | var key, val; 47 | for (key in obj) { 48 | val = parseValue(obj[key]); 49 | if (val !== null) result[key] = val; // ignore null values 50 | } 51 | return result; 52 | } 53 | 54 | function parseArray(arr) { 55 | var result = []; 56 | for (var i = 0; i < arr.length; i++) { 57 | result[i] = parseValue(arr[i]); 58 | } 59 | return result; 60 | } 61 | 62 | function parseNumber(val) { 63 | return Number(val); 64 | } 65 | 66 | function parseBoolean(val) { 67 | return val === 'true'; 68 | } 69 | 70 | function middleware() { 71 | return function(req, res, next) { 72 | req.query = parseObject(req.query); 73 | next(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "query-types", 3 | "version": "0.1.4", 4 | "description": "Handles numeric and boolean values for Express req.query object.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git@github.com:xpepermint/query-types.git" 12 | }, 13 | "keywords": [ 14 | "query", 15 | "parser", 16 | "boolean", 17 | "number", 18 | "numeric", 19 | "integer", 20 | "float", 21 | "type", 22 | "types", 23 | "querystring", 24 | "express", 25 | "connect" 26 | ], 27 | "author": "Xpepermint", 28 | "license": "MIT", 29 | "bugs": { 30 | "url": "https://github.com/xpepermint/query-types/issues" 31 | }, 32 | "homepage": "https://github.com/xpepermint/query-types" 33 | } 34 | --------------------------------------------------------------------------------