Skip to content

Commit

Permalink
Replace "pattern" export with "getPattern" method
Browse files Browse the repository at this point in the history
  • Loading branch information
omrilotan committed Feb 15, 2024
1 parent 6961611 commit 1969ad3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [5.0.0](https://github.com/omrilotan/isbot/compare/v4.4.0...v5.0.0)

- Remove named export "pattern" from the interface, instead use "getPattern" method

## [4.4.0](https://github.com/omrilotan/isbot/compare/v4.3.0...v4.4.0)

- Add a naive fallback pattern for engines that do not support lookbehind in regular expressions
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Using JSDeliver CDN you can import an iife script
| ------------------- | -------------------------------- | ---------------------------------------------------------------------------- |
| isbot | _(string?): boolean_ | Check if the user agent is a bot |
| isbotNaive | _(string?): boolean_ | Check if the user agent is a bot using a naive pattern (less accurate) |
| pattern | _RegExp_ | The regular expression used to identify bots |
| getPattern | (): _RegExp_ | The regular expression used to identify bots |
| list | _string[]_ | List of all individual pattern parts |
| isbotMatch | _(string?): string \| null_ | The substring matched by the regular expression |
| isbotMatches | _(string?): string[]_ | All substrings matched by the regular expression |
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.4.0",
"version": "5.0.0",
"description": "🤖 Recognise bots/crawlers/spiders using the user agent string.",
"keywords": [
"bot",
Expand Down
34 changes: 16 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { fullPattern, regularExpression } from "./pattern";
import patternsList from "./patterns.json";
import { fullPattern } from "./pattern";

/**
* Naive bot pattern.
*/
const naivePattern = /bot|spider|crawl|http|lighthouse/i;

// Workaround for TypeScript's type definition of imported variables and JSON files.

/**
* A pattern that matches bot identifiers in user agent strings.
*/
export const pattern = regularExpression;
let pattern: RegExp;
export function getPattern(): RegExp {
if (pattern instanceof RegExp) {
return pattern;
}
try {
// Build this RegExp dynamically to avoid syntax errors in older engines.
pattern = new RegExp(fullPattern, "i");
} catch (error) {
pattern = naivePattern;
}
return pattern;
}

/**
* A list of bot identifiers to be used in a regular expression against user agent strings.
Expand All @@ -24,20 +31,11 @@ export const list: string[] = patternsList;
export const isbotNaive = (userAgent?: string | null): boolean =>
Boolean(userAgent) && naivePattern.test(userAgent);

let usedPattern: RegExp;
/**
* Check if the given user agent includes a bot pattern.
*/
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;
}
}
return Boolean(userAgent) && usedPattern.test(userAgent);
return Boolean(userAgent) && getPattern().test(userAgent);
}

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

/**
* Find all parts of the user agent that match a bot pattern.
Expand Down

0 comments on commit 1969ad3

Please sign in to comment.