Comments (14)
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.
split-ext
doesn't preserve the path, though? https://github.com/babashka/fs/blob/master/test/babashka/fs_test.clj#L306-L307
from fs.
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.
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!):
- Shim over Java filesystem APIs.
- Handle common shell scripting needs for filesystem handling.
- 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.
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.
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.
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.
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.
awesome! I was just typing up a comment on why that would work better 😄
from fs.
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.
And at that point it's just string handling and not really extension removal, so not really fitting for this library.
from fs.
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.
I fixed the PR to with your comments. Let me know what you think.
from fs.
Looks like it was merged!
from fs.
Related Issues (20)
- add gzip and gunzip functions
- `babashka.fs/glob` does not find directories with `.` in the name HOT 3
- Efficiently read part of a file by seek offsets HOT 3
- UnsupportedOperationException when using `copy-tree` with zip file path as source HOT 5
- Links in API.md don't work HOT 6
- comment inconsistent: fs/parent returns file no matter if it exist or not
- Paths can have FileSystem that have different separators than the OS-specific one HOT 7
- suggestion: fs/find-up HOT 5
- glob: hidden-option based on pattern HOT 2
- fs/match won't find things in root-folder HOT 1
- `with-temp-dir` can raise a `AccessDeniedException` exception HOT 3
- weird behaviour fs/match, fs/glob HOT 8
- copy-tree fail when some non-leaf directories in the tree are not writable HOT 3
- unzip doesn't work if source is a resource from a jar HOT 1
- Potential regression HOT 4
- Inconsistent documentation for the `:posix-file-permissions` options HOT 1
- Unclear from `fs/glob`docs that it supports a `:max-depth` option HOT 1
- Allow for more declarative use of `xdg-*-home` HOT 1
- Consider allowing non-relative paths for fs/which HOT 4
- Add function to see file-owner
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fs.