Last active
September 23, 2025 06:15
-
-
Save dsfaccini/2c09a8496267758d6709099c1a4f1869 to your computer and use it in GitHub Desktop.
This is a javascript function that lets you indent your template strings relative to your code. It removes any extra indentation from the final string. This is specially important for LLM prompts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Source: https://github.com/sveltejs/kit/blob/environment-api/packages/kit/src/core/sync/utils.js | |
| /** | |
| * Allows indenting template strings without the extra indentation ending up in the result. | |
| * Still allows indentation of lines relative to one another in the template string. | |
| * @param {TemplateStringsArray} strings | |
| * @param {any[]} values | |
| */ | |
| export function dedent(strings, ...values) { | |
| let dedented = dedent_map.get(strings); | |
| if (!dedented) { | |
| const indentation = /** @type {RegExpExecArray} */ (/\n?([ \t]*)/.exec(strings[0]))[1]; | |
| const pattern = new RegExp(`^${indentation}`, 'gm'); | |
| dedented = { | |
| strings: strings.map((str) => str.replace(pattern, '')), | |
| indents: [] | |
| }; | |
| let current = '\n'; | |
| for (let i = 0; i < values.length; i += 1) { | |
| const string = dedented.strings[i]; | |
| const match = /\n([ \t]*)$/.exec(string); | |
| if (match) current = match[0]; | |
| dedented.indents[i] = current; | |
| } | |
| dedent_map.set(strings, dedented); | |
| } | |
| let str = dedented.strings[0]; | |
| for (let i = 0; i < values.length; i += 1) { | |
| str += String(values[i]).replace(/\n/g, dedented.indents[i]) + dedented.strings[i + 1]; | |
| } | |
| str = str.trim(); | |
| return str; | |
| } | |
| /* example | |
| ```javascript | |
| case sveltekit_server: { | |
| return dedent` | |
| export let read_implementation = null; | |
| export let manifest = null; | |
| export function set_read_implementation(fn) { | |
| read_implementation = fn; | |
| } | |
| export function set_manifest(_) { | |
| manifest = _; | |
| } | |
| `; | |
| } | |
| ``` | |
| will indent to: | |
| ```javascript | |
| export let read_implementation = null; | |
| export let manifest = null; | |
| export function set_read_implementation(fn) { | |
| read_implementation = fn; | |
| } | |
| export function set_manifest(_) { | |
| manifest = _; | |
| } | |
| ``` | |
| this is specially useful for not over-indenting llm prompts and keeps your codes nice and readable | |
| https://dev.to/darkmavis1980/template-literals-why-indentation-matters-especially-for-ai-prompts-3jl4 | |
| without dedent the string would keep the total indentation set by the code, like this: | |
| ```javascript | |
| export let read_implementation = null; | |
| export let manifest = null; | |
| export function set_read_implementation(fn) { | |
| read_implementation = fn; | |
| } | |
| export function set_manifest(_) { | |
| manifest = _; | |
| } | |
| ``` | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment