Skip to content

Commit 2eb235a

Browse files
committed
feat: add support for glob-like pattern matching
1 parent e646d16 commit 2eb235a

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# PathKit Changelog
22

3+
## TBD
4+
5+
### Enhancements
6+
7+
- Path's can now be tested against a glob-like pattern using
8+
`Path(...).match(pattern)`.
9+
310
## 1.0.0 (2019-03-27)
411

512
### Enhancements

Sources/PathKit.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,16 @@ extension Path {
617617
public func glob(_ pattern: String) -> [Path] {
618618
return Path.glob((self + pattern).description)
619619
}
620+
621+
public func match(_ pattern: String) -> Bool {
622+
let cPattern = strdup(pattern)
623+
let cPath = strdup(path)
624+
defer {
625+
free(cPattern)
626+
free(cPath)
627+
}
628+
return fnmatch(cPattern, cPath, 0) == 0
629+
}
620630
}
621631

622632

Tests/PathKitTests/PathKitSpec.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,5 +508,20 @@ describe("PathKit") {
508508
try expect(paths) == results.sorted(by: <)
509509
}
510510
}
511+
512+
$0.describe("#match") {
513+
$0.it("can match pattern against relative path") {
514+
try expect(Path("test.txt").match("test.txt")).to.beTrue()
515+
try expect(Path("test.txt").match("*.txt")).to.beTrue()
516+
try expect(Path("test.txt").match("*")).to.beTrue()
517+
try expect(Path("test.txt").match("test.md")).to.beFalse()
518+
}
519+
520+
$0.it("can match pattern against absolute path") {
521+
try expect(Path("/home/kyle/test.txt").match("*.txt")).to.beTrue()
522+
try expect(Path("/home/kyle/test.txt").match("/home/*.txt")).to.beTrue()
523+
try expect(Path("/home/kyle/test.txt").match("*.md")).to.beFalse()
524+
}
525+
}
511526
}
512527
}

0 commit comments

Comments
 (0)