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
1c638bd1
Commit
1c638bd1
authored
Dec 11, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
Dec 11, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert trimAndDedent in a tag function
parent
7db92f0c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
95 additions
and
26 deletions
+95
-26
trim-and-dedent-test.js
src/shared/test/trim-and-dedent-test.js
+35
-7
trim-and-dedent.ts
src/shared/trim-and-dedent.ts
+60
-19
No files found.
src/shared/test/trim-and-dedent-test.js
View file @
1c638bd1
...
...
@@ -2,10 +2,10 @@ import { trimAndDedent } from '../trim-and-dedent';
describe
(
'trimAndDedent'
,
()
=>
{
[
[
'Foo'
,
'Foo'
],
[
' Foo'
,
'Foo'
],
[
()
=>
trimAndDedent
`Foo`
,
'Foo'
],
[
()
=>
trimAndDedent
` Foo`
,
'Foo'
],
[
`First line
()
=>
trimAndDedent
`First line
Second line
Third line`
,
`First line
...
...
@@ -13,7 +13,7 @@ describe('trimAndDedent', () => {
Third line`
,
],
[
`
()
=>
trimAndDedent
`
Hello, Jane!
Indented line
...
...
@@ -24,10 +24,38 @@ describe('trimAndDedent', () => {
Indented line
Goodbye, John!`
,
],
].
forEach
(([
str
,
expectedResult
])
=>
{
[
()
=>
{
const
firstVar
=
` very indented`
;
const
secondVar
=
`
multiple
lines
with no indentation
`
;
return
trimAndDedent
`
Hello, Jane!
${
firstVar
}
Indented line
Goodbye, John!
${
secondVar
}
`
;
},
`Hello, Jane!
very indented
Indented line
Goodbye, John!
multiple
lines
with no indentation
`
,
],
].
forEach
(([
getResult
,
expectedResult
])
=>
{
it
(
'normalizes strings with multiple lines'
,
()
=>
{
const
result
=
trimAndDedent
(
str
);
assert
.
equal
(
result
,
expectedResult
);
assert
.
equal
(
getResult
(),
expectedResult
);
});
});
});
src/shared/trim-and-dedent.ts
View file @
1c638bd1
function
trimLeadingEmptyLines
(
str
:
string
):
string
{
return
str
.
replace
(
/^
\s
*
\n
/g
,
''
);
}
function
trimTrailingEmptyLines
(
str
:
string
):
string
{
return
str
.
replace
(
/
\n\s
*$/g
,
''
);
}
/**
* Remove
leading and trailing empty lines from
a string.
* Remove
specified indentation from each line of
a string.
*/
function
trimEmptyLines
(
str
:
string
):
string
{
return
str
.
replace
(
/^
\s
*
\n
|
\n\s
*$/g
,
''
);
function
dedentStr
(
str
:
string
,
indent
:
number
)
{
const
indentRegexp
=
new
RegExp
(
`^ {
${
indent
}
}`
,
'gm'
);
return
str
.
replace
(
indentRegexp
,
''
);
}
/**
* Remove common indentation from each line of a string.
* Remove common indentation from each line of every string, then interpolate
* params verbatim.
*/
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
,
''
);
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
;
let
result
=
''
;
for
(
const
[
i
,
param
]
of
params
.
entries
())
{
result
+=
dedentStr
(
strings
[
i
],
smallestIndent
);
result
+=
param
;
}
return
str
;
return
result
+
dedentStr
(
strings
[
strings
.
length
-
1
],
smallestIndent
)
;
}
/**
...
...
@@ -35,21 +52,45 @@ function dedent(str: string) {
* if (arg === 3) {
* console.log(`First line
* Second line
* Third line`);
* ${var}
* Fourth line`);
* }
* }
*
* to this:
* function foo(arg) {
* if (arg === 3) {
* console.log(trimAndDedent
(
`
* console.log(trimAndDedent`
* First line
* Second line
* Third line
* `));
* ${var}
* Fourth line
* `);
* }
* }
*/
export
function
trimAndDedent
(
str
:
string
):
string
{
return
dedent
(
trimEmptyLines
(
str
));
export
function
trimAndDedent
(
strings
:
TemplateStringsArray
,
...
params
:
any
[]
):
string
{
if
(
strings
.
length
<
2
)
{
// Trim leading and trailing empty lines from first (and only) string
const
trimmedLines
=
[
trimLeadingEmptyLines
(
trimTrailingEmptyLines
(
strings
[
0
])),
];
return
dedent
(
trimmedLines
,
...
params
);
}
const
firstString
=
strings
[
0
];
const
lastString
=
strings
[
strings
.
length
-
1
];
const
middle
=
strings
.
slice
(
1
,
strings
.
length
-
1
);
// Trim empty leading lines from first string, and empty trailing lines from last one
const
trimmedLines
=
[
trimLeadingEmptyLines
(
firstString
),
...
middle
,
trimTrailingEmptyLines
(
lastString
),
];
return
dedent
(
trimmedLines
,
...
params
);
}
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