Skip to content

Commit bf8aa65

Browse files
committed
refactor: reduce library weight even more
1 parent 70adcc2 commit bf8aa65

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

.changeset/little-lemons-push.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+
Even smaller library weight

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
- Options to customize return, pattern matching and sanity checking
1616
- Both ES Module and CommonJS distributions available. Use anywhere!
1717
- Tiny footprint:
18-
- ES Module: `396B` Gzipped (`572B` unpacked)
19-
- CommonJS: `634B` Gzipped (`1.04kB` unpacked)
18+
- ES Module: `383B` Gzipped (`553B` unpacked)
19+
- CommonJS: `623B` Gzipped (`1.02kB` unpacked)
2020

2121
## Motivation
2222

@@ -95,7 +95,7 @@ stringInterpolation(str, variables, options)
9595
```ts
9696
type Options = {
9797
raw?: boolean // default: false
98-
pattern?: RegExp // default: new RegExp(/\{{([^{]+)}}/g)
98+
pattern?: RegExp // default: /\{{([^{]+)}}/g
9999
sanity?: boolean // default: true
100100
}
101101
```
@@ -120,7 +120,7 @@ stringInterpolation(
120120
Provide your own `RegExp` pattern for variable matching. Must be defined as:
121121
122122
```ts
123-
pattern: new RegExp(/\{{([^{]+)}}/g) // Default
123+
pattern: /\{{([^{]+)}}/g // Default
124124
```
125125
126126
Example alternative pattern:

src/index.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,52 +32,52 @@ export function stringInterpolation<
3232
string: string,
3333
variables: Record<PropertyKey, VariableValue>,
3434
{
35-
pattern = new RegExp(/\{{([^{]+)}}/g),
35+
pattern = /\{{([^{]+)}}/g,
3636
sanity = true,
37-
raw: rawOutput = false,
37+
raw = false,
3838
}: StringInterpolationOptions<OptionRaw> = {},
3939
): StringInterpolationReturn<VariableValue, OptionRaw> {
4040
if (!string && sanity) throw new Error("Empty string")
4141

42-
const rawInterpolation: (string | VariableValue)[] = []
43-
let lastIndex = 0
44-
let matchCount = 0
45-
let canJoin = true
4642
const variableKeys = sanity ? Object.keys(variables) : undefined
43+
const segments: (string | VariableValue)[] = []
44+
let lastIndex = 0
45+
let foundCount = 0
46+
let joinable = true
4747

4848
let m: RegExpExecArray | null
4949
while ((m = pattern.exec(string))) {
50-
const idx = m.index || 0
50+
const idx = m.index
5151
const full = m[0]
5252
const key = m[1]
5353

54-
if (idx > lastIndex) rawInterpolation.push(string.slice(lastIndex, idx))
54+
if (idx > lastIndex) segments.push(string.slice(lastIndex, idx))
5555

5656
if (sanity && key && !variableKeys!.includes(key))
5757
throw new Error(`Variable '${key}' not found`)
5858

5959
const value = variables[key as unknown as PropertyKey]
60-
if (canJoin && typeof value !== "string" && typeof value !== "number")
61-
canJoin = false
62-
rawInterpolation.push(value)
60+
joinable =
61+
joinable && (typeof value === "string" || typeof value === "number")
62+
segments.push(value)
6363

6464
lastIndex = idx + full.length
65-
matchCount++
65+
foundCount++
6666
}
6767

68-
if (!matchCount)
68+
if (!foundCount)
6969
return string as StringInterpolationReturn<VariableValue, OptionRaw>
7070

71-
if (lastIndex < string.length) rawInterpolation.push(string.slice(lastIndex))
71+
if (lastIndex < string.length) segments.push(string.slice(lastIndex))
7272

73-
if (sanity && matchCount !== variableKeys!.length)
73+
if (sanity && foundCount !== variableKeys!.length)
7474
throw new Error("Variable count mismatch")
7575

76-
if (canJoin && !rawOutput)
77-
return rawInterpolation.join("") as StringInterpolationReturn<
76+
if (joinable && !raw)
77+
return segments.join("") as StringInterpolationReturn<
7878
VariableValue,
7979
OptionRaw
8080
>
8181

82-
return rawInterpolation as StringInterpolationReturn<VariableValue, OptionRaw>
82+
return segments as StringInterpolationReturn<VariableValue, OptionRaw>
8383
}

0 commit comments

Comments
 (0)