Skip to content

Commit b949828

Browse files
committed
refactor: remove un-needed function calls
1 parent 9a69ff7 commit b949828

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

src/index.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// aliased utils
22
const isA = Array.isArray
3-
const replace = (n, ...e) => n.replace(...e)
43

54
// https://github.com/justin-schroeder/arrow-js/blob/31c1861075aabe29b67620b58a33c7fecb209c8f/src/html.ts#L166C1-L166C23
65
const delimiter = '➳❍'
76
const delimiterComment = `<!--${delimiter}-->`
7+
const delimiterRegexGlobal = new RegExp(delimiterComment, 'g')
88

99
//FIXME: Don't really need to handle
1010
// const bookend = '❍⇚'
@@ -14,7 +14,6 @@ const eventRegex = /(@)(\w+)=["']$/
1414

1515
export function renderToString(template) {
1616
const isT = 'isT' in template
17-
1817
// FIXME: not a template, throw an error instead,
1918
// move the fault tolerant behavior to another function
2019
if (!isT) {
@@ -25,67 +24,68 @@ export function renderToString(template) {
2524

2625
let htmlString = renderResult[0]
2726
const expressions = renderResult[1]
28-
let index = -1
2927

3028
if (!expressions.length) return htmlString
3129

32-
return replace(
33-
htmlString,
34-
new RegExp(delimiterComment, 'g'),
35-
(...matchers) => {
36-
const str = matchers[0]
37-
const matchedAt = matchers[1]
38-
const matchedString = matchers[2]
39-
index += 1
40-
41-
const beforeDelim = matchedString.slice(0, matchedAt)
42-
const immediatelyFollowed = eventRegex.test(beforeDelim)
43-
if (immediatelyFollowed) {
44-
return str
45-
}
46-
return interpolateExpressions(str, expressions[index])
30+
return htmlString.replace(delimiterRegexGlobal, matchReplace(expressions))
31+
}
32+
33+
function matchReplace(expressions) {
34+
let index = -1
35+
return (...matchers) => {
36+
const str = matchers[0]
37+
const matchedAt = matchers[1]
38+
const matchedString = matchers[2]
39+
index += 1
40+
41+
const beforeDelim = matchedString.slice(0, matchedAt)
42+
const immediatelyFollowed = eventRegex.test(beforeDelim)
43+
44+
// check if we are on a event handler delimiter
45+
// while rendering strings, we don't need this information
46+
// so we remove it
47+
if (immediatelyFollowed) {
48+
return ''
4749
}
48-
)
50+
51+
return interpolateExpressions(expressions[index])
52+
}
4953
}
5054

51-
function interpolateExpressions(htmlString, expressionInstance) {
55+
// Replace the htmlString's next occuring
56+
function interpolateExpressions(expressionInstance) {
5257
const isExpressionReactive = expressionInstance && expressionInstance.e
5358
const isExpressionPartial = isExpressionReactive.isT
5459

5560
if (!isExpressionReactive) {
56-
return replace(htmlString, delimiterComment, expressionInstance())
61+
return expressionInstance()
5762
}
5863

5964
if (isExpressionPartial) {
60-
return replace(
61-
htmlString,
62-
delimiterComment,
63-
renderToString(expressionInstance.e)
64-
)
65+
return renderToString(expressionInstance.e)
6566
}
6667

6768
const watcherReturn = expressionInstance.e()
6869

6970
if (
7071
typeof watcherReturn !== 'object' &&
71-
typeof watcherReturn !== 'function' &&
72-
!isA(watcherReturn)
72+
typeof watcherReturn !== 'function'
7373
) {
74-
return replace(htmlString, delimiterComment, watcherReturn)
74+
return watcherReturn
7575
}
7676

7777
if (isA(watcherReturn)) {
78-
const result = watcherReturn.map(x => {
78+
return watcherReturn.reduce((acc, x) => {
7979
if ('isT' in x) {
80-
return renderToString(x)
80+
return acc + renderToString(x)
8181
}
82-
return x
83-
})
84-
return result.join('')
82+
return acc + x
83+
}, '')
8584
}
8685

8786
if (watcherReturn && watcherReturn.isT) {
88-
const _nestedHtmlString = renderToString(watcherReturn)
89-
return replace(htmlString, delimiterComment, _nestedHtmlString)
87+
return renderToString(watcherReturn)
9088
}
89+
90+
return ''
9191
}

tests/basic.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ test('events', async () => {
130130

131131
await inlineSnapshot(
132132
out,
133-
`<button @click="<!--➳❍-->">
133+
`<button @click="">
134134
0
135135
</button>`
136136
)
@@ -150,8 +150,8 @@ test('multiple events', async () => {
150150
await inlineSnapshot(
151151
out,
152152
`<button
153-
@click="<!--➳❍-->"
154-
@focus="<!--➳❍-->"
153+
@click=""
154+
@focus=""
155155
>
156156
0
157157
</button>`
@@ -181,12 +181,12 @@ test('nested events', async () => {
181181
out,
182182
`<button
183183
value="1"
184-
@click="<!--➳❍-->"
185-
@focus="<!--➳❍-->"
184+
@click=""
185+
@focus=""
186186
>
187187
0
188188
<input
189-
@focus="<!--➳❍-->"
189+
@focus=""
190190
/>
191191
192192
</button>`

0 commit comments

Comments
 (0)