Giter Site home page Giter Site logo

Comments (14)

borkdude avatar borkdude commented on July 17, 2024

Alternatives when we go for 4: (first (split-ext f)), (str/replace (str f) #"\.md$" ""), etc.
split-ext already preserves the entire path so you can call file-name on that afterwards: (file-name (first (split-ext f))).

Comment in Slack:

@lukaszkorecki I almost opened a PR for this (it was adding an option to stripe a suffix) but realized that fs/file-name and fs/extension basically give you all the building blocks. After reviewing the code and how we use babashka.fs in majority of the cases we wanted to full name (as in: with extension), so I dropped it.

from fs.

corasaurus-hex avatar corasaurus-hex commented on July 17, 2024

split-ext doesn't preserve the path, though? https://github.com/babashka/fs/blob/master/test/babashka/fs_test.clj#L306-L307

from fs.

lukaszkorecki avatar lukaszkorecki commented on July 17, 2024

To add to that: I think it would be reasonable addition if babashka/fs wants to be the equivalent of something like Ruby's FileUtils: https://ruby-doc.org/stdlib-2.5.5/libdoc/fileutils/rdoc/FileUtils.html and File class (https://ruby-doc.org/core-2.5.5/File.html) which are modeled after common shell operations and/or sys calls.

My initial assumption was that's how things will be named in bb/fs - maybe worth considering some sort of additional namespace/lib (babashka.fs.unix ????) - especially in babashka context, porting shell code which does a lot of path operations (and relies on things like dirname, basename, globbing etc) could be helpful.

But as I mentioned in my original comment in Slack: all the building blocks are present, and starting with a help namespace in your own code might be the way to go (that's how my team approaches things: we have a whole layer of code on top of cognitect/aws-api or carmine, but it wouldn't make sense to merge that in into base libraries.

Just some food for thought of course!

from fs.

corasaurus-hex avatar corasaurus-hex commented on July 17, 2024

It might be worth talking about why a thing gets included in babashka/fs, too. I tried to think through the categories of needs a library like this can fill and it seems like there are at least three (let me know if you can think of more!):

  1. Shim over Java filesystem APIs.
  2. Handle common shell scripting needs for filesystem handling.
  3. Handle complex shell scripting needs for filesystem handling.

I think babashka/fs definitely does 1 and 3. It seems like the trouble here is that this code sort of falls into category 2.

Can we make a good argument for 2? I think so. If people commonly need to do it exactly what a function does, and it's filesystem handling, then it makes sense to have it maintained in one place. It saves quite a bit of labor over time and allows babashka scripts to be shorter and not duplicate behavior from script to script. On top of that, it reveals the intention of the code more to see (file-name-without-extension path) vs (first (split-ext path)) which is always a nice thing to have. These are substantial benefits to babashka and babashka scripts.

However, this argument rests on whether this is something people commonly need to do, which I don't know how to show either way. It's common enough that it's included in POSIX shells but that also might be because removing the suffix from a file is so much more difficult in POSIX shells (which would be 3, not 2). Is this a common need? I certainly have used it to remove suffixes in the past in Ruby, but ever using isn't the same as commonly using.

from fs.

corasaurus-hex avatar corasaurus-hex commented on July 17, 2024

I'm not sure how relevant it is but for a survey of scripting language stdlibs and if they include suffix removal:

  • python - ❌
  • ruby - ✅
  • node - ✅
  • perl - ✅
  • php - ✅
  • joker - ❌
  • lua - ❌ (needs third party library for path handling)
  • elisp - ✅
  • janet - ❌ (needs third party library for path handling)

from fs.

borkdude avatar borkdude commented on July 17, 2024

split-ext doesn't preserve the path, though?

Oops, I think it maybe should. Perhaps it won't be really breaking if we fixed that.

Suffix removal.
Proposal: strip-ext: without an option it removes the extension and returns the full path... and it can take an option {:ext "md"} so it will only remove the ".md" extension and leaves all other extensions.
You can then call file-name on the result if you just wanted the file-name.

Up for discussion.

from fs.

corasaurus-hex avatar corasaurus-hex commented on July 17, 2024

Would that work with multiple extensions? So like (strip-ext path {:ext "html.template"}) would remove .html.template? Or would it be more like (strip-ext path {:ext ["html" "template"]})? or would you call it multiple times like (-> path (strip-ext {:ext "template"}) (strip-ext {:ext "html"})?

from fs.

borkdude avatar borkdude commented on July 17, 2024

We can make it work with (strip-ext path {:ext "html.template"}). By default it would only remove the .template part I think?

from fs.

corasaurus-hex avatar corasaurus-hex commented on July 17, 2024

awesome! I was just typing up a comment on why that would work better 😄

from fs.

corasaurus-hex avatar corasaurus-hex commented on July 17, 2024

I'd guess that basename removes suffix instead of extension in order to handle things like if you had widgets_view.html.template and wanted to remove _view.html.template. I can only remember needing to do that once or twice ever, but I thought I'd mention it

from fs.

corasaurus-hex avatar corasaurus-hex commented on July 17, 2024

And at that point it's just string handling and not really extension removal, so not really fitting for this library.

from fs.

corasaurus-hex avatar corasaurus-hex commented on July 17, 2024

I updated the PR to do strip-ext. I went with two positional arguments instead of making the second one an options map. Let me know if you feel strongly about that one, it just felt weird while I was implementing.

from fs.

corasaurus-hex avatar corasaurus-hex commented on July 17, 2024

I fixed the PR to with your comments. Let me know what you think.

from fs.

corasaurus-hex avatar corasaurus-hex commented on July 17, 2024

Looks like it was merged!

from fs.

Related Issues (20)

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.