Skip to content

Commit

Permalink
io/file-ext? supports now multiple file extensions for checking
Browse files Browse the repository at this point in the history
  • Loading branch information
juerg committed Dec 13, 2023
1 parent 43868ae commit 071bea4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.github.jlangch.venice.SecurityException;
import com.github.jlangch.venice.VncException;
Expand Down Expand Up @@ -93,6 +94,8 @@
import com.github.jlangch.venice.javainterop.IInterceptor;
import com.github.jlangch.venice.javainterop.ILoadPaths;

import net.lingala.zip4j.util.FileUtils;


public class IOFunctions {

Expand Down Expand Up @@ -424,26 +427,40 @@ public VncVal apply(final VncList args) {
VncFunction
.meta()
.arglists(
"(io/file-ext? f ext)")
"(io/file-ext? f ext & exts)")
.doc(
"Returns true if the file f hast the extension ext. " +
"f must be a file or a string (file path).")
.examples(
"(io/file-ext? \"/tmp/test/x.txt\" \"txt\")",
"(io/file-ext? (io/file \"/tmp/test/x.txt\") \".txt\")")
"(io/file-ext? (io/file \"/tmp/test/x.txt\") \".txt\")",
"(io/file-ext? \"/tmp/test/x.docx\" \"doc\" \"docx\")")
.seeAlso("io/file-ext")
.build()
) {
@Override
public VncVal apply(final VncList args) {
ArityExceptions.assertArity(this, args, 2);
ArityExceptions.assertMinArity(this, args, 2);

final File f = convertToFile(
args.first(),
"Function 'io/file-ext?' does not allow %s as f");

final String ext = Coerce.toVncString(args.second()).getValue();
return VncBoolean.of(f.getName().endsWith(ext.startsWith(".") ? ext : "." + ext));
if (args.size() == 2) {
final String ext = Coerce.toVncString(args.second()).getValue();
return VncBoolean.of(f.getName().endsWith(ext.startsWith(".") ? ext : "." + ext));
}
else {
final Set<String> exts = args.slice(1)
.stream()
.map(v -> Coerce.toVncString(args.second()).getValue())
.map(s -> s.startsWith(".") ? s.substring(1) : s)
.collect(Collectors.toSet());

final String fileExt = FileUtils.getFileExtension(f);

return VncBoolean.of(exts.contains(fileExt));
}
}

private static final long serialVersionUID = -1848883965231344442L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,20 @@ public void test_io_file_ext_Q() {

assertTrue((Boolean)venice.eval("(io/file-ext? (io/file \"/tmp/some.png\") \"png\")"));
assertTrue((Boolean)venice.eval("(io/file-ext? (io/file \"/tmp/some.png\") \".png\")"));
}


assertTrue((Boolean)venice.eval("(io/file-ext? \"some.png\" \"png\" \"jpg\" \"gif\")"));
assertTrue((Boolean)venice.eval("(io/file-ext? \"some.png\" \".png\" \".jpg\" \".gif\")"));

assertTrue((Boolean)venice.eval("(io/file-ext? \"/tmp/some.png\" \"png\" \"jpg\" \"gif\")"));
assertTrue((Boolean)venice.eval("(io/file-ext? \"/tmp/some.png\" \".png\" \".jpg\" \".gif\")"));

assertTrue((Boolean)venice.eval("(io/file-ext? (io/file \"some.png\") \"png\" \"jpg\" \"gif\")"));
assertTrue((Boolean)venice.eval("(io/file-ext? (io/file \"some.png\") \".png\" \".jpg\" \".gif\")"));

assertTrue((Boolean)venice.eval("(io/file-ext? (io/file \"/tmp/some.png\") \"png\" \"jpg\" \"gif\")"));
assertTrue((Boolean)venice.eval("(io/file-ext? (io/file \"/tmp/some.png\") \".png\" \".jpg\" \".gif\")"));
}

@Test
public void test_io_file_ext() {
Expand Down

0 comments on commit 071bea4

Please sign in to comment.