From 9c34fcf22399281694f7103b5864a1420d4c4d5e Mon Sep 17 00:00:00 2001 From: Joshua Kuestersteffen Date: Tue, 5 Nov 2024 17:59:16 -0600 Subject: [PATCH 1/3] Add temp JSDoc to the function --- src/contact-summary/contact-summary-emitter.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/contact-summary/contact-summary-emitter.js b/src/contact-summary/contact-summary-emitter.js index 11bbc06b1..4960dfd3c 100644 --- a/src/contact-summary/contact-summary-emitter.js +++ b/src/contact-summary/contact-summary-emitter.js @@ -1,3 +1,19 @@ +/** + * @param contactSummary {{ + * fields: { + * appliesToType: string[] | undefined, + * appliesIf: () => boolean | undefined, + * }[] | undefined, + * context: object | undefined, + * cards: { + * appliesToType: string[] | undefined, + * appliesIf: (report: object) => boolean | boolean | undefined, + * }[] | undefined, + * }} + * @param contact {object} + * @param reports {object[]} + * @returns {{cards: object[], fields: object[]}} + */ function emitter(contactSummary, contact, reports) { var fields = contactSummary.fields || []; var context = contactSummary.context || {}; From a776d124392c5778b86306ab29b8733737f70d2f Mon Sep 17 00:00:00 2001 From: Joshua Kuestersteffen Date: Mon, 18 Nov 2024 15:53:15 -0600 Subject: [PATCH 2/3] Extract filter function --- .../contact-summary-emitter.js | 55 ++++++++++++++----- .../contact-summary-emitter.spec.js | 2 +- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/contact-summary/contact-summary-emitter.js b/src/contact-summary/contact-summary-emitter.js index 4960dfd3c..94f627861 100644 --- a/src/contact-summary/contact-summary-emitter.js +++ b/src/contact-summary/contact-summary-emitter.js @@ -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: { @@ -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) { diff --git a/test/contact-summary/contact-summary-emitter.spec.js b/test/contact-summary/contact-summary-emitter.spec.js index 38f32f8ce..6809a28be 100644 --- a/test/contact-summary/contact-summary-emitter.spec.js +++ b/test/contact-summary/contact-summary-emitter.spec.js @@ -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 From 9dbb271279b2cf42d43f2f5f36bcbc3ca46ef0e1 Mon Sep 17 00:00:00 2001 From: Joshua Kuestersteffen Date: Wed, 4 Dec 2024 10:14:26 -0600 Subject: [PATCH 3/3] Continue refactoring --- .../contact-summary-emitter.js | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/contact-summary/contact-summary-emitter.js b/src/contact-summary/contact-summary-emitter.js index 94f627861..3e36d832e 100644 --- a/src/contact-summary/contact-summary-emitter.js +++ b/src/contact-summary/contact-summary-emitter.js @@ -4,22 +4,29 @@ const contactTypeIncluded = (contactType, includedTypes) => { }; const contactTypeExcluded = (contactType, excludedTypes) => { - return !(excludedTypes.length > 0) || excludedTypes.includes('!' + contactType); // TODO + return excludedTypes.length === 0 || excludedTypes.includes('!' + contactType); // TODO }; -const fieldFilter = (contactType, f) => { - const includedTypes = convertToArray(f.appliesToType); +const fieldFilter = (contactType, field) => { + // These two arrays should be disjoint sets but they are not. + const includedTypes = convertToArray(field.appliesToType); const excludedTypes = includedTypes.filter(function(type) { return type && type.charAt(0) === '!'; }); + const noTypesConfigured = includedTypes.length === 0; + const configuredTypeIncluded = includedTypes.includes(contactType); + const excludedTypesConfigured = excludedTypes.length > 0; + const contactTypeExcluded = excludedTypes.includes('!' + contactType); - if (contactTypeIncluded(contactType, includedTypes) || !contactTypeExcluded(contactType, excludedTypes)) { - if (!f.appliesIf || f.appliesIf()) { - delete f.appliesToType; - delete f.appliesIf; - return true; - } + const typeMatches = noTypesConfigured || configuredTypeIncluded || (excludedTypesConfigured && !contactTypeExcluded); + + const hasAppliesIf = field.appliesIf && typeof field.appliesIf === 'function'; + + if (typeMatches && (!hasAppliesIf || field.appliesIf())) { + delete field.appliesToType; + delete field.appliesIf; + return true; } };