|
1 |
| -# Typed string interpolation |
| 1 | +# `typed-string-interpolation` |
2 | 2 |
|
3 | 3 | [String interpolation](https://en.wikipedia.org/wiki/String_interpolation) utility that returns the correct type based on passed in variable substitutions.
|
4 | 4 |
|
|
8 | 8 | - Sanity checks that correct variables were passed in
|
9 | 9 | - Returns the correct type based on passed in variable substitutions
|
10 | 10 | - Options to customize return, pattern matching and sanity checking
|
11 |
| -- Both `.cjs` and `mjs` distributions available. Use anywhere! |
| 11 | +- Both ES Module `.mjs` and CommonJS `.cjs` distributions available. Use anywhere! |
| 12 | +- Tiny footprint: |
| 13 | + - ES Module: `0.462kB` (`773B` unpacked) |
| 14 | + - CommonJS: `0.833B` (`1.75kB` unpacked) |
12 | 15 |
|
13 |
| -## Install |
| 16 | +## Motivation |
| 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. |
| 19 | + |
| 20 | +This package aims to provide a high quality string interpolation "primitive" to use as is or within other localization frameworks and tooling. |
| 21 | + |
| 22 | +## Getting started |
| 23 | + |
| 24 | +### Install |
14 | 25 |
|
15 | 26 | ```bash
|
16 | 27 | npm i typed-string-interpolation
|
17 | 28 | ```
|
18 | 29 |
|
19 |
| -## Usage |
| 30 | +### Usage |
20 | 31 |
|
21 | 32 | ```ts
|
22 | 33 | // ES module
|
@@ -59,14 +70,14 @@ If the string can be joined you'll get back a string. Otherwise a union type wit
|
59 | 70 | stringInterpolation("hello {{world}} with number {{number}}", {
|
60 | 71 | world: "world",
|
61 | 72 | number: 1,
|
62 |
| -}) // => Returns type: string |
| 73 | +}) // : string |
63 | 74 | ```
|
64 | 75 |
|
65 | 76 | ```tsx
|
66 | 77 | stringInterpolation("hello {{world}} with number {{number}}", {
|
67 | 78 | world: <span className="bold">world</span>,
|
68 | 79 | number: 1,
|
69 |
| -}) // => Returns type: (string | JSX.Element | number)[] |
| 80 | +}) // : (string | JSX.Element | number)[] |
70 | 81 | ```
|
71 | 82 |
|
72 | 83 | ## Options
|
@@ -97,7 +108,7 @@ stringInterpolation(
|
97 | 108 | number: 1,
|
98 | 109 | },
|
99 | 110 | { raw: true }
|
100 |
| -) // => Returns type: (string | number)[] |
| 111 | +) // : (string | number)[] |
101 | 112 | ```
|
102 | 113 |
|
103 | 114 | `pattern`
|
@@ -126,13 +137,20 @@ Turning of sanity checking removes `throw` from:
|
126 | 137 |
|
127 | 138 | ## Contributing
|
128 | 139 |
|
129 |
| -API suggestions are welcome and greatly appreciated. |
| 140 | +Easiest way to contribute is to open new issues for API suggestions and fixes. |
| 141 | +
|
| 142 | +### Contributing for a release |
130 | 143 |
|
131 | 144 | Basic steps for contributing for a release:
|
132 | 145 |
|
133 |
| -- Fork `main` |
134 |
| -- `npm ci` |
135 |
| -- Run tests `npm run test` |
136 |
| -- Create a changeset with `npx changeset` |
| 146 | +- Fork `main` on Github and clone fork locally |
| 147 | +- `npm ci` to install dependencies |
| 148 | +- Make changes while running tests |
| 149 | + - Unit test in watch mode: |
| 150 | + - `npm run test:unit:watch` |
| 151 | + - Unit test for types in watch mode: |
| 152 | + - `npm run test:unit:types:watch` |
| 153 | +- Once all changes are complete create a new release with changeset |
| 154 | + - `npm run changeset` |
137 | 155 | - Commit and push changes
|
138 |
| -- Open a pull request |
| 156 | +- Open a pull request for the fork |
0 commit comments