Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { useState } from "react";
  2. import { useRef } from "react";
  3.  
  4. const useFormulaKey = () => {
  5.   const previousKeys = useRef([]);
  6.  
  7.   const getKeyFromName = name => {
  8.     if (name && typeof name === "string") {
  9.       name = name.replace(/\s{2,}/g, " ");
  10.       let nameKey = name
  11.         .split(" ")
  12.         .map(n => n[0].toUpperCase())
  13.         .join("");
  14.       if (previousKeys.current.indexOf(nameKey) > -1) {
  15.         let previousSimilarKeys = previousKeys.current.filter(c =>
  16.           c.match(RegExp(nameKey + "[0-9]*"))
  17.         ).length;
  18.         nameKey = nameKey + previousSimilarKeys;
  19.       }
  20.       previousKeys.current.push(nameKey);
  21.       return `$F${nameKey}`;
  22.     }
  23.   };
  24.   return [getKeyFromName];
  25. };
  26.  
  27. export default useFormulaKey;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement