SHARE
TWEET

Untitled

a guest Jun 26th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let createPattern = val => new RegExp(`(${val})`, "ig");
  2.  
  3. // matchHighlighter({
  4. //     searchText: "礼物",
  5. //     items: [
  6. //       {
  7. //         name: "礼物",
  8. //         id: "谁的",
  9. //         kk: "礼sdfsdfjk物"
  10. //       }
  11. //     ]
  12. //   })
  13.  
  14. export function matchHighlighter({
  15.   searchText,
  16.   items = [],
  17.   pick = [],
  18.   styleRule = "color: red"
  19. }) {
  20.   let pat = createPattern(searchText);
  21.   if (!pick.length) {
  22.     let keyDict = new Set();
  23.     items.forEach(item => {
  24.       Object.keys(item).forEach(k => keyDict.add(k));
  25.     });
  26.     pick = [...keyDict];
  27.   }
  28.   return items.map(item => {
  29.     let search = {};
  30.     pick.forEach(key => {
  31.       if (item[key] && pat.test(item[key])) {
  32.         search[key] = item[key].replace(
  33.           pat,
  34.           `<span style="${styleRule}">$1</span>`
  35.         );
  36.       }
  37.     });
  38.     return {
  39.       ...item,
  40.       _matchHighlighted: search
  41.     };
  42.   });
  43. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top