File tree Expand file tree Collapse file tree 3 files changed +32
-0
lines changed
Expand file tree Collapse file tree 3 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments