Skip to content

Commit 21fcfe9

Browse files
committed
feat: String.matchAll polyfill
1 parent 45d1fec commit 21fcfe9

File tree

6 files changed

+60
-10
lines changed

6 files changed

+60
-10
lines changed

.changeset/weak-points-brake.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+
Polyfill for String.matchAll

.vscode/launch.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
// https://github.com/microsoft/vscode-recipes/tree/main/debugging-jest-tests
9+
// https://www.youtube.com/watch?v=_usf3xQ7wys
10+
"type": "node",
11+
"request": "launch",
12+
"name": "Jest Current File",
13+
"program": "${workspaceFolder}/node_modules/.bin/jest",
14+
"args": [
15+
"--runTestsByPath",
16+
"${relativeFile}",
17+
"--config",
18+
"jest.config.ts"
19+
],
20+
"console": "integratedTerminal",
21+
"internalConsoleOptions": "neverOpen"
22+
}
23+
]
24+
}

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
- Options to customize return, pattern matching and sanity checking
1515
- Both ES Module and CommonJS distributions available. Use anywhere!
1616
- Tiny footprint:
17-
- ES Module: `0.354kB` (`0.481kB` unpacked)
18-
- CommonJS: `0.589kB` (`0.977kB` unpacked)
17+
- ES Module: `0.379B` (`0.533B` unpacked)
18+
- CommonJS: `0.612B` (`1.03kB` unpacked)
1919

2020
## Motivation
2121

@@ -144,8 +144,14 @@ Steps for contributing through a pull request:
144144
- `npm ci`
145145
- Make changes while running tests in watch mode
146146
- `npm run test:unit:all:watch`
147+
- This project has a `.vscode/launch.json` file containing configuration for running Jest tests with the VSCode debugger which makes it simple to step through logic excecution. Steps to use VSCode debugger:
148+
- Add a breakpoint to the source code
149+
- Open a Jest unit test file (`*.test.ts`)
150+
- Go to the VSCode debugger Tab (`shift` + `command` + `D` on MacOS) and select "Jest Current File" or optionally start the debug session from the command line (`shift` + `command` + `P` on MacOS) and type "Debug: Start debugging"
151+
- VSCode should open a new terminal window and attach the Jest instance to the debugger
152+
- Debugger should stop on the defined breakpoint in the source code
147153
- Once all changes are complete, create a new release with [changesets](https://github.com/changesets/changesets)
148-
- `npm run changeset`
154+
- `npm run create-release`
149155
- Commit and push changes to fork
150156
- Open a pull request against the fork
151157
- If the PR needs changes before a merge to `main` can be made, push more changes to the fork until the PR is approved

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"test": "npm run test:unit && npm run test:unit:types",
4343
"build": "tsup src/index.ts --format cjs,esm --dts --minify",
4444
"release": "npm run build && changeset publish",
45-
"changeset": "changeset"
45+
"create-release": "changeset"
4646
},
4747
"devDependencies": {
4848
"@babel/core": "^7.20.12",

src/__tests__/stringInterpolation.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { stringInterpolation } from "../index"
22

3-
describe("interpolation()", () => {
3+
describe("stringInterpolation()", () => {
44
// Throws
55
test("Empty", () => {
66
expect(() => {
@@ -43,7 +43,7 @@ describe("interpolation()", () => {
4343
)
4444
).toStrictEqual(["Hello ", "world with variable"])
4545
})
46-
test("Interpolate two variables", () => {
46+
test.only("Interpolate two variables", () => {
4747
expect(
4848
stringInterpolation("Hello {{world}} and {{anotherVariable}}", {
4949
world: "world with variable",

src/index.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ type StringInterpolationReturn<VariableValue extends any, OptionRaw> = Exclude<
1313
: string
1414
: (string | VariableValue)[]
1515

16+
/**
17+
* String.matchAll polyfill
18+
* Used because no support in Safari <= 12
19+
* @see https://caniuse.com/mdn-javascript_builtins_string_matchall
20+
* @see https://stackoverflow.com/questions/58003217/how-to-use-the-string-prototype-matchall-polyfill
21+
*/
22+
function matchAllPolyfill(string: string, pattern: RegExp) {
23+
let match
24+
const matches = []
25+
26+
while ((match = pattern.exec(string))) matches.push(match)
27+
28+
return matches
29+
}
30+
1631
/**
1732
* Takes in a string containing variables and an object containing variables for interpolation. Accepts options.
1833
*
@@ -42,7 +57,7 @@ export function stringInterpolation<
4257
if (!string && sanity) throw "Empty string"
4358

4459
// Find all variables within string
45-
const stringVariables = [...string.matchAll(pattern)]
60+
const stringVariables = matchAllPolyfill(string, pattern)
4661

4762
// No variables => no need to interpolate
4863
if (!stringVariables[0])
@@ -55,9 +70,9 @@ export function stringInterpolation<
5570
if (stringVariables.length !== variableKeys.length)
5671
throw "Variable count mismatch"
5772
for (const regExpMatchArray of stringVariables) {
58-
const variable = regExpMatchArray[1]
59-
if (variable && !variableKeys.includes(variable))
60-
throw `Variable '${variable}' not found`
73+
const variableKeyInString = regExpMatchArray[1]
74+
if (variableKeyInString && !variableKeys.includes(variableKeyInString))
75+
throw `Variable '${variableKeyInString}' not found`
6176
}
6277
}
6378

0 commit comments

Comments
 (0)