Skip to content

Commit

Permalink
Extract filter function
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuester committed Nov 18, 2024
1 parent 9c34fcf commit a776d12
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
55 changes: 41 additions & 14 deletions src/contact-summary/contact-summary-emitter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@

const contactTypeIncluded = (contactType, includedTypes) => {
return includedTypes.length === 0 || includedTypes.includes(contactType);
};

const contactTypeExcluded = (contactType, excludedTypes) => {
return !(excludedTypes.length > 0) || excludedTypes.includes('!' + contactType); // TODO
};

const fieldFilter = (contactType, f) => {
const includedTypes = convertToArray(f.appliesToType);
const excludedTypes = includedTypes.filter(function(type) {
return type && type.charAt(0) === '!';
});


if (contactTypeIncluded(contactType, includedTypes) || !contactTypeExcluded(contactType, excludedTypes)) {
if (!f.appliesIf || f.appliesIf()) {
delete f.appliesToType;
delete f.appliesIf;
return true;
}
}
};

/**
* @param contactSummary {{
* fields: {
Expand All @@ -23,20 +48,22 @@ function emitter(contactSummary, contact, reports) {

var result = {
cards: [],
fields: fields.filter(function(f) {
var appliesToType = convertToArray(f.appliesToType);
var appliesToNotType = appliesToType.filter(function(type) {
return type && type.charAt(0) === '!';
});
if (appliesToType.length === 0 || appliesToType.includes(contactType) ||
(appliesToNotType.length > 0 && !appliesToNotType.includes('!' + contactType))) {
if (!f.appliesIf || f.appliesIf()) {
delete f.appliesToType;
delete f.appliesIf;
return true;
}
}
}),

fields: fields.filter(field => fieldFilter(contactType, field)),
// fields: fields.filter(function(f) {
// var appliesToType = convertToArray(f.appliesToType);
// var appliesToNotType = appliesToType.filter(function(type) {
// return type && type.charAt(0) === '!';
// });
// if (appliesToType.length === 0 || appliesToType.includes(contactType) ||
// (appliesToNotType.length > 0 && !appliesToNotType.includes('!' + contactType))) {
// if (!f.appliesIf || f.appliesIf()) {
// delete f.appliesToType;
// delete f.appliesIf;
// return true;
// }
// }
// }),
};

cards.forEach(function(card) {
Expand Down
2 changes: 1 addition & 1 deletion test/contact-summary/contact-summary-emitter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const rewire = require('rewire');

const emitter = rewire('../../src/contact-summary/contact-summary-emitter');

describe('contact-summary-emitter', function() {
describe.only('contact-summary-emitter', function() {
describe('test-setup', function() {
it('should provide the lib', function() {
// given
Expand Down

0 comments on commit a776d12

Please sign in to comment.