JSFiddle - React, Tailwind, and code Playground
by Jimmy Chandra
HTML
<p>Go open up your console log and watch for result</p>
JavaScript
function applyRule(rule, doc, scenario, expected) {
var docGbn, docSeller, ruleGbn, ruleSeller, applied, expectedApplied;
//Normalize document fields
docGbn = doc.gbn === '' ? null : doc.gbn;
docSeller = doc.seller === '' ? null : doc.seller;
//Normalize rule fields (might not needed if already done during save)
ruleGbn = rule.gbn === '' ? null : rule.gbn;
ruleSeller = rule.seller === '' ? null : rule.seller;
if (ruleGbn === null && ruleSeller === null) {
//Ideally this should not happen, should check during rule creation and prevent this.
console.log('Invalid rule!');
applied = 'Not Applied';
} else {
//The magic rule...
if ((ruleGbn === null || docGbn == ruleGbn) || (ruleSeller === null || docSeller == ruleSeller)) {
applied = 'Applied';
} else {
applied = 'Not Applied';
}
}
expectedApplied = (expected ? 'Applied' : 'Not Applied');
passing = applied == expectedApplied ? 'PASS' : 'FAIL';
console.log('Scenario ' + scenario + ': ' + passing + ' Actual: ' + applied + '. Expected: ' + expectedApplied);
}
console.clear();
//matching company name only
var doc = { seller : 'ABC', gbn : null };
var rule = { seller : 'ABC', gbn : null };
applyRule(rule, doc, 1, 1);
//matching company name only
doc = { seller : 'ABC', gbn : '' };
rule = { seller : 'ABC', gbn : null };
applyRule(rule,doc, 2, 1);
//matching company name only
doc = { seller : 'ABC', gbn : null };
rule = { seller : 'ABC', gbn : '' };
applyRule(rule,doc, 3, 1);
//matching abn only
doc = { seller : null, gbn : '123' };
rule = { seller : null, gbn : '123' };
applyRule(rule,doc, 4, 1);
//matching abn only
doc = { seller : '', gbn : '123' };
rule = { seller : null, gbn : '123' };
applyRule(rule,doc, 5, 1);
//matching abn only
doc = { seller : null, gbn : '123' };
rule = { seller : '', gbn : '123' };
applyRule(rule,doc, 6,...