Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dedent function #120

Merged
merged 11 commits into from
Dec 1, 2024
Merged

feat: add dedent function #120

merged 11 commits into from
Dec 1, 2024

Conversation

aleclarson
Copy link
Member

@aleclarson aleclarson commented Jul 18, 2024

Tip

The owner of this PR can publish a preview release by commenting /publish in this PR. Afterwards, anyone can try it out by running pnpm add radashi@pr<PR_NUMBER>.

Summary

Use dedent to remove indentation from every line of a consistently indented string.

// Explicit indentation
_.dedent('\n    Hello\n    World!\n\n', '  ')
// => '  Hello\n  World!\n'

// Detected indentation
_.dedent('\n    Hello\n    World!\n\n')
// => 'Hello\nWorld!\n'

Features

  • Can detect indentation of the first non-empty line
    const string = `
      A
        B
      C
        D
    `
    
    _.dedent(string)
    // => 'A\n  B\nC\n  D'
  • Can be used with a tagged template string
    const story = _.dedent`
      Hello
      World!
    `
    // => 'Hello\nWorld!'
  • Can handle multi-line, embedded strings
    const foo = _.dedent`
      My list:
        - ${['Jello', 'Party Hats', 'Clown'].join('\n- ')}
    `
    // 'My list:\n' +
    // '  - Jello\n' +
    // '  - Party Hats\n' +
    // '  - Clown'

For any code change,

  • Related documentation has been updated, if needed
  • Related tests have been added or updated, if needed
  • Related benchmarks have been added or updated, if needed

Does this PR introduce a breaking change?

No

Bundle impact

Status File Size 1
A src/string/dedent.ts 385

Footnotes

  1. Function size includes the import dependencies of the function.

@aleclarson aleclarson added the new feature This PR adds a new function or extends an existing one label Jul 23, 2024
@aleclarson aleclarson added this to the v12.3.0 milestone Sep 21, 2024
@radashi-bot
Copy link

radashi-bot commented Nov 11, 2024

Benchmark Results

Name Current
dedent: dedent as tagged template literal 255,642.75 ops/sec ±0.32%
dedent: dedent as direct call 700,527.75 ops/sec ±0.23%

Performance regressions of 30% or more should be investigated, unless they were anticipated. Smaller regressions may be due to normal variability, as we don't use dedicated CI infrastructure.

@aleclarson aleclarson added the prerelease Publish to NPM under the "beta" or "next" tag label Nov 11, 2024
radashi-bot pushed a commit that referenced this pull request Nov 11, 2024
@aleclarson aleclarson merged commit 14132e5 into main Dec 1, 2024
9 checks passed
@aleclarson aleclarson deleted the feat/dedent-fn branch December 1, 2024 16:37
@radashi-bot
Copy link

A stable release 12.3.0 has been published to NPM. 🚀

To install:

See the changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature This PR adds a new function or extends an existing one prerelease Publish to NPM under the "beta" or "next" tag
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants