• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ddadaal / react-typed-i18n / 11818136938

13 Nov 2024 01:20PM UTC coverage: 98.0% (-2.0%) from 100.0%
11818136938

push

github

ddadaal
chore: fix package.json pnpm version

36 of 37 branches covered (97.3%)

Branch coverage included in aggregate %.

111 of 113 relevant lines covered (98.23%)

30.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.51
/src/utils.ts
1
import React from "react";
2
import { invalidIdError } from "./errors";
4✔
3
import { Definitions } from "./types";
4

5
export function replacePlaceholders(
4✔
6
  definition: string,
7
  args: React.ReactNode[] | Record<string, React.ReactNode>,
8
): React.ReactNode | string {
9

10
  let head = 0, index = 0;
24✔
11

12
  let valueArgs: React.ReactNode[] | undefined = undefined;
24✔
13

14
  const results = [] as React.ReactNode[];
24✔
15
  let escaped = false;
24✔
16

17
  let allString = true;
24✔
18

19
  const append = (text: string | React.ReactNode | undefined) => {
24✔
20

21
    if (typeof text !== "string") {
281✔
22
      results.push(text ?? "");
5✔
23
      allString = false;
5✔
24
      return;
5✔
25
    }
26

27
    if (results.length === 0) {
276✔
28
      results.push(text);
24✔
29
    } else {
30
      if (typeof results[results.length - 1] === "string") {
252✔
31
        results[results.length - 1] += text;
250✔
32
      } else {
33
        results.push(text);
2✔
34
      }
35
    }
36
  };
37

38
  while (head < definition.length) {
24✔
39
    if (definition[head] === "\\") {
285✔
40
      if (escaped) {
6✔
41
        append("\\");
2✔
42
        escaped = false;
2✔
43
      } else {
44
        escaped = true;
4✔
45
      }
46
      head++;
6✔
47
    } else if (definition[head] === "{") {
279✔
48
      if (escaped) {
30✔
49
        append("\\{");
2✔
50
        escaped = false;
2✔
51
        head++;
2✔
52
      } else {
53
        head++;
28✔
54
        if (head < definition.length) {
28✔
55
          let key = "";
28✔
56
          while (head < definition.length && definition[head] !== "}") {
28✔
57
            key += definition[head++];
31✔
58
          }
59
          if (head === definition.length) {
28!
60
            append("{" + key);
×
61
            break;
×
62
          } else {
63
            head++;
28✔
64
            if (key === "") {
28✔
65
              if (!valueArgs) {
15✔
66
                valueArgs = Array.isArray(args) ? args : Object.values(args);
9✔
67
              }
68
              append(valueArgs[index++]);
15✔
69
            } else {
70
              append(args[key]);
13✔
71
            }
72
          }
73
        }
74
      }
75
    } else {
76
      append(definition[head]);
249✔
77
      head++;
249✔
78
    }
79
  }
80

81
  return allString ? results.join("") : results;
24✔
82
}
83

84

85
export const getDefinition = (definitions: Definitions, id: string): string => {
4✔
86
  let content = definitions;
32✔
87
  for (const key of id.split(".")) {
48✔
88
    if (typeof content === "undefined") {
48✔
89
      throw invalidIdError(id);
4✔
90
    }
91
    // I know what I am doing. Trust me :)
92
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
93
    content = content[key] as any;
44✔
94
  }
95
  if (typeof content !== "string") {
28✔
96
    throw invalidIdError(id);
4✔
97
  }
98
  return content;
24✔
99
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc