Skip to content

Commit 32c3d71

Browse files
committed
initial commit
0 parents  commit 32c3d71

17 files changed

+17384
-0
lines changed

.changeset/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changesets
2+
3+
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
4+
with multi-package repos, or single-package repos to help you version and publish your code. You can
5+
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
6+
7+
We have a quick list of common questions to get you started engaging with this project in
8+
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)

.changeset/config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "https://unpkg.com/@changesets/config@2.3.0/schema.json",
3+
"changelog": "@changesets/cli/changelog",
4+
"commit": false,
5+
"fixed": [],
6+
"linked": [],
7+
"access": "public",
8+
"baseBranch": "main",
9+
"updateInternalDependencies": "patch",
10+
"ignore": []
11+
}

.changeset/quick-bees-destroy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"typed-string-interpolation": patch
3+
---
4+
5+
Initial commit

.github/workflows/main.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- "**"
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
- uses: actions/setup-node@v3
13+
with:
14+
node-version: 18
15+
cache: "npm"
16+
- run: npm ci
17+
- run: npm run lint && npm run test && npm run build

.github/workflows/publish.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Publish
2+
3+
on:
4+
push:
5+
branches:
6+
- "main"
7+
8+
concurrency: ${{ github.workflow }}-${{ github.ref }}
9+
10+
jobs:
11+
publish:
12+
name: Publish
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout Repo
16+
uses: actions/checkout@v3
17+
18+
- name: Setup Node.js 18
19+
uses: actions/setup-node@v3
20+
with:
21+
node-version: 18
22+
23+
- name: Install Dependencies
24+
run: npm ci
25+
26+
- name: Create Release Pull Request or Publish
27+
id: changesets
28+
uses: changesets/action@v1
29+
with:
30+
# This expects you to have a script called release which does a build for your packages and calls changeset publish
31+
publish: npm run release
32+
env:
33+
GITHUB_TOKEN: ${{ secrets.SCOPED_GH_PAT_TOKEN }}
34+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
18

README.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Typed string interpolation
2+
3+
[String interpolation](https://en.wikipedia.org/wiki/String_interpolation) utility that returns the correct type based on passed in variable substitutions.
4+
5+
## Main features
6+
7+
- Replaces variables within a string with passed in variables
8+
- Sanity checks that correct variables were passed in
9+
- Returns the correct type based on passed in variable substitutions
10+
- Options to customize return, pattern matching and sanity checking
11+
- Both `.cjs` and `mjs` distributions available. Use anywhere!
12+
13+
## Install
14+
15+
```bash
16+
npm i typed-string-interpolation
17+
```
18+
19+
## Usage
20+
21+
```ts
22+
// ES module
23+
import { stringInterpolation } from "typed-string-interpolation"
24+
// CommonJS
25+
const { stringInterpolation } = require("typed-string-interpolation")
26+
```
27+
28+
```ts
29+
stringInterpolation("hello {{world}}", {
30+
world: "world",
31+
}) // "hello world"
32+
```
33+
34+
Pass in anything you want an get back sane results when interpolation result shouldn't be turned into a `string`:
35+
36+
```tsx
37+
stringInterpolation("hello {{world}} with {{anything}}", {
38+
world: "world",
39+
anything: <span className="bold">anything</span>,
40+
})
41+
```
42+
43+
Returns an array for easy use with libraries like `react` or anything else!
44+
45+
```tsx
46+
const interpolationResult = [
47+
"hello ",
48+
"world",
49+
" with ",
50+
<span className="bold">anything</span>,
51+
]
52+
```
53+
54+
## TypeScript support
55+
56+
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.
57+
58+
```ts
59+
stringInterpolation("hello {{world}} with number {{number}}", {
60+
world: "world",
61+
number: 1,
62+
}) // => Returns type: string
63+
```
64+
65+
```tsx
66+
stringInterpolation("hello {{world}} with number {{number}}", {
67+
world: <span className="bold">world</span>,
68+
number: 1,
69+
}) // => Returns type: (string | JSX.Element | number)[]
70+
```
71+
72+
## Options
73+
74+
Takes in an optional third parameter for options:
75+
76+
```js
77+
stringInterpolation(str, variables, options)
78+
```
79+
80+
```ts
81+
type Options = {
82+
raw?: boolean // default: false
83+
pattern?: RegExp // default: new RegExp(/\{{([^{]+)}}/g)
84+
sanity?: boolean // default: true
85+
}
86+
```
87+
88+
`raw`
89+
90+
Return the raw interpolation results without joining to string when you want full control for some reason.
91+
92+
```tsx
93+
stringInterpolation(
94+
"hello {{world}} with number {{number}}",
95+
{
96+
world: "world",
97+
number: 1,
98+
},
99+
{ raw: true }
100+
) // => Returns type: (string | number)[]
101+
```
102+
103+
`pattern`
104+
105+
Provide your own `RegExp` pattern for variable matching. Must be defined as:
106+
107+
```ts
108+
pattern: new RegExp(/\{{([^{]+)}}/g)
109+
```
110+
111+
`sanity`
112+
113+
If you want to live dangerously, sanity checking can be turned off.
114+
115+
```ts
116+
{
117+
sanity: false
118+
}
119+
```
120+
121+
Turning of sanity checking removes `throw` from:
122+
123+
- empty string
124+
- string variables and passed in variables count mismatch
125+
- missing variables
126+
127+
## Contributing
128+
129+
API suggestions are welcome and greatly appreciated.
130+
131+
Basic steps for contributing for a release:
132+
133+
- Fork `main`
134+
- `npm ci`
135+
- Run tests `npm run test`
136+
- Create a changeset with `npx changeset`
137+
- Commit and push changes
138+
- Open a pull request

babel.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
presets: [
3+
["@babel/preset-env", { targets: { node: "current" } }],
4+
"@babel/preset-typescript",
5+
],
6+
}

jest.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { Config } from "jest"
2+
3+
const config: Config = {
4+
verbose: true,
5+
testMatch: ["<rootDir>/src/__tests__/*.ts"],
6+
}
7+
8+
export default config

jest.config.tsd.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
displayName: {
3+
color: "blue",
4+
name: "types",
5+
},
6+
runner: "jest-runner-tsd",
7+
testMatch: ["**/__typetests__/*.test.ts*"],
8+
}

0 commit comments

Comments
 (0)