All files / format-string index.ts

95.34% Statements 82/86
83.33% Branches 10/12
100% Functions 1/1
95.34% Lines 82/86

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 871x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 7x 7x 7x 10x 10x 10x 10x         9x 9x 9x 9x 10x 19x 19x 19x 9x 19x 18x 18x 18x 18x 18x 19x 10x 10x 10x 1x 1x  
import escapeRegex from "../libraries/escape-regexp-component";
 
/**
 * Formats a string with the given replacements.
 * @param {string} start - the start of the separator string or the entire separator string
 * @param {string} [end] - the start of the separator
 * @example
```
formatString("Hello {name}", { name:"Dolores" })
> Hello Dolores
```
```
formatString("Hello {{name}}", { name:"Dolores" }, "{{}}")
> Hello Sameer
```
 * @example
```
formatString("Hi, my name is {name} and I am {age} years old.", { name: "Dolores", age: 24000 }, "{}")
> Hi, my name is Dolores and I am 24000 years old.
```
 * @example
```
formatString("Hi, my name is {#name#} and I am {#age#} years old.", { name: "Dolores", age: 24000 }, "{#", "#}")
> Hi, my name is Dolores and I am 24000 years old.
```
 * @example
```
formatString("Hi, my name is {#name#} and I am {#age#} years old.", { name: "Dolores", age: 24000 }, "{##}")
> Hi, my name is Dolores and I am 24000 years old.
```
 * @returns
 */
function formatString(
  text: string,
  replacements: Record<string, string | number>,
  start?: string,
  end?: string
): string;
function formatString(
  text: string,
  replacements: Record<string, string | number>,
  start: string,
  end?: string
): string;
function formatString(
  text: string,
  replacements: Record<string, string | number>,
  start: string,
  end: string
): string {
  if (!start && end) end = undefined;
  if (!start) start = "{}";
 
  if (typeof end === "undefined") {
    end = start.substring(start.length / 2);
    start = start.substring(0, start.length / 2);
  }
 
  if (start === end) return text;
 
  if (!start.replace(/\s/g, "").length || !end.replace(/\s/g, "").length) {
    throw new Error(
      "You cannot only use whitespace as a separator. Please include a character for specifying separators better."
    );
  }
 
  const startRegex = escapeRegex(start);
  const endRegex = escapeRegex(end);
 
  for (const arg in replacements) {
    if (
      typeof replacements[arg] === "string" ||
      typeof replacements[arg] === "boolean" ||
      typeof replacements[arg] === "number"
    ) {
      text = text.replace(
        new RegExp(startRegex + arg + endRegex, "gi"),
        replacements[arg].toString()
      );
    }
  }
 
  return text;
}
 
export default formatString;