express-query-params
Advanced tools
Comparing version 0.3.0 to 0.4.0
'use strict'; | ||
var utils = require('./utils'); | ||
var moment = require('moment'); | ||
@@ -8,10 +9,4 @@ | ||
var parseDate = function(date) { | ||
var dto = moment(date, options.dateFormat).format(); | ||
return options.format === 'sql' ? "DATE('" + dto + "')" : dto; | ||
}; | ||
var options = { | ||
parseNum: parseFloat, | ||
parseDate: parseDate, | ||
dateFormat: moment.ISO_8601, | ||
@@ -22,72 +17,10 @@ typeCast: true, | ||
var typeCast = function(val) { | ||
if (!options.typeCast) return val; | ||
if (val === String(options.parseNum(val))) return options.parseNum(val); | ||
if (moment(val, options.dateFormat, true).isValid()) return options.parseDate(val); | ||
return val; | ||
}; | ||
options.parseDate = utils.parseDate(options); | ||
Parsers.mongodb = function(query) { | ||
var parsed = {}; | ||
Parsers.mongodb = require('./mongo'); | ||
Parsers.sql = require('./sql'); | ||
for (var p in query) { | ||
if (query[p].indexOf('*') > -1) { | ||
parsed[p] = new RegExp(query[p].split('*').join('')); | ||
} else if (query[p].indexOf('...') > -1) { | ||
var range = query[p].split('...'); | ||
parsed[p] = { $gte: typeCast(range[0]), $lte: typeCast(range[1]) }; | ||
} else if (query[p].indexOf('>') > -1) { | ||
var equal = query[p].indexOf('>=') > -1; | ||
var operator = equal ? '$gte' : '$gt'; | ||
parsed[p] = {}; | ||
parsed[p][operator] = typeCast(query[p].split(equal ? '>=' : '>').join('')); | ||
} else if (query[p].indexOf('<') > -1) { | ||
var equal = query[p].indexOf('<=') > -1; | ||
var operator = equal ? '$lte' : '$lt'; | ||
parsed[p] = {}; | ||
parsed[p][operator] = typeCast(query[p].split(equal ? '<=' : '<').join('')); | ||
} else if (query[p] === 'true' || query[p] === 'false') { | ||
parsed[p] = query[p] === 'true' ? true : false; | ||
} else if (query[p][0] === '^') { | ||
parsed[p] = new RegExp(query[p] + '$', 'i'); | ||
} else { | ||
parsed[p] = query[p]; | ||
} | ||
} | ||
return parsed; | ||
}; | ||
Parsers.sql = function(query) { | ||
var parsed = ''; | ||
for (var p in query) { | ||
parsed += (parsed === '') ? '' : ' AND '; | ||
if (query[p].indexOf('*') > -1) { | ||
parsed += p + ' LIKE `%' + query[p].split('*').join('') + '%`'; | ||
} else if (query[p].indexOf('...') > -1) { | ||
var range = query[p].split('...'); | ||
parsed += p + ' >= ' + typeCast(range[0]) + ' AND ' + p + ' <= ' + typeCast(range[1]); | ||
} else if (query[p].indexOf('>') > -1) { | ||
var equal = query[p].indexOf('>=') > -1; | ||
var operator = equal ? '>=' : '>'; | ||
parsed += p + ' ' + operator + ' ' + typeCast(query[p].split(operator).join('')); | ||
} else if (query[p].indexOf('<') > -1) { | ||
var equal = query[p].indexOf('<=') > -1; | ||
var operator = equal ? '<=' : '<'; | ||
parsed += p + ' ' + operator + ' ' + typeCast(query[p].split(operator).join('')); | ||
} else if (query[p][0] === '^') { | ||
parsed += p + ' ILIKE `' + query[p].slice(1) + '`'; | ||
} else { | ||
parsed += p + ' = `' + query[p] + '`'; | ||
} | ||
} | ||
return parsed; | ||
}; | ||
var middleware = function(req, res, next) { | ||
if (!req.query || req.query === {}) return next(); | ||
req.parsedQuery = Parsers[options.format](req.query); | ||
req.parsedQuery = Parsers[options.format](req.query, options); | ||
return next(); | ||
@@ -94,0 +27,0 @@ }; |
{ | ||
"name": "express-query-params", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Express.js middleware implementing the API Query Spec, converting the params to SQL or a Mongo query", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10432
9
201