incremental-writing icon indicating copy to clipboard operation
incremental-writing copied to clipboard

BUG: withExtension is very broken, mangling file some names

Open 229c9cf0 opened this issue 2 years ago • 1 comments

I was wondering why my iwq_extended queue would always be missed by adds, with them instead ending up in iwq_extende instead. Yet targeting iwq_extended_ would put them there... Turns out rtrim is either completely broken or working exactly as intended but a very wrong tool for the job:

"extended.md".rtrim(".md") // --> "extende"
"extendeddddddd.md".rtrim(".md") // --> "extende"
"extendedmmd.d.d.....mmmmdddmmmm......".rtrim(".md") // --> "extende"

Given that "add to queue" already adds the extension, I have no clue what the .rtrim(".md") + ".md" was supposed to achieve. (rtrim is also used at least to generate status bar text, so that's not the only problem.)

229c9cf0 avatar Feb 23 '22 09:02 229c9cf0

I don't really know JS, but with some experimentation it seems that something like

String.prototype.rcut = function( ext ) {
  return this.substr( 0, this.lastIndexOf( ext ) ) || this;
}

would do a much better job for the intended purpose here. (No clue if that already has a common name, this one's made up.) It's still not fully correct, since .rcut(".") would remove anything after a dot (instead maybe checking the length of the extension is its offset from the end or something like that would be more precise), but way better than rtrim, which removes all characters repeatedly. (Which seems to be a PHP-ism and working as intended, so the name rtrim SHOULD NOT be used for a different purpose, especially since you're adding it as a public function on String.)

229c9cf0 avatar Feb 23 '22 09:02 229c9cf0