reducing the cyclomatic complexity

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2019-07-23 09:13:50 -03:00
parent 7684947e41
commit 72642eb14b
2 changed files with 31 additions and 25 deletions

View File

@ -5,6 +5,11 @@ const Spec02 = require("../../specs/spec_0_2.js");
const JSONParser = require("../../formats/json/parser.js");
const {
isDefinedOrThrow,
isStringOrObjectOrThrow
} = require("../../utils/fun.js");
const parserByType = {};
parserByType[Constants.MIME_JSON] = new JSONParser();
@ -48,20 +53,18 @@ setterByHeader[Constants.HEADER_CONTENT_TYPE] = {
};
function validateArgs(payload, attributes) {
if(!payload){
throw {message: "payload is null or undefined"};
}
if(!attributes) {
throw {message: "attributes is null or undefined"};
}
Array.of(payload)
.filter(p => isDefinedOrThrow(p,
{message: "payload is null or undefined"}))
.filter(p => isStringOrObjectOrThrow(p,
{message: "payload must be an object or a string"}))
.shift();
if((typeof payload) !== "object" && (typeof payload) !== "string"){
throw {
message: "payload must be an object or a string",
errors: [typeof payload]
};
}
Array.of(attributes)
.filter(a => isDefinedOrThrow(a,
{message: "attributes is null or undefined"}))
.shift();
}
function Receiver(configuration) {
@ -128,7 +131,7 @@ Receiver.prototype.parse = function(payload, headers) {
parserByType[sanityHeaders[Constants.HEADER_CONTENT_TYPE]]
.parse(payload);
// Every unprocessed header should be an extension
// Every unprocessed header can be an extension
Array.from(Object.keys(sanityHeaders))
.filter(value => !processedHeaders.includes(value))
.filter(value =>

View File

@ -5,6 +5,11 @@ const Spec02 = require("../../specs/spec_0_2.js");
var JSONParser = require("../../formats/json/parser.js");
const {
isDefinedOrThrow,
isStringOrObjectOrThrow
} = require("../../utils/fun.js");
const spec02 = new Spec02();
const jsonParserSpec02 = new JSONParser();
@ -50,20 +55,18 @@ setterByAttribute[Constants.STRUCTURED_ATTRS_02.DATA] = {
};
function validateArgs(payload, attributes) {
if(!payload){
throw {message: "payload is null or undefined"};
}
if(!attributes) {
throw {message: "attributes is null or undefined"};
}
Array.of(payload)
.filter(p => isDefinedOrThrow(p,
{message: "payload is null or undefined"}))
.filter(p => isStringOrObjectOrThrow(p,
{message: "payload must be an object or string"}))
.shift();
if((typeof payload) !== "object" && (typeof payload) !== "string"){
throw {
message: "payload must be an object or string",
errors: [typeof payload]
};
}
Array.of(attributes)
.filter(a => isDefinedOrThrow(a,
{message: "attributes is null or undefined"}))
.shift();
}
function Receiver(configuration) {