Gathering detailed insights and metrics for @qnighy/dedent
Gathering detailed insights and metrics for @qnighy/dedent
npm install @qnighy/dedent
Typescript
Module System
73
Supply Chain
98.9
Quality
75.3
Maintenance
100
Vulnerability
100
License
TypeScript (57.83%)
Rust (38.36%)
JavaScript (3.82%)
Total Downloads
12,781
Last Day
35
Last Week
304
Last Month
792
Last Year
8,747
15 Stars
91 Commits
2 Watching
11 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
0.1.1
Package Id
@qnighy/dedent@0.1.1
Unpacked Size
24.52 kB
Size
5.59 kB
File Count
20
Publised On
27 Feb 2023
Cumulative downloads
Total Downloads
Last day
118.8%
35
Compared to previous day
Last week
54.3%
304
Compared to previous week
Last month
-31.4%
792
Compared to previous month
Last year
116.8%
8,747
Compared to previous year
20
@qnighy/dedent
: JS multi-line literal done right1import { dedent } from "@qnighy/dedent"; 2 3const mdDoc = dedent`\ 4 ## Hello! 5 6 - Plants 7 - Apples 8 - Oranges 9 - Animals 10 - Dogs 11 - Cats 12`;
The dedent
function does only one thing: it removes indentation as it appears in the source code
in a template literal.
Therefore the result is easily predictable. Additionally, you can express any text using the dedent function.
1import { dedent } from "@qnighy/dedent"; 2 3// End-of-line at the end 4const eol = dedent`\ 5 foo 6 bar 7`; 8// No end-of-line at the end 9const noEol = dedent`\ 10 foo 11 bar`; 12 13// With all lines indented 14const withIndent = dedent`\ 15 \ 16 This line is the 1st line. It has 2 spaces of indentation. 17 This line is the 2nd line. It has 2 spaces of indentation. 18`;
Tagged literals are also supported. You can write
1import { dedent } from "@qnighy/dedent"; 2 3const text = dedent(myTag)`\ 4 foo 5 ${bar} 6`;
for
1const text = myTag`\ 2foo 3${bar} 4`;
which is often equivalent to:
1const text = myTag`foo 2${bar} 3`;
You do not need to configure Babel or SWC or whatever to use @qnighy/dedent
. Simply import the function and you are ready to use it.
Though you can use @qnighy/babel-plugin-dedent
or @qnighy/swc-plugin-dedent
if you need optimization.
(NOTE: not released yet)
yarn add -D @qnighy/dedent
# OR:
npm install -D @qnighy/dedent
Indentation is determined based on the longest common indentation among lines. Therefore, you can have some lines indented among other lines.
1const text = dedent`\ 2 non-indented line 3 indented line 4`;
It does not remove indentation of the first line, as it is always preceded by a backtick `
.
Remember that the library removes indentation as it appears in the source code.
1const text = dedent` indented line 2 non-indented line 3`;
That is why we recommend putting a backslash \
immediately after the backtick `
, like this:
1const text = dedent`\ 2 the above backslash (\\) 3 tells JS to join the lines. 4`;
Spaces and tabs are counted equally. The result would be unpredictable should you mix spaces and tabs.
Lines containing only spaces and tabs are considered to have infinity number of indentation.
If they are shorter than the inferred indentation, they become empty. Otherwise, there remain some spaces or tabs.
1const text = dedent`\ 2 1st line 3 4 3rd line. The above line is empty. 5`;
The closing backtick `
is not accounted for in this rule. Therefore, in the example above,
last line is considered to be empty.
This library removes indentation as it appears in the source code. Therefore, escapes and interpolations are both considered to be non-space elements.
1const text = dedent`\ 2 \x20 <- This is a non-space element 3 ${" "} <- This is a non-space element too 4`;
However, indentation within interpolated expressions are excluded from this rule, as we cannot reliably know the indentation there.
1// prettier-ignore 2const text = dedent`\ 3 ${ 4"Indentation is still removed" 5 } 6`;
This is the only exception to the aforementioned rule.
There are three types of newline in JS:
\n
)\u2028
)\u2029
)They all start new lines. Note that CR (\r
) and CRLF (\r\n
) in the source text are automatically converted to LF (\n
) before parsing, thus the library cannot distinguish between LF, CR, and CRLF.
MIT
No vulnerabilities found.
No security vulnerabilities found.