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
98ced81f
Commit
98ced81f
authored
Nov 21, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
Nov 21, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add helper function to trim and dedent multiline strings
parent
bfe223ac
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
88 additions
and
0 deletions
+88
-0
trim-and-dedent-test.js
src/shared/test/trim-and-dedent-test.js
+33
-0
trim-and-dedent.ts
src/shared/trim-and-dedent.ts
+55
-0
No files found.
src/shared/test/trim-and-dedent-test.js
0 → 100644
View file @
98ced81f
import
{
trimAndDedent
}
from
'../trim-and-dedent'
;
describe
(
'trimAndDedent'
,
()
=>
{
[
[
'Foo'
,
'Foo'
],
[
' Foo'
,
'Foo'
],
[
`First line
Second line
Third line`
,
`First line
Second line
Third line`
,
],
[
`
Hello, Jane!
Indented line
Goodbye, John!
`
,
`Hello, Jane!
Indented line
Goodbye, John!`
,
],
].
forEach
(([
str
,
expectedResult
])
=>
{
it
(
'normalizes strings with multiple lines'
,
()
=>
{
const
result
=
trimAndDedent
(
str
);
assert
.
equal
(
result
,
expectedResult
);
});
});
});
src/shared/trim-and-dedent.ts
0 → 100644
View file @
98ced81f
/**
* Remove leading and trailing empty lines from a string.
*/
function
trimEmptyLines
(
str
:
string
):
string
{
return
str
.
replace
(
/^
\s
*
\n
|
\n\s
*$/g
,
''
);
}
/**
* Remove common indentation from each line of a string.
*/
function
dedent
(
str
:
string
)
{
// Match the smallest indentation
const
match
=
str
.
match
(
/^
[
\t]
*
(?=\S)
/gm
);
const
indent
=
match
&&
Math
.
min
(...
match
.
map
(
el
=>
el
.
length
));
if
(
indent
)
{
const
regexp
=
new
RegExp
(
`^ {
${
indent
}
}`
,
'gm'
);
return
str
.
replace
(
regexp
,
''
);
}
return
str
;
}
/**
* Remove leading and trailing empty lines from a string, then remove common
* indentation from each line.
*
* This is useful to improve readability for multi-line template literals,
* where you don't want to
* 1) start the first line right after the first backtick.
* 2) indent the string inconsistently with the rest of the code.
*
* This function allows to move from this:
* function foo(arg) {
* if (arg === 3) {
* console.log(`First line
* Second line
* Third line`);
* }
* }
*
* to this:
* function foo(arg) {
* if (arg === 3) {
* console.log(trimAndDedent(`
* First line
* Second line
* Third line
* `));
* }
* }
*/
export
function
trimAndDedent
(
str
:
string
):
string
{
return
dedent
(
trimEmptyLines
(
str
));
}
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