1

I have a checkbox grid that has a combination of AccountIds (with x number of digits) and file names as values. Each pair of values is delimited with a pipe.

The file names will always start with PRC or FE and then a various combination of letters. '{accountId}|{fileName}'

Validation Rule: No AccountIds can have > 1 file name type (PRC or FE)

['123|PRC1', '123|FE1']  // Good

['123|PRC1', '123|PRC2'] // BAD b/c 2 PRC filename types with same accountId

['123|FE1', '123|FE2']   // BAD b/c 2 FE filenames types with same accountId

const checkedItems = [
'12345|PRC-3343',
'12345|FELMP',
'567892|PRC-3343',
'567892|FEIIO',
'12345|PRCNOWAY'
];

// Dataset should have false returned from a validation method
// It is invalid because there are two PRC files with accountId 12345
validateForm(checkedItems: string[]) {
  // find dups
}

formValid = this.validateForm(checkedItems);

Your help is very much appreciated. I am pretty much looking for an optimized way of finding out if form values are valid or not. Thank you.

AlPh
  • 13
  • 3

1 Answers1

0

Answer based on https://stackoverflow.com/a/7376645/12031452


const checkedItems = [
'12345|PRC-3343',
'12345|FELMP',
'567892|PRC-3343',
'567892|FEIIO',
'12345|PCNOWAY'
];

function validateForm(checkedItems: string[]): boolean {
    const parsedCheckedItems = parseValues(checkedItems);
    return !checkDups(parsedCheckedItems);
}

function parseValues(checkedItems: string[]): string[] {
    return checkedItems.map(item => {
        const pipeIdx = item.indexOf('|');
        return item.substr(1, pipeIdx + 1);
    });
}

function checkDups(parsedCheckedItems: string[]): boolean {
    return new Set(parsedCheckedItems).size !== parsedCheckedItems.length;
}

const formValid = validateForm(checkedItems);

console.log(formValid);
user3366195
  • 99
  • 2
  • 6