@bitovi/querystring-parser
Advanced tools
Comparing version 0.6.4 to 0.6.5
/** | ||
* An enumeration of value types available for use in "IBM-style" query filters. | ||
* Examples: equals(name,'michael') -> string value | ||
* Examples: equals(name,'true') -> boolean value | ||
* equals(name,'michael') -> string value | ||
* equals(age,'25') -> number value | ||
@@ -10,2 +11,3 @@ * equals(born,'2020-01-01') -> date value | ||
const IbmValueType = Object.freeze({ | ||
BOOLEAN: Symbol("BOOLEAN"), | ||
STRING: Symbol("STRING"), | ||
@@ -12,0 +14,0 @@ NUMBER: Symbol("NUMBER"), |
/** | ||
* An enumeration of value types available for use in "MongoDB-style" query filters. | ||
* Examples: filter[name]=michael -> string value | ||
* Examples: filter[active]=true -> boolean value | ||
* filter[name]=michael -> string value | ||
* filter[age]=25 -> number value | ||
@@ -10,2 +11,3 @@ * filter[born]=2020-01-01 -> date value | ||
const MongoValueType = Object.freeze({ | ||
BOOLEAN: Symbol("BOOLEAN"), | ||
STRING: Symbol("STRING"), | ||
@@ -12,0 +14,0 @@ NUMBER: Symbol("NUMBER"), |
@@ -10,2 +10,3 @@ const qs = require("qs"); | ||
const QuerystringParsingError = require("../../lib/errors/querystring-parsing-error"); | ||
const isBooleanString = require("../helpers/is-boolean-string"); | ||
@@ -125,12 +126,6 @@ /** Parses "IBM-style" filter expression from of a querystring. */ | ||
value = value.slice(1, value.length - 1); | ||
if (isNumberString(value)) { | ||
// number | ||
return Number(value); | ||
} else if (isDateString(value)) { | ||
// date | ||
return value; | ||
} else { | ||
// string | ||
return wildCardString(value, parentOperator); | ||
} | ||
if (isBooleanString(value)) return value.toLowerCase() === "true"; | ||
if (isNumberString(value)) return Number(value); | ||
if (isDateString(value)) return value; | ||
return wildCardString(value, parentOperator); // string | ||
} else { | ||
@@ -137,0 +132,0 @@ // attribute reference |
@@ -46,3 +46,3 @@ const qs = require("qs"); | ||
/************************************************************************ | ||
* 2. Apply defaults and type coersion | ||
* 2. Apply defaults and type coercion | ||
************************************************************************/ | ||
@@ -62,9 +62,7 @@ // temp wrap single values in array so the logic works the same either way | ||
values = values.map((value) => { | ||
if (isNullString(value)) { | ||
return null; | ||
} else if (valueType === MongoValueType.NUMBER) { | ||
return Number(value); | ||
} else { | ||
return value; | ||
} | ||
if (isNullString(value)) return null; | ||
if (valueType === MongoValueType.BOOLEAN) | ||
return value.toLowerCase() === "true"; | ||
if (valueType === MongoValueType.NUMBER) return Number(value); | ||
return value; | ||
}); | ||
@@ -78,2 +76,3 @@ | ||
switch (valueType) { | ||
case MongoValueType.BOOLEAN: | ||
case MongoValueType.NUMBER: | ||
@@ -97,3 +96,3 @@ case MongoValueType.DATE: | ||
************************************************************************/ | ||
// array compatability check | ||
// array compatibility check | ||
if ( | ||
@@ -111,3 +110,3 @@ providedValueWasAnArray && | ||
// null compatability check | ||
// null compatibility check | ||
if ( | ||
@@ -129,4 +128,17 @@ valueType === MongoValueType.NULL && | ||
// number compatability check | ||
// boolean compatibility check | ||
if ( | ||
valueType === MongoValueType.BOOLEAN && | ||
operator === MongoOperator.ILIKE | ||
) { | ||
errors.push( | ||
createError( | ||
`"${operator}" operator should not be used with boolean values` | ||
) | ||
); | ||
return { results, errors }; // short circuit | ||
} | ||
// number compatibility check | ||
if ( | ||
valueType === MongoValueType.NUMBER && | ||
@@ -143,3 +155,3 @@ operator === MongoOperator.ILIKE | ||
// date compatability check | ||
// date compatibility check | ||
if (valueType === MongoValueType.DATE && operator === MongoOperator.ILIKE) { | ||
@@ -146,0 +158,0 @@ errors.push( |
const MongoValueType = require("../enums/mongo-value-type"); | ||
const isDateString = require("../helpers/is-date-string"); | ||
const isNullString = require("../helpers/is-null-string"); | ||
const isNumberString = require("../helpers/is-number-string"); | ||
const isBooleanString = require("./is-boolean-string"); | ||
const isDateString = require("./is-date-string"); | ||
const isNullString = require("./is-null-string"); | ||
const isNumberString = require("./is-number-string"); | ||
@@ -16,11 +17,7 @@ /** | ||
const types = values.map((val) => { | ||
if (isNumberString(val)) { | ||
return MongoValueType.NUMBER; | ||
} else if (isDateString(val)) { | ||
return MongoValueType.DATE; | ||
} else if (isNullString(val)) { | ||
return MongoValueType.NULL; | ||
} else { | ||
return MongoValueType.STRING; | ||
} | ||
if (isBooleanString(val)) return MongoValueType.BOOLEAN; | ||
if (isNumberString(val)) return MongoValueType.NUMBER; | ||
if (isDateString(val)) return MongoValueType.DATE; | ||
if (isNullString(val)) return MongoValueType.NULL; | ||
return MongoValueType.STRING; | ||
}); | ||
@@ -27,0 +24,0 @@ |
const MongoValueType = require("../../lib/enums/mongo-value-type"); | ||
const areMongoTypesTheSame = require("../../lib/helpers/are-mongo-types-the-same"); | ||
const areMongoTypesTheSame = require("./are-mongo-types-the-same"); | ||
describe("areMongoTypesTheSame", () => { | ||
/** | ||
* Should return the MongoValueType if they all match eachother (besides null) | ||
* Should return the MongoValueType if they all match each other (besides null) | ||
* Otherwise, should return false | ||
*/ | ||
const testCases = [ | ||
// booleans | ||
[MongoValueType.BOOLEAN, ["true"]], | ||
[MongoValueType.BOOLEAN, ["true", "false"]], | ||
[MongoValueType.BOOLEAN, ["true", "false", "null"]], | ||
[MongoValueType.BOOLEAN, ["null", "true"]], | ||
// strings | ||
@@ -32,2 +38,3 @@ [MongoValueType.STRING, ["michael"]], | ||
// false | ||
[false, ["true", "2020-01-02"]], | ||
[false, ["michael", "2020-01-02"]], | ||
@@ -34,0 +41,0 @@ [false, ["25", "2020-01-02"]], |
{ | ||
"name": "@bitovi/querystring-parser", | ||
"version": "0.6.4", | ||
"version": "0.6.5", | ||
"description": "", | ||
@@ -39,3 +39,3 @@ "main": "index.js", | ||
}, | ||
"gitHead": "acb488bc82bfa070edd6876b4ab82949e44e00cc" | ||
"gitHead": "e6c78d07257affc6352a304eced33991e5688071" | ||
} |
@@ -55,4 +55,4 @@ # querystring-parser | ||
const { page } = querystringParser.parse("page[number]=0&page[size]=10"); | ||
console.log(page.number); // --> 0 | ||
const { page } = querystringParser.parse("page[number]=1&page[size]=10"); | ||
console.log(page.number); // --> 1 | ||
console.log(page.size); // --> 10 | ||
@@ -81,4 +81,4 @@ ``` | ||
```js | ||
const { page } = querystringParser.parse("page[number]=0&page[size]=10"); | ||
console.log(page.number); // --> 0 | ||
const { page } = querystringParser.parse("page[number]=1&page[size]=10"); | ||
console.log(page.number); // --> 1 | ||
console.log(page.size); // --> 10 | ||
@@ -85,0 +85,0 @@ ``` |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
119170
44
2993
1