0

I have an array like this.If atleast one of the substrings within the array have the value i'm searching for it should pass.

Value1 = [
  "Grape | 100 |Food Catering Service",
  "Apple | 100,000m |Food Catering Service",
  "Water Melon | 100,000m |Catering Service Outside",
  "100,000m |Food Catering Service Outside
]

Using typescript i'm trying to make sure that atleast if a substring in the array there is the value Food. Even though the third substring within this arrat has no food it should still pass.

Code i've tried so far but does not do the job. It just returns the array

export function arraySubstring (expected:string, ignoreCase?: boolean): Expectation<string[], string> {
 return Expectation.that("test", expected, 
    async(actor: Actor, actual: string[]): Promise<boolean> ==> {

  try {
   for (const value of actual) {
     if(ignoreCase) {
       if (!value.toLowerCase().includes(expected.toLowerCase())) return 
         false;
     } else {
         if (!value.includes(expected)) return false;
     }
  }
  return true;
})
}

const value2= await webActor.attemptsTo(Ensure.that("my expectation", 
value1, arraySubstring ("Food")))
9
  • 1
    Please edit your question to show us the output you need. Your written spec is not quite clear.
    – O. Jones
    Commented Dec 21, 2021 at 23:57
  • @O.Jones is that more explanatory. So if i declare that const value2. if it compares the array value1 and Food. My assertion should be true Commented Dec 22, 2021 at 0:20
  • Also please remember to properly indent your code, because you want people to be able to easily read your code. Commented Dec 22, 2021 at 0:20
  • Why should the third that does not have the keyword pass ?
    – charlietfl
    Commented Dec 22, 2021 at 0:23
  • @charlietfl if atleast one of the substrings within the array have the value i'm searching for it should pass. Since atleast 3 have it then it should pass. Commented Dec 22, 2021 at 0:25

2 Answers 2

2

Your code relies on it's order. If the first element that gets looped contains no "food", it will return false. Return statements always exit the function.

I'd suggest to create a single string and check the index of the first occurance with indexOf("food"), something like that.

const array = [
  "Grape | 100 |Food Catering Service",
  "Apple | 100,000m |Food Catering Service",
  "Water Melon | 100,000m |Catering Service Outside",
  "100,000m |Food Catering Service Outside",
];

const found = array
  .reduce((obj, entry) => obj.concat(entry))
  .toLocaleLowerCase()
  .indexOf("food");
if (found >= 0) {
  console.log("Array contains food");
} else {
  console.log("Array does not contain food");
}

If you want to iterate you need to return on the true case, this will remove the importance of order from your code.

const array = [
  "Grape | 100 |Food Catering Service",
  "Apple | 100,000m |Food Catering Service",
  "Water Melon | 100,000m |Catering Service Outside",
  "100,000m |Food Catering Service Outside",
];

const ignoreCase = true;
const expected = "Food";

const containsExptected = (_array) => {
  for (const value of _array) {
    if (ignoreCase) {
      if (value.toLowerCase().indexOf(expected.toLowerCase()) >= 0) return true
    } else {
      if (value.indexOf(expected) >= 0) return true;
    }
  }
  return false;
};

const found = containsExptected(array);
if (found) {
  console.log("Array contains food");
} else {
  console.log("Array does not contain food");
}

2
  • Isn't it >=0 instead of >0?
    – Deepak
    Commented Dec 22, 2021 at 1:48
  • Thanks@Deepak i fixed that in my code Commented Dec 22, 2021 at 2:01
0

Here's a one-liner to check for a match of substring in an array of strings:

const value = ["Grape | 100 |Food Catering Service", "Apple | 100,000m |Food Catering Service", "Water Melon | 100,000m |Catering Service Outside", "100,000m |Food Catering Service Outside"]

const expected = "Food"
const ignoreCase = true
console.log(value.some(val => (ignoreCase ? val.toLowerCase().includes(expected.toLowerCase()) : val.includes(expected))))

Not the answer you're looking for? Browse other questions tagged or ask your own question.