mxcl / Path.swift

Delightful, robust, cross-platform and chainable file-pathing functions.
The Unlicense
946 stars 36 forks source link

suggestion: Entry.files(withExtension) could make the extension optional #21

Closed ConfusedVorlon closed 5 years ago

ConfusedVorlon commented 5 years ago

I'm making a list of files in a directory. I want files that are not directories.

I see that there is a convenience filter

path.ls().directories()

but not

path.ls().files()

my suggestion would be to make the withExtension param optional in the existing files convenience function

func files(withExtension ext: String? = nil)

so that you can use

path.ls().files() -> Returns all non-directories

or

path.ls().files(withExtension:"png") -> Returns all non-directories with file extension .png

while you're in there, it would also be handy to have

func files(withExtensions ext: [String])

e.g. for when you're looking for image files and have a list of supported extensions

thanks for the library. I'm just starting to play with it, but am impressed so far :)

mxcl commented 5 years ago

Well, now we have files() and directories, so I guess I should make directories a function too.

ConfusedVorlon commented 5 years ago

I guess that's the most minimal consistent implementation.

personally, I have no issue with var directories as a duplicate for func directories() and var files as an duplicate for func files()

mxcl commented 5 years ago

I have no issue with var directories as a duplicate for func directories()

Swift doesn’t allow this, though I think it would if directories() had a parameter, so perhaps I can do it for files.

mxcl commented 5 years ago

Anyway, almost done. Coming up.

ConfusedVorlon commented 5 years ago

thank you :)

ConfusedVorlon commented 5 years ago

as you say, swift does allow duplication if there is a parameter - even if that param has a default value so, I just put this in a playground

class Foo {
    var bar:[String] {
        return ["this"]
    }

    func bar(test:Bool = true) -> [String] {
        return ["that"]
    }
}

let test = Foo()
test.bar
test.bar()

if you want duplicates on directory(), then you'd just need to think of an obscure filter method param and provide a nil default

func directories(startingWith:String? = nil)

mxcl commented 5 years ago

Refs #22

mxcl commented 5 years ago

K released: https://github.com/mxcl/Path.swift/releases/tag/0.10.0

CocoaPods will deploy when CI for the tag succeeds.

mxcl commented 5 years ago

If you found my help useful, please consider reimbursing my time by contributing to my Patreon. Thank you so much!

ConfusedVorlon commented 5 years ago

that's great - thank you.

ConfusedVorlon commented 5 years ago

thinking about this a bit more as I work through my own app; It would actually make complete sense for the directories and files APIs to match up exactly, including filtering for extensions.

Extensions on directories seems odd until you think that file bundles are actually directories with extensions.

So Path("~/Applications").directories(withExtension:"app")

is actually useful

on a related note, filtering should probably handle case sensitivity to be properly useful. It's pretty common for images to turn up as .JPG as well as .jpg

func files(withExtension ext: String? = nil, caseSensitive:Bool = false)

I'm happy to send these in as pull requests. Let me know if that would help.

mxcl commented 5 years ago

I'm actually considering dropping the extensions-specialization and having the user do the filtering themselves.

ls().files().filter{ ["jpg", "jpeg"] =~ $0.extension.lowercased() }
ConfusedVorlon commented 5 years ago

yup - seems reasonable. I didn't know about =~ on an array

alternatively, how about a chaining approach with a convenience filter (this has to be a super common use case)

public extension Array where Element == Entry {
 func filter(withExtensions:[String],caseSensitive:Bool = false) -> [Entry]
}

that way files and directories can both be var, and you can chain if you want a filter

ls().files.filter(withExtensions:["jpg","png"])