diff --git a/CHANGELOG.md b/CHANGELOG.md index e20b25e..ff75780 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 2.1.7 + +### Added +- **Package Lifecycle API**: + - `SafeTextFilter.isInitialized` getter — check whether word lists have been loaded and the Trie is built. + - `SafeTextFilter.reset()` method — clear loaded Trie word lists and reset initialization state, useful when dynamically switching languages or releasing memory. + ## 2.1.6 ### Documentation diff --git a/README.md b/README.md index d7e6717..ba75c17 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ A high-performance Flutter package for filtering offensive language (profanity) - [Quick Start](#quick-start) - [API Reference](#api-reference) - [`SafeTextFilter.init`](#safetextfilterinit) + - [`SafeTextFilter.isInitialized` \& `SafeTextFilter.reset`](#safetextfilterisinitialized--safetextfilterreset) - [`SafeTextFilter.filterText`](#safetextfilterfiltertext) - [Masking Strategies](#masking-strategies) - [`SafeTextFilter.containsBadWord`](#safetextfiltercontainsbadword) @@ -67,7 +68,7 @@ Or manually add it to your `pubspec.yaml`: ```yaml dependencies: - safe_text: ^2.1.6 + safe_text: ^2.1.7 ``` Then run: @@ -143,6 +144,21 @@ await SafeTextFilter.init(language: Language.all); > **Note:** If neither parameter is provided, the filter defaults to `Language.english`. +### `SafeTextFilter.isInitialized` & `SafeTextFilter.reset` + +Check initialization status or reset loaded word lists dynamically (e.g., when switching languages): + +```dart +// Check if initialized +if (!SafeTextFilter.isInitialized) { + await SafeTextFilter.init(language: Language.english); +} + +// Reset state to reload with a different language +SafeTextFilter.reset(); +await SafeTextFilter.init(language: Language.spanish); +``` + --- ### `SafeTextFilter.filterText` diff --git a/lib/src/safe_text_filter.dart b/lib/src/safe_text_filter.dart index 62680aa..216e382 100644 --- a/lib/src/safe_text_filter.dart +++ b/lib/src/safe_text_filter.dart @@ -11,6 +11,16 @@ class SafeTextFilter { static AhoCorasick? _trie; static bool _isInitialized = false; + /// Whether [SafeTextFilter] has been initialized via [init]. + static bool get isInitialized => _isInitialized; + + /// Resets the internal state of [SafeTextFilter], clearing the loaded Trie + /// word lists. Useful when dynamically switching languages or freeing memory. + static void reset() { + _trie = null; + _isInitialized = false; + } + /// Matches any Unicode letter or digit (covers all scripts: Latin, Arabic, /// Devanagari, CJK, Hangul, Cyrillic, Hebrew, Thai, etc.) static final RegExp _unicodeLetterOrDigit = diff --git a/pubspec.yaml b/pubspec.yaml index 3d72d23..b0cd5f9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: safe_text description: A fast Flutter profanity & swear word filter with phone number detection, powered by Aho-Corasick. Supports 80+ languages and 55,000+ curated bad words. -version: 2.1.6 +version: 2.1.7 homepage: https://github.com/master-wayne7/safe_text repository: https://github.com/master-wayne7/safe_text issue_tracker: https://github.com/master-wayne7/safe_text/issues diff --git a/test/profanity_filter_test.dart b/test/profanity_filter_test.dart index 7913d3b..08630e7 100644 --- a/test/profanity_filter_test.dart +++ b/test/profanity_filter_test.dart @@ -347,5 +347,16 @@ void main() { expect(acTime, isNotNull); }); + + test('will correctly reflect initialization status via isInitialized and reset state via reset()', () async { + SafeTextFilter.reset(); + expect(SafeTextFilter.isInitialized, false); + + await SafeTextFilter.init(language: Language.english); + expect(SafeTextFilter.isInitialized, true); + + SafeTextFilter.reset(); + expect(SafeTextFilter.isInitialized, false); + }); }); }