I study patterns used in an open source project found on Github Trending. For this week, I reviewed some parts of Storybook codebase and wrote this article.
import { dedent } from 'ts-dedent';console.log(dedent`A string that gets so long you need to break it over multiple lines. Luckily dedent is here to keep it readable without lots of spaces ending up in the string itself.`);// orconsole.log(dedent` A string that gets so long you need to break it over multiple lines. Luckily dedent is here to keep it readable without lots of spaces ending up in the string itself.`);
results in
A string that gets so long you need to break it overmultiple lines. Luckily dedent is here to keep itreadable without lots of spaces ending up in the stringitself.
What would be result if you just used back ticks — ``? well, this dedent is not about line breaks but rather about indentation because of spaces. Below is an example that makes it easy to understand:
function example() { const message = ` Hello, This is a message with multiple lines `; console.log(message);}
This outputs with all the leading spaces preserved:
export function resolveWithExtension(importPath: string, currentFilePath: string): string { // If the import already has an extension, return it as-is if (path.extname(importPath)) { return importPath; } deprecate(dedent`One or more extensionless imports detected: "${importPath}" in file "${currentFilePath}". For maximum compatibility, you should add an explicit file extension to this import. Storybook will attempt to resolve it automatically, but this may change in the future. If adding the extension results in an error from TypeScript, we recommend setting moduleResolution to "bundler" in tsconfig.json or alternatively look into the allowImportingTsExtensions option.`);