-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ecd152d
commit f2dfb67
Showing
1 changed file
with
34 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
/** | ||
* | ||
* 判断某个文件夹或文件是否在指定的文件夹下 | ||
* | ||
* const p1 = "c:/temp/a/b/c" | ||
* const p2 = "d:/temp/a/b/c" | ||
* const base = "c:/temp" | ||
* | ||
* includePath(p1,base) // true | ||
* includePath(p2,base) // false | ||
* | ||
* | ||
* | ||
*/ | ||
|
||
import path from "path" | ||
|
||
|
||
export function includePath(src: string, basePath: string): boolean { | ||
// 将路径字符串转换为目录对象 | ||
let srcDir = path.normalize(src); | ||
let baseDir = path.normalize(basePath); | ||
return srcDir.startsWith(baseDir) | ||
} | ||
|
||
|
||
|
||
// const p1 = "c:/temp/a/b/c" | ||
// const p2 = "d:/temp/c" | ||
// const p3 = "d:/temp/a/b/c.zip" | ||
// const base = "c:/temp" | ||
// console.log(includePath(p1,base)) // true | ||
// console.log(includePath(p2,base)) // false | ||
// console.log(includePath(p3,base)) // false | ||
/** | ||
* | ||
* 判断某个文件夹或文件是否在指定的文件夹下 | ||
* | ||
* const p1 = "c:/temp/a/b/c" | ||
* const p2 = "d:/temp/a/b/c" | ||
* const base = "c:/temp" | ||
* | ||
* includePath(p1,base) // true | ||
* includePath(p2,base) // false | ||
* | ||
* | ||
* | ||
*/ | ||
|
||
import path from "path" | ||
|
||
|
||
export function includePath(src: string, basePath: string): boolean { | ||
// 将路径字符串转换为目录对象 | ||
let srcDir = path.normalize(src); | ||
let baseDir = path.normalize(basePath); | ||
return srcDir.startsWith(baseDir) | ||
} | ||
|
||
|
||
|
||
// const p1 = "c:/temp/a/b/c" | ||
// const p2 = "d:/temp/c" | ||
// const p3 = "d:/temp/a/b/c.zip" | ||
// const base = "c:/temp" | ||
// console.log(includePath(p1,base)) // true | ||
// console.log(includePath(p2,base)) // false | ||
// console.log(includePath(p3,base)) // false |