0

findErr method should return true if any of the text value in condition found in the string it throws error i added. What would be correct approach to cover all casing scenario ?

const str = "Not Covered Invalid Quantity invalid quantity Invalid quantityfor the product"

const findErrCode = ((item) =>
  item.match(/(Not Covered|Invalid Quantity|invalid quanity|Invalid quantity)/)
);

console.log(findErrCode(str));

Error

  TypeError: Cannot read property 'match' of undefined
        at findErrCode:43:30
        at eval:46:13
        at eval
        at new Promise
adiga
  • 34,372
  • 9
  • 61
  • 83
hussain
  • 6,587
  • 18
  • 79
  • 152
  • Why are using `item.settlementDesc`? strings don't have a `settlementDesc` property by default. – adiga Jun 06 '19 at 17:58
  • Sorry i minimized code for the question i updated question – hussain Jun 06 '19 at 17:59
  • The error means that the value of `item` is `undefined`, not a string. The error wouldn't happen in your example though. We cannot you help if you provide an example that doesn't reproduce the issue. – Felix Kling Jun 06 '19 at 17:59
  • Doesn't throw the error in the snippet. Please create a [mcve] – adiga Jun 06 '19 at 18:00
  • @adiga yes i updated the question but i wanted to get boolean value so above code should return true – hussain Jun 06 '19 at 18:01
  • You seem to be asking two questions: How to cover different string casing and how to resolve the error. One of them has been answered before: https://stackoverflow.com/q/8993773/218196 – Felix Kling Jun 06 '19 at 18:02

2 Answers2

1

If you just want to check if the string contains either of "Not Covered" or "Invalid Quantity" without caring about their casing, you can use RegExp#test with an i flag. i instructs the test to ignore the casing of the string:

const str = "Not Covered Invalid Quantity invalid quantity Invalid quantityfor the product"

const findErrCode = ((item) =>
  /not covered|invalid quantity/i.test(item)
);

console.log(findErrCode("Not Covered Invalid Quantity"));
console.log(findErrCode("InVaLid quAnTity"));
console.log(findErrCode("Valid quantuty"));
adiga
  • 34,372
  • 9
  • 61
  • 83
  • Can you please explain what this code is doing and where are you getting `i` and `test` – hussain Jun 06 '19 at 18:08
  • @hussain added links with some explanation – adiga Jun 06 '19 at 18:12
  • thank you for the explanation . is there a simple way to implement same code ? – hussain Jun 06 '19 at 18:38
  • This is the simple way... it's one of the simplest regular expressions you could use and a basic `.test` method (which simply returns true if the pattern matches and false if not; `.match` will actually try to return the bits that matched, which is slower if you just want to know whether they're there or not). – IceMetalPunk Jun 06 '19 at 19:52
0

use Regex after converting the string to lowercase, then you maintain some level of control over the string that is returned.

var str = "Not Covered Invalid Quantity INVALID quantityfor the product";

var findErrCode = function()
{
var regex = RegExp("invalid quantity"); 
return regex.test(str.toLowerCase());

}
Jake Steffen
  • 415
  • 2
  • 11