Skip to content

Commit

Permalink
Accept null in place of userAgent string (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
omrilotan authored Jan 3, 2024
1 parent f4f476a commit d3e37df
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [4.1.2](https://github.com/omrilotan/isbot/compare/v4.1.1...v4.1.2)

- Accept `null` in place of user agent string to allow header value to be used "as is" (`request.headers.get("user-agent")`)

## [4.1.1](https://github.com/omrilotan/isbot/compare/v4.1.0...v4.1.1)

- Recognise browsers with GMS Core ([Google's Play Services](https://github.com/microg/GmsCore/wiki)) as natural non-bot browsers
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "isbot",
"version": "4.1.1",
"version": "4.1.2",
"description": "🤖 Recognise bots/crawlers/spiders using the user agent string.",
"keywords": [
"bot",
Expand Down
24 changes: 14 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ export const list: string[] = patternsList;
/**
* Check if the given user agent includes a bot pattern.
*/
export const isbot = (userAgent: string): boolean =>
export const isbot = (userAgent: string | null): boolean =>
Boolean(userAgent) && pattern.test(userAgent);

/**
* Create a custom isbot function with a custom pattern.
*/
export const createIsbot =
(customPattern: RegExp): ((userAgent: string) => boolean) =>
(customPattern: RegExp): ((userAgent: string | null) => boolean) =>
(userAgent: string): boolean =>
Boolean(userAgent) && customPattern.test(userAgent);

Expand All @@ -41,25 +41,29 @@ export const createIsbotFromList = (
/**
* Find the first part of the user agent that matches a bot pattern.
*/
export const isbotMatch = (userAgent: string): string | null =>
userAgent.match(pattern)?.[0];
export const isbotMatch = (userAgent: string | null): string | null =>
userAgent?.match(pattern)?.[0] ?? null;

/**
* Find all parts of the user agent that match a bot pattern.
*/
export const isbotMatches = (userAgent: string): string[] =>
export const isbotMatches = (userAgent: string | null): string[] =>
list
.map((part) => userAgent.match(new RegExp(part, "i"))?.[0])
.map((part) => userAgent?.match(new RegExp(part, "i"))?.[0])
.filter(Boolean);

/**
* Find the first bot patterns that match the given user agent.
*/
export const isbotPattern = (userAgent: string): string | null =>
list.find((pattern) => new RegExp(pattern, "i").test(userAgent)) ?? null;
export const isbotPattern = (userAgent: string | null): string | null =>
userAgent
? list.find((pattern) => new RegExp(pattern, "i").test(userAgent)) ?? null
: null;

/**
* Find all bot patterns that match the given user agent.
*/
export const isbotPatterns = (userAgent: string): string[] =>
list.filter((pattern) => new RegExp(pattern, "i").test(userAgent));
export const isbotPatterns = (userAgent: string | null): string[] =>
userAgent
? list.filter((pattern) => new RegExp(pattern, "i").test(userAgent))
: [];
7 changes: 7 additions & 0 deletions tests/spec/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ describe("isbot", () => {
expect(isbot(ua)).toBe(true);
expect(isbot2(ua)).toBe(false);
});
test("all functions can accept null", () => {
expect(isbot(null)).toBe(false);
expect(isbotMatch(null)).toBe(null);
expect(isbotMatches(null)).toEqual([]);
expect(isbotPattern(null)).toBe(null);
expect(isbotPatterns(null)).toEqual([]);
});
});

describe("fixtures", () => {
Expand Down

0 comments on commit d3e37df

Please sign in to comment.