-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- can block only www paths - www blocked by default (for convenience) - adding /* to paths, if there is no / (for convenience) - escaping regex-like characters in paths (. and +)
- Loading branch information
Showing
10 changed files
with
132 additions
and
68 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
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import removeProtocol from "../remove-protocol"; | ||
|
||
describe("removeProtocol()", () => { | ||
it("removes https, http", () => { | ||
expect(removeProtocol("https://example.com/")).toBe("example.com/"); | ||
expect(removeProtocol("http://example.com/")).toBe("example.com/"); | ||
|
||
expect(removeProtocol("https://www.example.com/")).toBe("www.example.com/"); | ||
expect(removeProtocol("http://www.example.com/")).toBe("www.example.com/"); | ||
|
||
expect(removeProtocol("https://dashboard.example.com/")).toBe("dashboard.example.com/"); | ||
expect(removeProtocol("http://dashboard.example.com/")).toBe("dashboard.example.com/"); | ||
|
||
expect(removeProtocol("https://www.dashboard.example.com/")).toBe("www.dashboard.example.com/"); | ||
expect(removeProtocol("http://www.dashboard.example.com/")).toBe("www.dashboard.example.com/"); | ||
}); | ||
|
||
it("keeps path unchanged", () => { | ||
expect(removeProtocol("https://www.example.com/apple/projects/1?tab=analytics#charts")).toBe( | ||
"www.example.com/apple/projects/1?tab=analytics#charts", | ||
); | ||
|
||
expect(removeProtocol("https://example.com/apple/projects/1?tab=analytics#charts")).toBe( | ||
"example.com/apple/projects/1?tab=analytics#charts", | ||
); | ||
|
||
expect(removeProtocol("https://example.com/apple/projects/1")).toBe( | ||
"example.com/apple/projects/1", | ||
); | ||
|
||
expect(removeProtocol("https://example.com/apple/projects/")).toBe( | ||
"example.com/apple/projects/", | ||
); | ||
}); | ||
}); |
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,18 +1,43 @@ | ||
import normalizeUrl from "./normalize-url"; | ||
import removeProtocol from "./remove-protocol"; | ||
import makeRules, { Rule } from "./make-rules"; | ||
|
||
const expandPath = (path: string) => { | ||
const expanded = [path]; | ||
if (!["*.", "www."].find((prefix) => path.startsWith(prefix))) { | ||
expanded.push(`www.${path}`); | ||
} | ||
|
||
[...expanded].forEach((path) => { | ||
if (!path.includes("/")) { | ||
expanded.push(`${path}/*`); | ||
} | ||
}); | ||
|
||
return expanded; | ||
}; | ||
|
||
export default (url: string, blocked: string[]): Rule | undefined => { | ||
const normalizedUrl = normalizeUrl(url); | ||
const normalizedUrl = removeProtocol(url); | ||
const rules = makeRules(blocked); | ||
|
||
const foundRule = rules.find((rule) => | ||
normalizedUrl.match(new RegExp( | ||
"^" | ||
+ rule.path | ||
.replace(/\?/g, ".") // user can type "?" to match any one character | ||
.replace(/\*/g, ".*") // user can type "*" to match any zero or more characters | ||
+ "$", | ||
))); | ||
const foundRule = rules.find(({ path }) => { | ||
const patterns = expandPath(path) | ||
.map((path) => path.replace(/[.+]/g, "\\$&")) // escape regex characters | ||
.map((path) => ( | ||
"^" | ||
+ path | ||
.replace(/\?/g, ".") // user can type "?" to match any one character | ||
.replace(/\*/g, ".*") // user can type "*" to match any zero or more characters | ||
+ "$" | ||
)); | ||
|
||
const found = patterns.some((pattern) => { | ||
const matches = normalizedUrl.match(new RegExp(pattern)); | ||
return matches; | ||
}); | ||
|
||
return found; | ||
}); | ||
|
||
return foundRule; | ||
}; |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export default (url: string): string => url.replace(/^http(s?):\/\//, ""); |