@bitovi/querystring-parser
Advanced tools
Comparing version 0.7.8 to 0.7.9
@@ -7,38 +7,44 @@ const qs = require("qs"); | ||
function parseFields(querystring) { | ||
const results = {}; | ||
const errors = []; | ||
const params = qs.parse(querystring, { depth: 0, comma: true }); | ||
let params = qs.parse(querystring, { depth: 0, comma: true }); | ||
params = Object.entries(params).filter(([key]) => key.startsWith("fields")); | ||
return Object.entries(params).reduce( | ||
(acc, [key, value]) => { | ||
if (!key.startsWith("fields")) return acc; | ||
for (const [key, value] of params) { | ||
// force array of values for simple logic | ||
let values = Array.isArray(value) ? value : [value]; | ||
// force array of values for simple logic | ||
let values = Array.isArray(value) ? value : [value]; | ||
// remove duplicates | ||
values = values.reduce(removeDuplicatesReducer, []); | ||
if (!key.match(/^fields\[(.*?)\]$/)) { | ||
return { | ||
...acc, | ||
errors: [ | ||
...acc.errors, | ||
new QuerystringParsingError({ | ||
message: "Incorrect format was provided for fields.", | ||
querystring, | ||
paramKey: key, | ||
paramValue: values, | ||
}), | ||
], | ||
}; | ||
} | ||
// skip empty string values | ||
values = values.filter(isNonEmptyString); | ||
if (values.length === 0) { | ||
continue; | ||
} | ||
// remove duplicates | ||
values = [...new Set(values)]; | ||
const type = getType(key); | ||
if (!isNonEmptyString(type)) { | ||
errors.push( | ||
new QuerystringParsingError({ | ||
message: "Incorrect format was provided for fields.", | ||
querystring, | ||
paramKey: key, | ||
paramValue: values, | ||
}), | ||
); | ||
continue; | ||
} | ||
// skip empty string values | ||
values = values.filter(isNonEmptyString); | ||
results[type] = values; | ||
} | ||
if (!values.length) return acc; | ||
return { results, errors }; | ||
return { | ||
...acc, | ||
results: { | ||
...acc.results, | ||
[getType(key)]: values, | ||
}, | ||
}; | ||
}, | ||
{ results: {}, errors: [] }, | ||
); | ||
} | ||
@@ -55,9 +61,2 @@ | ||
function removeDuplicatesReducer(accumulator, currentValue) { | ||
if (!accumulator.includes(currentValue)) { | ||
accumulator.push(currentValue); | ||
} | ||
return accumulator; | ||
} | ||
module.exports = parseFields; |
{ | ||
"name": "@bitovi/querystring-parser", | ||
"version": "0.7.8", | ||
"version": "0.7.9", | ||
"description": "", | ||
@@ -39,3 +39,3 @@ "main": "index.js", | ||
}, | ||
"gitHead": "96833029f0c567feea43fb4284402fb277719070" | ||
"gitHead": "e6c78d07257affc6352a304eced33991e5688071" | ||
} |
@@ -20,12 +20,2 @@ const parseFields = require("../lib/parse-fields"); | ||
{ | ||
title: "should skip empty string values (fields)", | ||
querystring: "fields", | ||
expectedResults: {}, | ||
}, | ||
{ | ||
title: "should skip empty string values (fields=)", | ||
querystring: "fields=", | ||
expectedResults: {}, | ||
}, | ||
{ | ||
title: "should skip empty string values (fields[]=)", | ||
@@ -104,2 +94,9 @@ querystring: "fields[]=", | ||
}, | ||
{ | ||
title: "should support attributes for main table", | ||
querystring: "fields[]=title", | ||
expectedResults: { | ||
"": ["title"], | ||
}, | ||
}, | ||
]); | ||
@@ -111,11 +108,10 @@ }); | ||
{ | ||
title: | ||
"should return return error when the type could not be parsed (fields=title)", | ||
querystring: "fields=title", | ||
title: "should skip empty string values (fields)", | ||
querystring: "fields", | ||
expectedErrors: [ | ||
new QuerystringParsingError({ | ||
message: "Incorrect format was provided for fields.", | ||
querystring: "fields=title", | ||
querystring: "fields", | ||
paramKey: "fields", | ||
paramValue: ["title"], | ||
paramValue: [""], | ||
}), | ||
@@ -125,10 +121,22 @@ ], | ||
{ | ||
title: "should skip empty string values (fields=)", | ||
querystring: "fields=", | ||
expectedErrors: [ | ||
new QuerystringParsingError({ | ||
message: "Incorrect format was provided for fields.", | ||
querystring: "fields=", | ||
paramKey: "fields", | ||
paramValue: [""], | ||
}), | ||
], | ||
}, | ||
{ | ||
title: | ||
"should return return error when the type could not be parsed (fields[]=title)", | ||
querystring: "fields[]=title", | ||
"should return return error when the type could not be parsed (fields=title)", | ||
querystring: "fields=title", | ||
expectedErrors: [ | ||
new QuerystringParsingError({ | ||
message: "Incorrect format was provided for fields.", | ||
querystring: "fields[]=title", | ||
paramKey: "fields[]", | ||
querystring: "fields=title", | ||
paramKey: "fields", | ||
paramValue: ["title"], | ||
@@ -135,0 +143,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
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
3422
0
133686
44