Skip to content

Commit

Permalink
Add tests to see all pattern uses are the same
Browse files Browse the repository at this point in the history
  • Loading branch information
omrilotan committed Jan 10, 2024
1 parent 8a40d9f commit cb98232
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 1 addition & 3 deletions scripts/build/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ const pattern = new RegExp(
patterns
.map((pattern) => pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
.join("|"),
)
.toString()
.slice(1, -1);
).source;

const expression = new RegExp(patterns.join("|"), "i").toString();

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ let usedPattern: RegExp;
export function isbot(userAgent?: string | null): boolean {
if (typeof usedPattern === "undefined") {
try {
// Build this RegExp dynamically to avoid syntax errors in older engines.
usedPattern = new RegExp(fullPattern, "i");
} catch (error) {
usedPattern = naivePattern;
Expand Down
7 changes: 6 additions & 1 deletion tests/spec/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
createIsbot,
createIsbotFromList,
} from "../../src";
import { fullPattern, regularExpression } from "../../src/pattern";
import { crawlers, browsers } from "../../fixtures";
let isbotInstance: any;

Expand Down Expand Up @@ -131,7 +132,7 @@ describe("isbot", () => {
afterAll(() => {
jest.restoreAllMocks();
});
test("Fallback regex detects commong crawlers", () => {
test("fallback regex detects commong crawlers", () => {
USER_AGENT_COMMON.forEach((ua) => {
if (!isbotInstance(ua)) {
throw new Error(`Failed to detect ${ua} as bot`);
Expand Down Expand Up @@ -186,5 +187,9 @@ describe("isbot", () => {
);
expect(types).toMatchSnapshot();
});
test("regular expressions exports are as expected", () => {
expect(pattern).toBe(regularExpression);
expect(new RegExp(fullPattern, "i").toString()).toBe(pattern.toString());
});
});
});

0 comments on commit cb98232

Please sign in to comment.