You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+19-24Lines changed: 19 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -10,17 +10,21 @@
10
10
- Options to customize return, pattern matching and sanity checking
11
11
- Both ES Module `.mjs` and CommonJS `.cjs` distributions available. Use anywhere!
12
12
- Tiny footprint:
13
-
- ES Module: `0.462kB` (`773B` unpacked)
14
-
- CommonJS: `0.833B` (`1.75kB` unpacked)
13
+
- ES Module: `0.46kB` (`0.77kB` unpacked)
14
+
- CommonJS: `0.83kB` (`1.75kB` unpacked)
15
15
16
16
## Motivation
17
17
18
-
String interpolation/variable substitution (i.e. injecting variables within text) is a really common operation when building single and multilingual applications. Existing string interpolation utilities within the most used `i18n` / `l10n` packages like `i18next` and `formatjs` come with massive overhead while lacking thing like proper TypeScript infer support for the interpolation operation.
18
+
String interpolation/variable substitution (i.e. injecting variables within text) is a really common operation when building single and multilingual applications. Existing string interpolation utilities within the most used `i18n` / `l10n` packages like `i18next` and `formatjs` come with massive overhead while lacking proper TypeScript infer support for the interpolation operation.
19
19
20
20
This package aims to provide a high quality string interpolation "primitive" to use as is or within other localization frameworks and tooling.
21
21
22
22
## Getting started
23
23
24
+
Easiest way to get started is to play around with a [React example sandbox](https://codesandbox.io/p/sandbox/typed-string-interpolation-react-example-slpjgp).
25
+
26
+
> ℹ Note that the library itself is framework agnostic and could be used with anything.
27
+
24
28
### Install
25
29
26
30
```bash
@@ -36,35 +40,26 @@ import { stringInterpolation } from "typed-string-interpolation"
Returns a `string` when the result can be joined into a string.
44
+
39
45
```ts
40
46
stringInterpolation("hello {{world}}", {
41
47
world: "world",
42
48
}) // "hello world"
43
49
```
44
50
45
-
Pass in anything you want an get back sane results when interpolation result shouldn't be turned into a `string`:
51
+
Returns an array when the result can't be joined into a `string`. This makes it really easy to use the utility with libraries like `react` or anything else.
46
52
47
53
```tsx
48
54
stringInterpolation("hello {{world}} with {{anything}}", {
49
55
world: "world",
50
-
anything: <spanclassName="bold">anything</span>,
51
-
})
52
-
```
53
-
54
-
Returns an array for easy use with libraries like `react` or anything else!
55
-
56
-
```tsx
57
-
const interpolationResult = [
58
-
"hello ",
59
-
"world",
60
-
" with ",
61
-
<spanclassName="bold">anything</span>,
62
-
]
56
+
anything: <strong>anything</strong>,
57
+
}) // ["hello ", "world", " with ", <strong>anything</strong>]
63
58
```
64
59
65
60
## TypeScript support
66
61
67
-
If the string can be joined you'll get back a string. Otherwise a union type within an array is returned based on the passed in variables.
62
+
If the string can be joined you'll get back a `string` type. Otherwise a ` type within an array is returned based on the passed in variables.
68
63
69
64
```ts
70
65
stringInterpolation("hello {{world}} with number {{number}}", {
@@ -75,7 +70,7 @@ stringInterpolation("hello {{world}} with number {{number}}", {
75
70
76
71
```tsx
77
72
stringInterpolation("hello {{world}} with number {{number}}", {
0 commit comments