api-query-params
Advanced tools
Comparing version 4.15.0 to 4.16.0
@@ -0,1 +1,7 @@ | ||
# 4.16.0 / 2020-09-21 | ||
- Make built-in casting functions customizable using the `casters` option | ||
- Improve number detection to exclude strings greater than MAX_SAFE_INTEGER (fix #113) | ||
- upgrade all dependencies | ||
# 4.9.0 / 2018-11-29 | ||
@@ -2,0 +8,0 @@ |
@@ -14,4 +14,8 @@ "use strict"; | ||
const builtInCasters = { | ||
string: val => String(val), | ||
date: val => new Date(String(val)) | ||
boolean: val => val === 'true', | ||
date: val => new Date(val), | ||
null: () => null, | ||
number: val => Number(val), | ||
regex: (val, flags) => new RegExp(val, flags), | ||
string: val => String(val) | ||
}; | ||
@@ -21,3 +25,3 @@ | ||
// Match type casting operators like string(true) | ||
const casters = _objectSpread({}, builtInCasters, {}, options.casters); | ||
const casters = _objectSpread(_objectSpread({}, builtInCasters), options.casters); | ||
@@ -47,12 +51,8 @@ const casting = value.match(/^(\w+)\((.*)\)$/); | ||
if (regex) { | ||
return new RegExp(regex[1], regex[2]); | ||
return casters.regex(regex[1], regex[2]); | ||
} // Match boolean values | ||
if (value === 'true') { | ||
return true; | ||
} | ||
if (value === 'false') { | ||
return false; | ||
if (value === 'true' || value === 'false') { | ||
return casters.boolean(value); | ||
} // Match null | ||
@@ -62,8 +62,8 @@ | ||
if (value === 'null') { | ||
return null; | ||
} // Match numbers (string padded with zeros are not numbers) | ||
return casters.null(value); | ||
} // Match numbers (strings greater than MAX_SAFE_INTEGER or padded with zeros are not numbers) | ||
if (!Number.isNaN(Number(value)) && !/^0[0-9]+/.test(value)) { | ||
return Number(value); | ||
if (!Number.isNaN(Number(value)) && Math.abs(value) <= Number.MAX_SAFE_INTEGER && !/^0[0-9]+/.test(value)) { | ||
return casters.number(value); | ||
} // Match YYYY-MM-DDTHH:mm:ssZ format dates | ||
@@ -75,6 +75,7 @@ | ||
if (date) { | ||
return new Date(value); | ||
} | ||
return casters.date(value); | ||
} // Default to string | ||
return value; | ||
return casters.string(value); | ||
}; | ||
@@ -270,3 +271,3 @@ | ||
if (unprefixedKey.indexOf('.') === -1) { | ||
row.select = _objectSpread({}, row.select, { | ||
row.select = _objectSpread(_objectSpread({}, row.select), {}, { | ||
[unprefixedKey]: result.projection[key] | ||
@@ -273,0 +274,0 @@ }); |
{ | ||
"name": "api-query-params", | ||
"version": "4.15.0", | ||
"version": "4.16.0", | ||
"description": "Convert query parameters from API urls to MongoDB queries", | ||
@@ -38,17 +38,17 @@ "main": "dist/cjs/index.js", | ||
"devDependencies": { | ||
"@babel/cli": "7.7.7", | ||
"@babel/core": "7.7.7", | ||
"@babel/plugin-proposal-object-rest-spread": "7.7.7", | ||
"@babel/preset-env": "7.7.7", | ||
"@babel/register": "7.7.7", | ||
"ava": "2.4.0", | ||
"coveralls": "3.0.9", | ||
"eslint": "6.8.0", | ||
"eslint-config-gowento": "6.4.1", | ||
"mkdirp": "0.5.1", | ||
"np": "5.2.1", | ||
"nyc": "15.0.0", | ||
"prettier": "1.19.1", | ||
"rimraf": "3.0.0" | ||
"@babel/cli": "7.11.6", | ||
"@babel/core": "7.11.6", | ||
"@babel/plugin-proposal-object-rest-spread": "7.11.0", | ||
"@babel/preset-env": "7.11.5", | ||
"@babel/register": "7.11.5", | ||
"ava": "3.12.1", | ||
"coveralls": "3.1.0", | ||
"eslint": "7.9.0", | ||
"eslint-config-gowento": "7.0.0", | ||
"mkdirp": "1.0.4", | ||
"np": "6.5.0", | ||
"nyc": "15.1.0", | ||
"prettier": "2.1.2", | ||
"rimraf": "3.0.2" | ||
} | ||
} |
@@ -299,3 +299,3 @@ # api-query-params | ||
You can specify you own casting functions to apply to query parameter values, either by explicitly wrapping the value in URL with your custom function name (See example below) or by implictly mapping a key to a function (See `Specify casting per param keys` below) | ||
You can specify you own casting functions to apply to query parameter values, either by explicitly wrapping the value in URL with your custom function name (See example below) or by implictly mapping a key to a function (See `Specify casting per param keys` below). Note that you can also override built-in casting functions: `boolean`, `date` ,`null` ,`number` ,`regex` and `string`. | ||
@@ -305,6 +305,7 @@ - `casters`: object to specify custom casters, key is the caster name, and value is a function which is passed the query parameter value as parameter. | ||
```js | ||
aqp('key1=lowercase(VALUE)&key2=int(10.5)', { | ||
aqp('key1=lowercase(VALUE)&key2=int(10.5)&key3=true', { | ||
casters: { | ||
lowercase: val => val.toLowerCase(), | ||
int: val => parseInt(val, 10), | ||
boolean: val => (val === 'true' ? '1' : '0'), | ||
}, | ||
@@ -316,2 +317,3 @@ }); | ||
// key2: 10, | ||
// key3: '1', | ||
// } | ||
@@ -318,0 +320,0 @@ // } |
24128
264
362