All files / src/util string.tsx

100% Statements 39/39
100% Branches 10/10
100% Functions 6/6
100% Lines 39/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103      6x       8x 8x   2x         5x   5x 5x     5x 4x 4x     4x 3x       4x           4x       5x 4x     5x     1x         4x   4x 4x   4x   4x 5x 5x 5x     5x 3x       5x 4x             1x     5x       4x 1x     4x     1x       4x 3x   4x    
import React from "react";
 
export function capitalise(val: string) {
  return String(val).charAt(0).toUpperCase() + String(val).slice(1);
}
 
export function stringifyUnknown(value: unknown): string {
  try {
    return JSON.stringify(value);
  } catch (err) {
    return `"Unserializable value"`;
  }
}
 
export function renderWithHighlightedVariables(input: string): React.ReactNode {
  const regex = /(?<!\\)(\$\{[^}]+\})/g;
 
  const parts: React.ReactNode[] = [];
  let lastIndex = 0;
  let match;
 
  while ((match = regex.exec(input)) !== null) {
    const start = match.index;
    const end = regex.lastIndex;
 
    // Add text before the match
    if (start > lastIndex) {
      parts.push(input.slice(lastIndex, start));
    }
 
    // Add highlighted variable
    parts.push(
      <span key={start} className="bold">
        {match[1]}
      </span>
    );
 
    lastIndex = end;
  }
 
  // Add the rest of the text after the last match
  if (lastIndex < input.length) {
    parts.push(input.slice(lastIndex));
  }
 
  return <>"{parts}"</>;
}
 
export const renderedTemplate = (
  template: string,
  args: Record<string, string>
) => {
  // Regex to match ${key}, ignoring escaped ones
  const regex = /(?<!\\)\$\{([^}]+)\}/g;
 
  const parts: React.ReactNode[] = [];
  let lastIndex = 0;
  let match;
  let keyIndex = 0;
 
  while ((match = regex.exec(template)) !== null) {
    const start = match.index;
    const end = regex.lastIndex;
    const varName = match[1];
 
    // Push text before match
    if (start > lastIndex) {
      parts.push(template.slice(lastIndex, start));
    }
 
    // Replace with span if variable exists
    if (Object.prototype.hasOwnProperty.call(args, varName)) {
      parts.push(
        <span key={keyIndex++} className="bold">
          {args[varName]}
        </span>
      );
    } else {
      // Fallback: keep the original `${varName}`
      parts.push(template.slice(start, end));
    }
 
    lastIndex = end;
  }
 
  // Push remaining text
  if (lastIndex < template.length) {
    parts.push(template.slice(lastIndex));
  }
 
  return <>"{parts}"</>;
};
 
export const renderedStrTemplate = (
  template: string,
  args: Record<string, string>
) => {
  Object.entries(args).forEach(([key, value]) => {
    template = template.replace("${" + key + "}", value);
  });
  return template;
};