-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
30 changed files
with
405 additions
and
253 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,14 +1,14 @@ | ||
import { relativePath, slash } from "@fourze/core"; | ||
import { normalizeURL, withoutBase } from "@fourze/core"; | ||
import { suite } from "../lib/suite"; | ||
|
||
// const source = new Array(100).fill(0).map((_, i) => i); | ||
suite("relativePath", 100000, () => { | ||
relativePath("/api/abc", "/api"); | ||
withoutBase("/api/abc", "/api"); | ||
}); | ||
|
||
suite("replace", 100000, () => { | ||
const a = "/api/abc"; | ||
const b = "/api"; | ||
slash((a.replace(b, ""))); | ||
normalizeURL((a.replace(b, ""))); | ||
}); | ||
|
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
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
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
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
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
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,75 +1,28 @@ | ||
import minimatch from "minimatch"; | ||
import type { MaybeRegex } from "maybe-types"; | ||
import { resolveURL } from "ufo"; | ||
import { hasProtocol, isEmptyURL, isRelative, joinURL, normalizeURL, withLeadingSlash, withTrailingSlash, withoutBase, withoutLeadingSlash, withoutTrailingSlash } from "ufo"; | ||
import minimatch from "minimatch"; | ||
import { isRegExp } from "./is"; | ||
|
||
export function slash(...paths: string[]): string { | ||
let path = paths | ||
.map((p) => p.replace(/\\/g, "/")) | ||
.join("/") | ||
.replace(/\/+/g, "/"); | ||
|
||
if (path.length > 1 && path.endsWith("/")) { | ||
path = path.slice(0, -1); | ||
} | ||
|
||
if (!path.startsWith("/")) { | ||
path = "/".concat(path); | ||
} | ||
|
||
return path; | ||
} | ||
|
||
export function relativePath(path: string, base?: string): string | null { | ||
if (!hasProtocol(path)) { | ||
if (base) { | ||
if (base.endsWith("/")) { | ||
base = base.slice(0, -1); | ||
} | ||
if (path.startsWith(base)) { | ||
path = path.slice(base.length); | ||
} else { | ||
return null; | ||
} | ||
} | ||
return path; | ||
} | ||
return null; | ||
} | ||
|
||
const protocolReg = /^(\w+):\/\//i; | ||
|
||
export function hasProtocol(path: string) { | ||
return protocolReg.test(path); | ||
} | ||
|
||
export function isMatch(path: string, ...pattern: MaybeRegex[]) { | ||
return pattern.some((r) => { | ||
if (isRegExp(r)) { | ||
return r.test(path); | ||
} | ||
return path.startsWith(r) || minimatch(path, r, { matchBase: true, partial: true }); | ||
|
||
return path.startsWith(r) || minimatch(path, r, { matchBase: true }); | ||
}); | ||
} | ||
|
||
/** | ||
* 格式化路径 | ||
* @param path | ||
*/ | ||
export function normalize(path: string) { | ||
if (!path.startsWith("/")) { | ||
path = "/".concat(path); | ||
} | ||
path = path.replace(/\\/g, "/").replace(/\/+/g, "/"); | ||
if (path.length > 1 && path.endsWith("/")) { | ||
path = path.slice(0, -1); | ||
export function withBase(input: string, base: string, trailingSlash = true) { | ||
if (isEmptyURL(base) || hasProtocol(input)) { | ||
return input; | ||
} | ||
return path; | ||
} | ||
const _base = trailingSlash ? withTrailingSlash(base) : base; | ||
|
||
export function resolves(...paths: string[]) { | ||
if (paths.length === 0) { | ||
return "/"; | ||
if (input.startsWith(_base)) { | ||
return input; | ||
} | ||
return resolveURL(paths[0], ...paths.slice(1)); | ||
return joinURL(_base, input); | ||
} | ||
|
||
export { withoutBase, normalizeURL, isEmptyURL, isRelative, joinURL, hasProtocol, withTrailingSlash, withoutTrailingSlash, withLeadingSlash, withoutLeadingSlash }; |
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
Oops, something went wrong.