diff --git a/CHANGELOG.md b/CHANGELOG.md index 53e4d8e..e440538 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 1.0.0 + +* _Tested with dart `2.13`._ +* Refactored to support for null safety. +* Updated SDK constraints to `>=2.12.0 <3.0.0` for null safety. +* Switch to using `package:lints`. + ## 0.2.1 * _Minor pub updates and tested with dart `2.10`._ diff --git a/README.md b/README.md index 1b2d851..95b38ee 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ [![Build Status](https://travis-ci.com/raycardillo/dart_phonetics.svg?branch=master)](https://travis-ci.com/raycardillo/dart_phonetics) [![Pub package](https://img.shields.io/pub/v/dart_phonetics)](https://pub.dev/packages/dart_phonetics) [![Dartdoc reference](https://img.shields.io/badge/dartdoc-reference-blue)](https://pub.dev/documentation/dart_phonetics/latest/) +[![style: lint](https://img.shields.io/badge/style-lint-4BC0F5.svg)](https://pub.dev/packages/lint) [![Project license](https://img.shields.io/badge/license-Apache%202.0-informational)](https://www.apache.org/licenses/LICENSE-2.0) A collection of phonetic algorithms for [Dart](https://dart.dev/) and [Flutter](https://flutter.dev/). These algorithms help find words or names that sound similar by generating an encoding that can be compared or indexed for fuzzy searching. diff --git a/analysis_options.yaml b/analysis_options.yaml index 108d105..117cfbe 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1 +1,6 @@ -include: package:pedantic/analysis_options.yaml +include: package:lints/recommended.yaml + +linter: + rules: + constant_identifier_names: false + non_constant_identifier_names: false \ No newline at end of file diff --git a/example/main.dart b/example/main.dart index 93b76b3..069a4ee 100644 --- a/example/main.dart +++ b/example/main.dart @@ -3,7 +3,7 @@ import 'package:dart_phonetics/dart_phonetics.dart'; void _printResult(PhoneticEncoder encoder, String input) { final encoding = encoder.encode(input); print( - '${encoder?.runtimeType?.toString()} - "$input"\n primary = ${encoding?.primary}\n alternate = ${encoding?.alternates}\n'); + '${encoder.runtimeType.toString()} - "$input"\n primary = ${encoding?.primary}\n alternate = ${encoding?.alternates}\n'); } void main() { @@ -13,7 +13,7 @@ void main() { _printResult(soundex, inputString); final customSoundex = Soundex.fromMapping(Soundex.americanMapping, - maxLength: null, paddingEnabled: false, ignoreHW: false); + maxLength: 0, paddingEnabled: false, ignoreHW: false); _printResult(customSoundex, inputString); final refinedSoundex = RefinedSoundex.defaultEncoder; @@ -22,8 +22,7 @@ void main() { final nysiisOriginal = Nysiis.originalEncoder; _printResult(nysiisOriginal, inputString); - final nysiisModified = - Nysiis.withOptions(maxLength: null, enableModified: true); + final nysiisModified = Nysiis.withOptions(maxLength: 0, enableModified: true); _printResult(nysiisModified, inputString); final doubleMetaphone = DoubleMetaphone.withMaxLength(12); diff --git a/lib/src/double_metaphone.dart b/lib/src/double_metaphone.dart index 7f8e616..f429f11 100644 --- a/lib/src/double_metaphone.dart +++ b/lib/src/double_metaphone.dart @@ -29,22 +29,22 @@ class _DoubleMetaphoneEncoding { /// Buffer used for writing alternate encoding. final StringBuffer _alternate = StringBuffer(); - /// Max length to encode (or `null` if no max length). + /// Max length to encode (defaults to `0` for no max length). final int _maxLength; /// Internal constructor. - _DoubleMetaphoneEncoding([this._maxLength]); + _DoubleMetaphoneEncoding([this._maxLength = 0]); /// Append a [charCode] to the primary encoding. void appendPrimary(final int charCode) { - if (_maxLength == null || _primary.length < _maxLength) { + if (_maxLength <= 0 || _primary.length < _maxLength) { _primary.writeCharCode(charCode); } } /// Append a [charCode] to the alternate encoding. void appendAlternate(final int charCode) { - if (_maxLength == null || _alternate.length < _maxLength) { + if (_maxLength <= 0 || _alternate.length < _maxLength) { _alternate.writeCharCode(charCode); } } @@ -97,11 +97,10 @@ class _DoubleMetaphoneEncoding { } /// Returns `true` if the encoding is maxed out (both encodings are at the - /// max length), `false` otherwise (or if [_maxLength] is `null`). + /// max length), `false` otherwise (or if [_maxLength] is `0`). bool isMaxedOut() { - return _maxLength != null && - _primary.length >= _maxLength && - _alternate.length >= _maxLength; + return _maxLength > 0 && _primary.length >= _maxLength && _alternate + .length >= _maxLength; } /// Returns the string value of the primary encoding. This renders the @@ -142,7 +141,7 @@ class DoubleMetaphone implements PhoneticEncoder { /// Default metaphone encoding length to use. static const int defaultMaxLength = 4; - /// Maximum length of the encoding, where `null` indicates no maximum. + /// Maximum length of the encoding, where `0` indicates no maximum. final int maxLength; // Set.contains() for single character matches are fast and convenient @@ -246,13 +245,18 @@ class DoubleMetaphone implements PhoneticEncoder { //#endregion + /// Encodes a string using the Double Metaphone algorithm as configured. + /// /// Per specification, the encoding always contains two values. If there is /// no alternate, the primary and the alternate encodings will be the same. + /// + /// Returns a [PhoneticEncoding] for the [input] String or + /// `null` if the [input] is empty (after cleaning up). @override - PhoneticEncoding encode(String input) { + PhoneticEncoding? encode(String input) { // clean up the input and convert to uppercase input = PhoneticUtils.clean(input); - if (input == null) { + if (input.isEmpty) { return null; } @@ -368,7 +372,7 @@ class DoubleMetaphone implements PhoneticEncoder { final primary = encoding.primary; final alternate = encoding.alternate; - return PhoneticEncoding(primary, (alternate.isEmpty ? null : {alternate})); + return PhoneticEncoding(primary, {alternate}); } //#region Special Encoding Rules diff --git a/lib/src/encoder.dart b/lib/src/encoder.dart index 1c28fc6..ff03730 100644 --- a/lib/src/encoder.dart +++ b/lib/src/encoder.dart @@ -20,14 +20,14 @@ /// phonetically encoding an input string. class PhoneticEncoderException implements Exception { /// The input that was being processed when the exception occurred. - /// This should only be `null` if the input being encoded was `null`. + /// This may be empty if the input being encoded was empty after cleanup. final String input; /// A message describing the problem that was encountered. final String message; /// Optionally indicates another exception that was the root cause. - final Exception cause; + final Exception? cause; /// Creates a new PhoneticEncoderException with an optional root [cause]. PhoneticEncoderException(this.input, this.message, [this.cause]); @@ -49,7 +49,7 @@ class PhoneticEncoding { final String primary; /// An alternative phonetic encoding for algorithms that support this. - final Set alternates; + final Set? alternates; /// Creates an instance of this data class. PhoneticEncoding(this.primary, [this.alternates]); @@ -63,6 +63,6 @@ class PhoneticEncoding { /// The common interface for all phonetic encoders. abstract class PhoneticEncoder { - /// Returns a [PhoneticEncoding] for the [input] String. - PhoneticEncoding encode(String input); + /// Returns a [PhoneticEncoding] for the [input] String or `null`. + PhoneticEncoding? encode(String input); } diff --git a/lib/src/nysiis.dart b/lib/src/nysiis.dart index c20c6cf..318ec7c 100644 --- a/lib/src/nysiis.dart +++ b/lib/src/nysiis.dart @@ -56,7 +56,7 @@ class Nysiis implements PhoneticEncoder { /// Default encoding length to use for the modified algorithm. static const int defaultModifiedMaxLength = 8; - /// Maximum length of the encoding, where `null` indicates no maximum. + /// Maximum length of the encoding, where `0` indicates no maximum. final int maxLength; /// Indicates if the "modified" rules should be applied when encoding. @@ -276,9 +276,7 @@ class Nysiis implements PhoneticEncoder { // NOTE: per specification, this loop starts at the second character. for (var i = 1; i < chars.length; i++) { final char = chars[i]; - if (char == null) { - break; - } else if (PhoneticUtils.isSpecialCharacter(char)) { + if (PhoneticUtils.isSpecialCharacter(char)) { continue; } @@ -401,11 +399,14 @@ class Nysiis implements PhoneticEncoder { /// Encodes a string using the NYSIIS algorithm as configured. This encoder /// does not produce any [PhoneticEncoding.alternates] values. + /// + /// Returns a [PhoneticEncoding] for the [input] String or + /// `null` if the [input] is empty (after cleaning up). @override - PhoneticEncoding encode(String input) { + PhoneticEncoding? encode(String input) { // clean up the input and convert to uppercase input = PhoneticUtils.clean(input); - if (input == null) { + if (input.isEmpty) { return null; } @@ -430,7 +431,7 @@ class Nysiis implements PhoneticEncoder { // truncate the encoding to maxLength if required var finalEncoding = String.fromCharCodes(encoding); - if (maxLength != null && finalEncoding.length > maxLength) { + if (maxLength > 0 && finalEncoding.length > maxLength) { finalEncoding = finalEncoding.substring(0, maxLength); } diff --git a/lib/src/refined_soundex.dart b/lib/src/refined_soundex.dart index 1eb3c24..bd6b522 100644 --- a/lib/src/refined_soundex.dart +++ b/lib/src/refined_soundex.dart @@ -36,7 +36,7 @@ class RefinedSoundex implements PhoneticEncoder { /// ignore the input character and do not encode it (e.g., vowels). final Map soundexMapping; - /// Maximum length of the encoding (and how much to pad if [paddingEnabled]). + /// Maximum length of the encoding, where `0` indicates no maximum. final int maxLength; /// This is a default mapping of the 26 letters used in US English. @@ -81,7 +81,7 @@ class RefinedSoundex implements PhoneticEncoder { /// Creates a custom Soundex instance. This constructor can be used to /// provide custom mappings for non-Western character sets, etc. factory RefinedSoundex.fromMapping(final Map soundexMapping, - {int maxLength}) => + {int maxLength = 0}) => RefinedSoundex._internal(Map.unmodifiable(soundexMapping), maxLength); /// Gets the [defaultEncoder] instance of a RefinedSoundex encoder. @@ -89,13 +89,15 @@ class RefinedSoundex implements PhoneticEncoder { //#endregion - /// Returns a [PhoneticEncoding] for the [input] String. - /// Returns `null` if the input is `null` or empty (after cleaning up). + /// Encodes a string using the Refined Soundex algorithm as configured. + /// + /// Returns a [PhoneticEncoding] for the [input] String or + /// `null` if the [input] is empty (after cleaning up). @override - PhoneticEncoding encode(String input) { + PhoneticEncoding? encode(String input) { // clean up the input and convert to uppercase input = PhoneticUtils.clean(input, allowLatin: false); - if (input == null) { + if (input.isEmpty) { return null; } @@ -105,7 +107,7 @@ class RefinedSoundex implements PhoneticEncoder { // always write first character soundex.writeCharCode(input.codeUnitAt(0)); - int last, current; + int? last, current; last = $asterisk; // encode all characters @@ -117,7 +119,7 @@ class RefinedSoundex implements PhoneticEncoder { soundex.writeCharCode(current); } - if (maxLength != null && soundex.length >= maxLength) { + if (maxLength > 0 && soundex.length >= maxLength) { break; } diff --git a/lib/src/soundex.dart b/lib/src/soundex.dart index 34ce92c..4ac3f84 100644 --- a/lib/src/soundex.dart +++ b/lib/src/soundex.dart @@ -85,7 +85,8 @@ class Soundex implements PhoneticEncoder { /// The character to use for padding (when [paddingEnabled] is `true`). final int paddingChar; - /// Maximum length of the encoding (and how much to pad if [paddingEnabled]). + /// Maximum length of the encoding, where `0` indicates no maximum, + /// and how much to pad if [paddingEnabled]. final int maxLength; /// This is a default mapping of the 26 letters used in US English. @@ -186,13 +187,13 @@ class Soundex implements PhoneticEncoder { return input.split(RegExp(r'\s*-\s*')); } - /// Returns a single encoding for the [input] String. - /// Returns `null` if the input is `null` or empty (after cleaning up). + /// Returns a [PhoneticEncoding] for the [input] String or + /// an empty string if the [input] is empty (after cleaning up). String _encode(String input) { // clean up the input and convert to uppercase input = PhoneticUtils.clean(input, allowLatin: false); - if (input == null) { - return null; + if (input.isEmpty) { + return input; } // we'll write to a buffer to avoid string copies @@ -228,13 +229,13 @@ class Soundex implements PhoneticEncoder { } } - if (maxLength != null && soundex.length >= maxLength) { + if (maxLength > 0 && soundex.length >= maxLength) { break; } } // pad the encoding if required - if (paddingEnabled && maxLength != null) { + if (paddingEnabled && maxLength > 0) { while (soundex.length < maxLength) { soundex.writeCharCode(paddingChar); } @@ -252,11 +253,15 @@ class Soundex implements PhoneticEncoder { } } - /// Returns a [PhoneticEncoding] for the [input] String. - /// Returns `null` if the input is `null` or empty (after cleaning up). + /// Encodes a string using the Soundex algorithm as configured. + /// + /// Returns a [PhoneticEncoding] for the [input] String or + /// `null` if the [input] is empty (after cleaning up). @override - PhoneticEncoding encode(String input) { - if (input == null || input.isEmpty) { + PhoneticEncoding? encode(String input) { + // clean up the input and convert to uppercase + input = PhoneticUtils.clean(input, allowLatin: false); + if (input.isEmpty) { return null; } @@ -285,17 +290,15 @@ class Soundex implements PhoneticEncoder { // now go through all parts and add more alternates while (iterator.moveNext()) { final part = iterator.current; - if (part != null) { - alternates.add(_encode(part)); - if (prefixesEnabled) { - _addTrimmedPrefixToAlternates(alternates, part); - } + alternates.add(_encode(part)); + if (prefixesEnabled) { + _addTrimmedPrefixToAlternates(alternates, part); } } // remove the primary if it made it into the alternate list from others alternates.remove(primary); - return PhoneticEncoding(primary, alternates.isEmpty ? null : alternates); + return PhoneticEncoding(primary, alternates); } } diff --git a/lib/src/utils.dart b/lib/src/utils.dart index 07b16d8..993c4fd 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -125,8 +125,8 @@ class PhoneticUtils { /// need to do additional cleaning if you're working with strings /// that are particularly dirty. static String clean(final String value, {bool allowLatin = true}) { - if (value == null || value.isEmpty) { - return null; + if (value.isEmpty) { + return value; } final cleaned = value @@ -136,7 +136,7 @@ class PhoneticUtils { .replaceAll( (allowLatin) ? _cleanLatinAllowed : _cleanLatinNotAllowed, ''); - return cleaned.isEmpty ? null : cleaned; + return cleaned; } /// Returns the character from [value] at [index] or [$nul] if the index is @@ -186,7 +186,7 @@ class PhoneticUtils { /// /// Despite the name, this is actually a measure of similarity. /// This naming is consistent with the SQL `DIFFERENCE` function definition. - static int differenceEncoded(final String e1, final String e2) { + static int differenceEncoded(final String? e1, final String? e2) { if (e1 == null || e1.isEmpty || e2 == null || e2.isEmpty) { return 0; } diff --git a/misc/experiments/single_char_exp.dart b/misc/experiments/single_char_exp.dart index 0d8bf07..40423c3 100644 --- a/misc/experiments/single_char_exp.dart +++ b/misc/experiments/single_char_exp.dart @@ -30,9 +30,8 @@ void main() { final watch = Stopwatch(); const numIterations = 2000000; - var vowelsFound; + var vowelsFound = 0; - vowelsFound = 0; print('running if statement matching...'); watch.start(); for (var i = 0; i < numIterations; i++) { diff --git a/pubspec.lock b/pubspec.lock index 90513b6..2597f43 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,342 +7,342 @@ packages: name: _fe_analyzer_shared url: "https://pub.dartlang.org" source: hosted - version: "11.0.0" + version: "22.0.0" analyzer: dependency: transitive description: name: analyzer url: "https://pub.dartlang.org" source: hosted - version: "0.40.4" + version: "1.7.0" args: dependency: transitive description: name: args url: "https://pub.dartlang.org" source: hosted - version: "1.6.0" + version: "2.1.1" async: dependency: transitive description: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.4.2" + version: "2.7.0" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0" charcode: dependency: "direct main" description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.1.3" + version: "1.3.1" cli_util: dependency: transitive description: name: cli_util url: "https://pub.dartlang.org" source: hosted - version: "0.2.0" + version: "0.3.0" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.14.13" + version: "1.15.0" convert: dependency: transitive description: name: convert url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" + version: "3.0.0" coverage: dependency: transitive description: name: coverage url: "https://pub.dartlang.org" source: hosted - version: "0.14.1" + version: "1.0.3" crypto: dependency: transitive description: name: crypto url: "https://pub.dartlang.org" source: hosted - version: "2.1.5" - glob: + version: "3.0.1" + file: dependency: transitive description: - name: glob + name: file url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" - http: + version: "6.1.2" + frontend_server_client: dependency: transitive description: - name: http + name: frontend_server_client url: "https://pub.dartlang.org" source: hosted - version: "0.12.2" + version: "2.1.0" + glob: + dependency: transitive + description: + name: glob + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" http_multi_server: dependency: transitive description: name: http_multi_server url: "https://pub.dartlang.org" source: hosted - version: "2.2.0" + version: "3.0.1" http_parser: dependency: transitive description: name: http_parser url: "https://pub.dartlang.org" source: hosted - version: "3.1.4" + version: "4.0.0" io: dependency: transitive description: name: io url: "https://pub.dartlang.org" source: hosted - version: "0.3.4" + version: "1.0.0" js: dependency: transitive description: name: js url: "https://pub.dartlang.org" source: hosted - version: "0.6.2" + version: "0.6.3" + lints: + dependency: "direct dev" + description: + name: lints + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" logging: dependency: transitive description: name: logging url: "https://pub.dartlang.org" source: hosted - version: "0.11.4" + version: "1.0.1" matcher: dependency: transitive description: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.9" + version: "0.12.10" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.2.3" + version: "1.4.0" mime: dependency: transitive description: name: mime url: "https://pub.dartlang.org" source: hosted - version: "0.9.7" - node_interop: - dependency: transitive - description: - name: node_interop - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.1" - node_io: - dependency: transitive - description: - name: node_io - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.1" + version: "1.0.0" node_preamble: dependency: transitive description: name: node_preamble url: "https://pub.dartlang.org" source: hosted - version: "1.4.12" + version: "2.0.1" package_config: dependency: transitive description: name: package_config url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "2.0.0" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0" pedantic: - dependency: "direct dev" + dependency: transitive description: name: pedantic url: "https://pub.dartlang.org" source: hosted - version: "1.9.2" + version: "1.11.1" pool: dependency: transitive description: name: pool url: "https://pub.dartlang.org" source: hosted - version: "1.4.0" + version: "1.5.0" pub_semver: dependency: transitive description: name: pub_semver url: "https://pub.dartlang.org" source: hosted - version: "1.4.4" + version: "2.0.0" shelf: dependency: transitive description: name: shelf url: "https://pub.dartlang.org" source: hosted - version: "0.7.9" + version: "1.1.4" shelf_packages_handler: dependency: transitive description: name: shelf_packages_handler url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "3.0.0" shelf_static: dependency: transitive description: name: shelf_static url: "https://pub.dartlang.org" source: hosted - version: "0.2.8" + version: "1.0.0" shelf_web_socket: dependency: transitive description: name: shelf_web_socket url: "https://pub.dartlang.org" source: hosted - version: "0.2.3" + version: "1.0.1" source_map_stack_trace: dependency: transitive description: name: source_map_stack_trace url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0" source_maps: dependency: transitive description: name: source_maps url: "https://pub.dartlang.org" source: hosted - version: "0.10.9" + version: "0.10.10" source_span: dependency: transitive description: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.1" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.5" + version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.5" + version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0" test: dependency: "direct dev" description: name: test url: "https://pub.dartlang.org" source: hosted - version: "1.15.4" + version: "1.17.8" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.18" + version: "0.4.1" test_core: dependency: transitive description: name: test_core url: "https://pub.dartlang.org" source: hosted - version: "0.3.11+1" + version: "0.3.28" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0" vm_service: dependency: transitive description: name: vm_service url: "https://pub.dartlang.org" source: hosted - version: "4.2.0" + version: "7.1.0" watcher: dependency: transitive description: name: watcher url: "https://pub.dartlang.org" source: hosted - version: "0.9.7+15" + version: "1.0.0" web_socket_channel: dependency: transitive description: name: web_socket_channel url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "2.1.0" webkit_inspection_protocol: dependency: transitive description: name: webkit_inspection_protocol url: "https://pub.dartlang.org" source: hosted - version: "0.7.3" + version: "1.0.0" yaml: dependency: transitive description: name: yaml url: "https://pub.dartlang.org" source: hosted - version: "2.2.1" + version: "3.1.0" sdks: - dart: ">=2.7.0 <3.0.0" + dart: ">=2.12.0 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 548a6b9..4094c52 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,15 +1,15 @@ name: dart_phonetics description: A collection of phonetic algorithms. These algorithms help find words or names that sound similar by generating an encoding that can be compared or indexed for fuzzy searching. -version: 0.2.1 +version: 1.0.0 homepage: https://github.com/raycardillo/dart_phonetics repository: https://github.com/raycardillo/dart_phonetics environment: - sdk: ">=2.2.2 <3.0.0" + sdk: ">=2.12.0 <3.0.0" dependencies: - charcode: ^1.1.3 + charcode: ^1.3.1 dev_dependencies: - test: ^1.15.4 - pedantic: ^1.9.2 + test: ^1.17.8 + lints: ^1.0.1 diff --git a/test/double_metaphone_test.dart b/test/double_metaphone_test.dart index 5155cd3..32e5657 100644 --- a/test/double_metaphone_test.dart +++ b/test/double_metaphone_test.dart @@ -56,7 +56,7 @@ void main() { // https://scholar.google.com/scholar?cluster=634245576371390488&hl=en&as_sdt=0,21&as_vis=1 test('test performance paper examples', () { final encoder = DoubleMetaphone.defaultEncoder; - final encoderNoMax = DoubleMetaphone.withMaxLength(null); + final encoderNoMax = DoubleMetaphone.withMaxLength(0); //expectEncoding(encoder, 'Phonetic', 'FNTK'); expectEncoding(encoderNoMax, 'Phonetic', 'FNTK'); diff --git a/test/nysiis_test.dart b/test/nysiis_test.dart index 3c7a2be..517ebad 100644 --- a/test/nysiis_test.dart +++ b/test/nysiis_test.dart @@ -49,7 +49,7 @@ void main() { expectEncoding(encoder8, 'PFEISTER', 'FASTAR'); expectEncoding(encoder8, 'SCHOENHOEFT', 'SANAFT'); - final encoderNoMax = Nysiis.withOptions(maxLength: null); + final encoderNoMax = Nysiis.withOptions(maxLength: 0); expectEncoding(encoderNoMax, 'PHILLIPSON', 'FALAPSAN'); expectEncoding(encoderNoMax, 'PFEISTER', 'FASTAR'); expectEncoding(encoderNoMax, 'SCHOENHOEFT', 'SANAFT'); @@ -60,7 +60,7 @@ void main() { // https://scholar.google.com/scholar?cluster=634245576371390488&hl=en&as_sdt=0,21&as_vis=1 test('test performance paper examples', () { final encoder = Nysiis.originalEncoder; - final encoderNoMax = Nysiis.withOptions(maxLength: null); + final encoderNoMax = Nysiis.withOptions(maxLength: 0); expectEncoding(encoder, 'Phonetic', 'FANATA'); expectEncoding(encoderNoMax, 'Phonetic', 'FANATAC'); @@ -75,7 +75,7 @@ void main() { // version the "modified" implementation is not the same as the USDA // version of the modified algorithm that is implemented here. test('test original dropby.com examples', () { - final encoder = Nysiis.withOptions(maxLength: null); + final encoder = Nysiis.withOptions(maxLength: 0); expectEncoding(encoder, 'MACINTOSH', 'MCANT'); expectEncoding(encoder, 'KNUTH', 'NAT'); expectEncoding(encoder, 'KOEHN', 'CAN'); @@ -117,7 +117,7 @@ void main() { // http://ntz-develop.blogspot.com/2011/03/phonetic-algorithms.html test('test ntz examples', () { - final encoder = Nysiis.withOptions(maxLength: null); + final encoder = Nysiis.withOptions(maxLength: 0); expectEncoding(encoder, 'Diggell', 'DAGAL'); expectEncoding(encoder, 'Dougal', 'DAGAL'); expectEncoding(encoder, 'Doughill', 'DAGAL'); @@ -281,7 +281,7 @@ void main() { expectEncoding(encoder8, 'SCHOENHOEFT', 'SANAFT'); final encoderNoMax = - Nysiis.withOptions(maxLength: null, enableModified: true); + Nysiis.withOptions(maxLength: 0, enableModified: true); expectEncoding(encoderNoMax, 'PHILLIPSON', 'FALAPSAN'); expectEncoding(encoderNoMax, 'PFEISTER', 'FASTAR'); expectEncoding(encoderNoMax, 'SCHOENHOEFT', 'SANAFT'); @@ -293,7 +293,7 @@ void main() { // version the "modified" implementation is not the same as the USDA // version of the modified algorithm that is implemented here. test('test modified dropby.com examples', () { - final encoder = Nysiis.withOptions(maxLength: null, enableModified: true); + final encoder = Nysiis.withOptions(maxLength: 0, enableModified: true); expectEncoding(encoder, 'MACINTOSH', 'MCANTAS'); expectEncoding(encoder, 'KNUTH', 'NAT'); expectEncoding(encoder, 'KOEHN', 'CAN'); diff --git a/test/refined_soundex_test.dart b/test/refined_soundex_test.dart index 5c98721..b4ecb56 100644 --- a/test/refined_soundex_test.dart +++ b/test/refined_soundex_test.dart @@ -88,7 +88,7 @@ void main() { // test some strings with irregular characters expectEncoding(encoder, '#@', null); expectEncoding(encoder, '', 'T6036084'); - expectEncoding(encoder, '\0#tes@ting!', 'T6036084'); + expectEncoding(encoder, '${$nul}#tes@ting!', 'T6036084'); expectEncoding(encoder, ' \t\n\r Washington \t\n\r ', 'W03084608'); }); @@ -130,9 +130,7 @@ void main() { // testing examples from: // http://ntz-develop.blogspot.com/2011/03/phonetic-algorithms.html - var inputs; - - inputs = [ + var inputs = [ 'Braz', 'Broz', ]; @@ -196,7 +194,6 @@ void main() { final encoder = RefinedSoundex(); // Edge cases - expect(0, PhoneticUtils.primaryDifference(encoder, null, null)); expect(0, PhoneticUtils.primaryDifference(encoder, '', '')); expect(0, PhoneticUtils.primaryDifference(encoder, ' ', ' ')); diff --git a/test/smoke_test.dart b/test/smoke_test.dart index 8771461..0b3c686 100644 --- a/test/smoke_test.dart +++ b/test/smoke_test.dart @@ -25,8 +25,7 @@ import 'test_utils.dart'; void main() { group('Utility Smoke Tests', () { test('null behavior', () { - expect(PhoneticUtils.clean(null), null); - expect(PhoneticUtils.clean(''), null); + expect(PhoneticUtils.clean(''), ''); expect(PhoneticUtils.differenceEncoded(null, ''), 0); expect(PhoneticUtils.differenceEncoded('', null), 0); }); diff --git a/test/soundex_test.dart b/test/soundex_test.dart index 2ba99d6..0b69e85 100644 --- a/test/soundex_test.dart +++ b/test/soundex_test.dart @@ -90,7 +90,7 @@ void main() { expectEncoding(encoder10, 'supercalifragilistic', 'S162416242'); final encoderNoMax = - Soundex.fromMapping(Soundex.americanMapping, maxLength: null); + Soundex.fromMapping(Soundex.americanMapping, maxLength: 0); expectEncoding(encoderNoMax, 'testing', 'T2352'); expectEncoding(encoderNoMax, 'supercalifragilistic', 'S16241624232'); }); @@ -100,7 +100,7 @@ void main() { expectEncoding(encoder, '#@', null); expectEncoding(encoder, '', 'T235'); - expectEncoding(encoder, '\0#tes@ting!', 'T235'); + expectEncoding(encoder, '${$nul}#tes@ting!', 'T235'); expectEncoding(encoder, ' \t\n\r Washington \t\n\r ', 'W252'); }); @@ -110,7 +110,7 @@ void main() { test('test performance paper examples', () { final encoder = Soundex(); final encoderNoMax = - Soundex.fromMapping(Soundex.americanMapping, maxLength: null); + Soundex.fromMapping(Soundex.americanMapping, maxLength: 0); expectEncoding(encoder, 'Phonetic', 'P532'); expectEncoding(encoderNoMax, 'Phonetic', 'P532'); @@ -387,9 +387,7 @@ void main() { expectEncoding(encoder, 'Fusedale', 'F234'); - var inputs; - - inputs = [ + var inputs = [ 'Genthner', 'Gentner', 'Gianettini', @@ -617,7 +615,6 @@ void main() { final encoder = Soundex(); // Edge cases - expect(0, PhoneticUtils.primaryDifference(encoder, null, null)); expect(0, PhoneticUtils.primaryDifference(encoder, '', '')); expect(0, PhoneticUtils.primaryDifference(encoder, ' ', ' ')); diff --git a/test/test_utils.dart b/test/test_utils.dart index 5dbb683..0ef9d2e 100644 --- a/test/test_utils.dart +++ b/test/test_utils.dart @@ -23,13 +23,15 @@ import 'package:test/test.dart'; void expectEncodingEquals( PhoneticEncoder encoder, String input1, String input2) { final encoding1 = encoder.encode(input1); + final encoding1Alternates = encoding1?.alternates; final encoding2 = encoder.encode(input2); + final encoding2Alternates = encoding2?.alternates; final foundMatch = ((encoding1?.primary == encoding2?.primary) || - (encoding1?.alternates != null && - encoding1.alternates.contains(encoding2?.primary)) || - (encoding2?.alternates != null && - encoding2.alternates.contains(encoding1?.primary))); + (encoding1Alternates != null && + encoding1Alternates.contains(encoding2?.primary)) || + (encoding2Alternates != null && + encoding2Alternates.contains(encoding1?.primary))); expect(true, foundMatch, reason: 'Match not found between ' @@ -38,8 +40,8 @@ void expectEncodingEquals( /// Verify an encoding matches [expected] for a single [input]. void expectEncoding( - PhoneticEncoder encoder, String input, String expectedPrimary, - [List expectedAlternates]) { + PhoneticEncoder encoder, String input, String? expectedPrimary, + [List? expectedAlternates]) { final encoding = encoder.encode(input); expect(encoding?.primary, expectedPrimary, reason: 'Primary failed for input=$input'); @@ -51,9 +53,9 @@ void expectEncoding( /// Verify an encoding matches [expected] for a list of [inputs]. void expectEncodings( - PhoneticEncoder encoder, List inputs, String expectedPrimary, - [List expectedAlternates]) { - inputs.forEach((input) { + PhoneticEncoder encoder, List inputs, String? expectedPrimary, + [List? expectedAlternates]) { + for (var input in inputs) { expectEncoding(encoder, input, expectedPrimary, expectedAlternates); - }); + } }