Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
06b77010
Commit
06b77010
authored
Dec 14, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
Dec 14, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix trim-and-dedent for interpolated vars with text after
parent
317b2d87
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
9 deletions
+34
-9
trim-and-dedent-test.js
src/shared/test/trim-and-dedent-test.js
+27
-0
trim-and-dedent.ts
src/shared/trim-and-dedent.ts
+7
-9
No files found.
src/shared/test/trim-and-dedent-test.js
View file @
06b77010
...
...
@@ -53,6 +53,33 @@ lines
with no indentation
`
,
],
[
()
=>
{
const
name
=
`Jane`
;
const
secondVar
=
`
multiple
lines
with no indentation
`
;
return
trimAndDedent
`
Hello,
${
name
}
!
Indented line
Goodbye, John!
text
${
secondVar
}
more text
`
;
},
`Hello, Jane!
Indented line
Goodbye, John!
text
multiple
lines
with no indentation
more text`
,
],
].
forEach
(([
getResult
,
expectedResult
])
=>
{
it
(
'normalizes strings with multiple lines'
,
()
=>
{
assert
.
equal
(
getResult
(),
expectedResult
);
...
...
src/shared/trim-and-dedent.ts
View file @
06b77010
...
...
@@ -19,16 +19,14 @@ function dedentStr(str: string, indent: number) {
* params verbatim.
*/
function
dedent
(
strings
:
string
[],
...
params
:
any
[])
{
// Match the smallest indentation among all strings
const
indents
=
strings
.
map
(
str
=>
{
const
match
=
str
.
match
(
/^
[
\t]
*
(?=\S)
/gm
);
return
match
?
Math
.
min
(...
match
.
map
(
el
=>
el
.
length
))
:
-
1
;
})
// Exclude lines where indentation could not be matched
.
filter
(
num
=>
num
>
-
1
);
const
smallestIndent
=
indents
.
length
>
0
?
Math
.
min
(...
indents
)
:
0
;
// Match the smallest indentation among all lines of the full string before
// interpolating params
const
match
=
strings
.
join
(
''
).
match
(
/^
[
\t]
*
(?=\S)
/gm
);
const
smallestIndent
=
match
?
Math
.
min
(...
match
.
map
(
el
=>
el
.
length
))
:
0
;
// Dedent every individual string while interpolating params
// Those strings which are not the beginning of a line will have zero-indent
// and dedenting will have no effect
let
result
=
''
;
for
(
const
[
i
,
param
]
of
params
.
entries
())
{
result
+=
dedentStr
(
strings
[
i
],
smallestIndent
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment