Coder Social home page Coder Social logo

dedents when it shouldn't about dedent-js HOT 6 OPEN

brandonkal avatar brandonkal commented on May 23, 2024
dedents when it shouldn't

from dedent-js.

Comments (6)

MartinKolarik avatar MartinKolarik commented on May 23, 2024

Looking at the code, I see that lines that have no indentation are ignored. It is not explicitly mentioned in the readme but I guess it makes sense, otherwise dedent would do nothing in those cases and there would be no point in using it.

from dedent-js.

brandonkal avatar brandonkal commented on May 23, 2024

I've created a fix and PR. I'm using this internal to a yaml template tag function. Naturally YAML is very indent focused but the function doesn't know ahead of time if a dedent needs to occur before parsing the string to a JavaScript object.

from dedent-js.

KernelDeimos avatar KernelDeimos commented on May 23, 2024

Looking at the code, I see that lines that have no indentation are ignored. It is not explicitly mentioned in the readme but I guess it makes sense, otherwise dedent would do nothing in those cases and there would be no point in using it.

Should dedent really be useful in this situation? I think consistent behaviour here has more advantages. Here are a few:

  • dedent becomes an idempotent operation if this quirk is removed
  • When dedent is used for the purpose of making code more readable (I would speculate this is the most common use case), indentation is lost when a string happens to start from all the way at the left
  • dedent-js could provide a flag to explicitly remove indentation up to a specified depth, if that's what's desired, covering more use cases overall while making behaviour more consistent

from dedent-js.

KernelDeimos avatar KernelDeimos commented on May 23, 2024

Also Github reports that there are over 18 thousand repositories using this package. It is irresponsible to leave this awful quirk in the code.

from dedent-js.

dvirtz avatar dvirtz commented on May 23, 2024

A possible workaround is to add a space at the begging of each line

dedent(text.replace(/(^[^\n]|\n)/g, '$1 '))

from dedent-js.

KevinGhadyani-minted avatar KevinGhadyani-minted commented on May 23, 2024

I think the issue here is you might be using dedent-js on something where you don't know the final tabbing. Because of that, it makes sense for dedent to do nothing. I'm also for this change.

from dedent-js.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.