Ad

The object array in this format

const items = [
{
"name":"MEMORY 3600 DIMM KINGSTON",
"code":"445sdp"
},
{
"name":"SSD KINGSTON",
"code":"445sdA"
},
{
"name":"MEMORY HP 3600",
"code":"445sdD"
}
];

Your work is filter this array

if the text to search has a space, it must have the name of the two words
filterData(array,"MEMO hp")->[{"name":"MEMORY HP 3600","code":"445sdD"}]

filterData(items,"445sdp")-> [{
"name":"MEMORY 3600 DIMM KINGSTON"
"code":"445sdp"
}]
and if you do not have spaces you must search by name and by code

filterData(items,"memo /-KINGSTON")-> [{
"name":"MEMORY HP 3600"
"code":"445sdD"
}]
and if the name contains /- everything that goes next to it must be excluded for example

if text to search is '' or null retun all array

function filterData(items,search){
  if (!search || search?.length == 0) {
		return items;
	}
	
	let searchInput=search.replace(/\s\s+/g," ");
	let textNegate=searchInput.split(' ').filter(rs=>rs.startsWith('/-')).map(rs=>rs.replace('/-',''));
	    searchInput=searchInput.replace(/[\s][/-]+.*/g, '');

	    searchInput=searchInput.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&').toLowerCase();
	let seachArray=searchInput.split(' ');
		seachArray=seachArray.filter(rs=>!rs.startsWith('\-'));

	return items.filter(rs=>{
            let text=rs.name.toLowerCase();

            // test if contain only on word negated
            if(textNegate.length==1 && textNegate[0].length>0){
              if(text.search(textNegate[0].toLowerCase())>=0)
              {
                "negate one word"
                return false;
              }
            }


            if(text.search(search)>=0)
              return true;

            if(seachArray.length==1 && rs.code.search(search)>=0){
              return true;
            }

            let result=false;
            for(let i=0;i<seachArray.length;i++){
              if(!new RegExp(`${seachArray[i]}.*`).test(text)){
              result=false;
              break;
              }
              result=true;
            }
            return result;
        });
  }