From 1969ad3f8f2e05688550004cf9a640595c8cb054 Mon Sep 17 00:00:00 2001 From: omrilotan Date: Thu, 15 Feb 2024 16:26:43 +0000 Subject: [PATCH] Replace "pattern" export with "getPattern" method --- CHANGELOG.md | 4 ++++ README.md | 2 +- package.json | 2 +- src/index.ts | 34 ++++++++++++++++------------------ 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b829bb..4e006df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 4819046..5e1290f 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/package.json b/package.json index 289f098..3648661 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 2dff947..ed62699 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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. @@ -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); } /** @@ -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.