Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { useState } from "react";
- import { useRef } from "react";
- const useFormulaKey = () => {
- const previousKeys = useRef([]);
- const getKeyFromName = name => {
- if (name && typeof name === "string") {
- name = name.replace(/\s{2,}/g, " ");
- let nameKey = name
- .split(" ")
- .map(n => n[0].toUpperCase())
- .join("");
- if (previousKeys.current.indexOf(nameKey) > -1) {
- let previousSimilarKeys = previousKeys.current.filter(c =>
- c.match(RegExp(nameKey + "[0-9]*"))
- ).length;
- nameKey = nameKey + previousSimilarKeys;
- }
- previousKeys.current.push(nameKey);
- return `$F${nameKey}`;
- }
- };
- return [getKeyFromName];
- };
- export default useFormulaKey;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement