Skip to content

Commit

Permalink
Accept "undefined" in place of userAgent string
Browse files Browse the repository at this point in the history
  • Loading branch information
omrilotan committed Jan 4, 2024
1 parent 0a22693 commit 8ecaa12
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 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.3.0](https://github.com/omrilotan/isbot/compare/v4.2.0...v4.3.0)

- Accept `undefined` in place of user agent string to allow headers property to be used "as is" (`request.headers["user-agent"]`)

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

- Accept `null` in place of user agent string to allow header value to be used "as is" (`request.headers.get("user-agent")`)
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.2.0",
"version": "4.3.0",
"description": "🤖 Recognise bots/crawlers/spiders using the user agent string.",
"keywords": [
"bot",
Expand Down
12 changes: 6 additions & 6 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 | null): 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 | null) => boolean) =>
(customPattern: RegExp): ((userAgent?: string | null) => boolean) =>
(userAgent: string): boolean =>
Boolean(userAgent) && customPattern.test(userAgent);

Expand All @@ -41,29 +41,29 @@ export const createIsbotFromList = (
/**
* Find the first part of the user agent that matches a bot pattern.
*/
export const isbotMatch = (userAgent: string | null): string | null =>
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 | null): string[] =>
export const isbotMatches = (userAgent?: string | null): string[] =>
list
.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 | null): string | 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 | null): string[] =>
export const isbotPatterns = (userAgent?: string | null): string[] =>
userAgent
? list.filter((pattern) => new RegExp(pattern, "i").test(userAgent))
: [];
14 changes: 8 additions & 6 deletions tests/spec/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ 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([]);
test.each(
[null, undefined, ""]
)("all functions can accept %p", (value: string | null | undefined) => {
expect(isbot(value)).toBe(false);
expect(isbotMatch(value)).toBe(null);
expect(isbotMatches(value)).toEqual([]);
expect(isbotPattern(value)).toBe(null);
expect(isbotPatterns(value)).toEqual([]);
});
});

Expand Down

0 comments on commit 8ecaa12

Please sign in to comment.