undent

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2025 License: MIT Imports: 1 Imported by: 4

README

Undent

Undent lets you inline multi-line strings in Go code without losing in readability. You no longer have to write:

func main() {
    const v = `this is
a multi-line                       <--- not aligned with the func's indentation
string`
    fmt.Println("world!")
}

Instead, you can write:

func main() {
    const v = undent.Undent(`
        this is
        a multi-line
        string
    `)
    fmt.Println("world!")
}

This is much cleaner and easier to read.

Features

Like in Jim Myhrberg's Undent, it is possible to start the literal string on the next line. For example, in the following example, name and labels have the same indentation level but aren't aligned due to the leading "`":

v := `name: foo          <--- not aligned with the rest of string
labels:
foo: bar`

Instead, you can write a well-aligned text like this:

v := undent.Undent(`
    name: foo
    labels:
       foo: bar
    `)

An improvement I made is to make it possible to not have the correct number of indentations in the last line.

Before:

v := undent.Undent(`
    foo
    bar
    `)              // <--- last line had to match the indentation of the rest

After:

v := undent.Undent(`
    foo
    bar
`)                  // <--- last line can have any indentation

Note that the last line's indentation must not be greater than the rest; otherwise, it will be considered as a line instead of being skipped.

Another improvement is the indentation of empty lines. One problem I had is that my editor is configured to remove any trailing white-space, so I couldn't have empty lines with spaces.

Example:

v := undent.Undent(`
    foo               <---- 4 spaces
                      <---- no indentation needed here
    bar               <---- 4 spaces
`)

Context: After using Jim Myhrberg's Undent for a while, I needed to add some features and fix some bugs. However, the original code was unscrutable to me, so I decided to re-write it from scratch. I haven't written as many tests as the original, though.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Undent

func Undent(s string) string

Undent removes leading indentation/white-space from given string and returns it as a string. Useful for inlining YAML manifests in Go code. Inline YAML manifests in the Go test files makes it easier to read the test case as opposed to reading verbose-y Go structs.

This Undent implementation is a re-write of Jim Myhrberg's https://github.com/jimeh/Undent. I decided to re-write it because I was unable to understand the original implementation, and thus could not fix it.

For code readability purposes, it is possible to start the literal string with "\n", in which case, the first line is ignored. For example, in the following example, name and labels have the same indentation level but aren't aligned due to the leading '`':

Undent(
`    name: foo
      labels:
        foo: bar`)

Instead, you can write a well-aligned text like this:

Undent(`
    name: foo
    labels:
       foo: bar`)

For code readability purposes, it is also possible to not have the correct number of indentations in the last line. For example:

Undent(`
    foo
    bar
`)

For code readability purposes, you can also omit the indentations for empty lines. For example:

Undent(`
    foo     <---- 4 spaces
            <---- no indentation here
    bar     <---- 4 spaces
`)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL