dmnd / dedent

⬅️ ES6 string tag that strips indentation from multi-line strings.
MIT License
931 stars 36 forks source link

🐛 Bug: Dedent not working with carriage returns \r #94

Open matthewt-assurity opened 3 months ago

matthewt-assurity commented 3 months ago

Bug Report Checklist

Expected

I've got code that splits a string by newlines and carriage returns into lines. I've got a test that checks that newlines and carriage returns have been removed.


const myString = dedent`
            line one\r
            line two
            line three`

const lines = myString.split(/\r\n|\r|\n/)

When console.logging lines, I would expect to see the below:

[
  'line one',
  'line two',
  'line three'
]

Actual

What I actually see when I console.log lines is this. It appears the \r is not removed, and \\r is added in.

[
  'line one\\r',
  'line two',
  'line three'
]

Additional Info

I can get my use-case working by not using dedent, so I'm not blocked, but I sure would like to use dedent to make my strings look nicer!

const myString = `line one\r
line two
line three`
JoshuaKGoldberg commented 3 months ago

Ha, interesting issue - thanks for filing! I always forget \r sometimes appears on its own...

But: I'm not sure it's clear that a \r on its own should be trimmed? Reading https://stackoverflow.com/questions/1761051/difference-between-n-and-r:

in Unix and all Unix-like systems, \n is the code for end-of-line, \r means nothing special

Is there a reason strings would have lone \r without \n?

matthewt-assurity commented 3 months ago

I've seen the issue when I try to split the result from a FileReader.

Given I have a file (e.g from an HTML input element) I can run the below to log the split lines of the file to the console:

const reader = new FileReader();

reader.onload = () => {
    const lines = reader.result.split('\n');
    console.log(lines);
};

reader.readAsText(file);

This results in the output containing \r at the end of each line.

However, based on what you've said I might be handling this incorrectly... should I always be trying to split with the full /\r\n|\r|\n/?

Bringing this back to dedent, I am using dedent to write some test data strings for my test functions to use (so I don't have to parse a file for testing purposes, mainly because it seems the JS FileReader API can't be used in NodeJS :/ ). I wanted to test for cases where strings end with \r (re: my original post), but is it possible I just have the wrong understanding and this is a non-issue? Am I handling things incorrectly by splitting only by \n?