Skip to content

Add support to ignore diacritics #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.8.2"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,14 +21,14 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
Expand All @@ -42,14 +42,21 @@ packages:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
version: "1.16.0"
diacritic:
dependency: transitive
description:
name: diacritic
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.0"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -66,21 +73,28 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.4"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -92,7 +106,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.2"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -120,7 +134,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.27"
version: "1.0.33"
term_glyph:
dependency: transitive
description:
Expand All @@ -134,20 +148,13 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "0.4.9"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.2"
sdks:
dart: ">=2.12.0 <3.0.0"
dart: ">=2.17.0-0 <3.0.0"
12 changes: 11 additions & 1 deletion lib/substring_highlight.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library substring_highlight;

import 'package:diacritic/diacritic.dart';
import 'package:flutter/material.dart';

final int __int64MaxValue = double.maxFinite.toInt();
Expand All @@ -8,6 +9,7 @@ final int __int64MaxValue = double.maxFinite.toInt();
class SubstringHighlight extends StatelessWidget {
SubstringHighlight(
{this.caseSensitive = false,
this.ignoreDiacritics = false,
this.maxLines,
this.overflow = TextOverflow.clip,
this.term,
Expand All @@ -27,6 +29,11 @@ class SubstringHighlight extends StatelessWidget {
})
: assert(term != null || terms != null);

/// By default, diacritics are not ignored, which means "e" will not match "é".
/// If this parameter is set to true, diacritics will be converted to their non-diacritic
/// form to determine matches
final bool ignoreDiacritics;

/// By default the search terms are case insensitive. Pass false to force case sensitive matches.
final bool caseSensitive;

Expand Down Expand Up @@ -67,14 +74,17 @@ class SubstringHighlight extends StatelessWidget {

@override
Widget build(BuildContext context) {
final String textLC = caseSensitive ? text : text.toLowerCase();
final String textDiacritics =
ignoreDiacritics ? removeDiacritics(text) : text;
final String textLC = caseSensitive ? text : textDiacritics.toLowerCase();

// corner case: if both term and terms array are passed then combine
final List<String> termList = [term ?? '', ...(terms ?? [])];

// remove empty search terms ('') because they cause infinite loops
final List<String> termListLC = termList
.where((s) => s.isNotEmpty)
.map((s) => ignoreDiacritics ? removeDiacritics(s) : s)
.map((s) => caseSensitive ? s : s.toLowerCase())
.toList();

Expand Down
45 changes: 26 additions & 19 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.8.2"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,14 +21,14 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
Expand All @@ -42,14 +42,21 @@ packages:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
version: "1.16.0"
diacritic:
dependency: "direct main"
description:
name: diacritic
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.0"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -66,21 +73,28 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.4"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -92,7 +106,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.2"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -127,20 +141,13 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "0.4.9"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.2"
sdks:
dart: ">=2.12.0 <3.0.0"
dart: ">=2.17.0-0 <3.0.0"
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ environment:
sdk: ">=2.12.0 <3.0.0"

dependencies:
diacritic: ^0.1.3
flutter:
sdk: flutter

Expand Down