Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bitovi/querystring-parser

Package Overview
Dependencies
Maintainers
15
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bitovi/querystring-parser - npm Package Compare versions

Comparing version 0.7.8 to 0.7.9

71

lib/parse-fields.js

@@ -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 @@ }),

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc