Gathering detailed insights and metrics for outdent
Gathering detailed insights and metrics for outdent
Gathering detailed insights and metrics for outdent
Gathering detailed insights and metrics for outdent
npm install outdent
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
165 Stars
134 Commits
5 Forks
2 Watching
5 Branches
4 Contributors
Updated on 25 Oct 2024
TypeScript (99.61%)
JavaScript (0.39%)
Cumulative downloads
Total Downloads
Last day
-18.7%
335,493
Compared to previous day
Last week
3.8%
2,182,560
Compared to previous week
Last month
11.1%
9,140,196
Compared to previous month
Last year
61.9%
75,570,652
Compared to previous year
ES6 template strings are great, but they preserve everything between the backticks, including leading spaces. Sometimes I want to indent my template literals to make my code more readable without including all those spaces in the string.
Outdent will remove those leading spaces, as well as the leading and trailing newlines.
Import outdent using your module system of choice.
1// Deno w/JSR 2import outdent from 'jsr:@cspotcode/outdent'; 3// Deno w/DenoLand 4import outdent from 'https://deno.land/x/outdent/mod.ts'; 5// ECMAScript modules 6import outdent from 'outdent'; 7// CommonJS 8const outdent = require('outdent');
1import outdent from 'outdent'; 2 3const markdown = outdent` 4 # My Markdown File 5 6 Here is some indented code: 7 8 console.log("hello world!"); 9`; 10 11console.log(markdown); 12 13fs.writeFileSync('output.md', markdown);
The contents of output.md
do not have the leading indentation:
1# My Markdown File
2
3Here is some indented code:
4
5 console.log("hello world!");
As a JavaScript string:
1var markdown = '# My Markdown File\n' + 2 '\n' + 3 'Here is some indented code:\n' + 4 '\n' + 5 ' console.log("hello world!");';
You can pass options to outdent to control its behavior. They are explained in Options.
1const output = outdent({trimLeadingNewline: false, trimTrailingNewline: false})` 2 Hello world! 3`; 4 5assert(output === '\nHello world!\n');
You can explicitly specify the indentation level by passing outdent
as the first interpolated value. Its position sets the indentation level and it is removed from the output:
1const output = outdent` 2 ${outdent} 3 Yo 4 12345 5 Hello world 6`; 7 8assert(output === ' Yo\n345\n Hello world');
Note: ${outdent}
must be alone on its own line without anything before or after it. It cannot be preceded by any non-whitespace characters.
If these conditions are not met, outdent will follow normal indentation-detection behavior.
Outdent can also remove indentation from plain strings via the string
method.
1const output = outdent.string('\n Hello world!\n'); 2 3assert(output === 'Hello world!');
trimLeadingNewline
Default: true
trimTrailingNewline
Default: true
Whether or not outdent should remove the leading and/or trailing newline from your template string. For example:
1var s = outdent({trimLeadingNewline: false})` 2 Hello 3`; 4 5assert(s === '\nHello'); 6 7s = outdent({trimTrailingNewline: false})` 8 Hello 9` 10 11assert(s === 'Hello\n'); 12 13s = outdent({trimLeadingNewline: false, trimTrailingNewline: false})` 14 15`; 16 17assert(s === '\n\n');
newline
Default: null
If set to a string, normalize all newlines in the template literal to this value.
If null
, newlines are left untouched.
s = outdent({newline: '\r\n'}) `
first
second
`;
assert(s === 'first\r\nsecond');
Newlines that get normalized are '\r\n', '\r', and '\n'.
Newlines within interpolated values are never normalized.
Although intended for normalizing to '\r\n', you can use any string, for example a space.
1const s = outdent({newline: ' '}) ` 2 Hello 3 world! 4`; 5 6assert(s === 'Hello world!');
Start the contents of your template string on a new line after the opening backtick. Otherwise, outdent has no choice but to detect indentation from the second line, which does not work in all situations.
1// Bad 2const output = outdent `* item 1 3 * sub-item 4`; 5// output === '* item 1\n* sub-item'; Indentation of sub-item is lost 6 7// Good 8const output = outdent ` 9 * item 1 10 * sub-item 11`;
Spaces and tabs are treated identically. outdent does not verify that you are using spaces or tabs consistently; they are all treated as a single character for the purpose of removing indentation. Spaces, tabs, and smart tabs should all work correctly provided you use them consistently.
This module includes TypeScript type declarations so you will get code completion and error-checking without installing anything else.
File an issue on Github: https://github.com/cspotcode/outdent/issues
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
11 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 10
Reason
license file detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
16 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More