From bc9df8c672dce4fa7ebe72a524cb023153b6c380 Mon Sep 17 00:00:00 2001 From: Jack Warren Date: Fri, 17 May 2019 22:53:12 -0400 Subject: [PATCH] run dartfmt --- example/date_example.dart | 3 +- lib/date.dart | 10 ++- lib/name.dart | 25 ++++-- lib/src/date/parsed_date.dart | 5 +- lib/src/name/parsed_name.dart | 16 +++- test/date_test.dart | 140 +++++++++++++++++++++++++--------- test/name_test.dart | 45 +++++++---- 7 files changed, 175 insertions(+), 69 deletions(-) diff --git a/example/date_example.dart b/example/date_example.dart index e31d5ea..98e57e8 100644 --- a/example/date_example.dart +++ b/example/date_example.dart @@ -8,4 +8,5 @@ import 'package:best_effort_parser/date.dart'; /// $ dart date_example.dart 'January 1st, 2019' /// [Day]: 1 [Month]: 1 [Year]: 2019 /// ``` -void main(List arguments) => DateParser.basic().parse(arguments.join(' ')).forEach(print); \ No newline at end of file +void main(List arguments) => + DateParser.basic().parse(arguments.join(' ')).forEach(print); diff --git a/lib/date.dart b/lib/date.dart index 9c98d64..a283f5a 100644 --- a/lib/date.dart +++ b/lib/date.dart @@ -222,7 +222,9 @@ class DateParser { final List dayParts = [], monthParts = [], yearParts = []; // collect parts - RegExp(r'((?:[\d]+\D){2}[\d]+)|([^\W_]+)').allMatches(text).forEach((Match match) { + RegExp(r'((?:[\d]+\D){2}[\d]+)|([^\W_]+)') + .allMatches(text) + .forEach((Match match) { if (match.group(1) != null) { // Compact format matching ((?:[\d]+\D){2}[\d]+), like `MM/DD/YY` final numbers = RegExp(r'(\d+)') @@ -271,7 +273,8 @@ class DateParser { if (_seasons.isNotEmpty && _seasonToMonth != null) { for (int s = 0; s < _seasons.length; s++) { if (part.contains(_seasons[s])) { - monthParts.add(_seasonToMonth[s + 1] ?? defaultSeasonToMonthApproximations[s + 1]); + monthParts.add(_seasonToMonth[s + 1] ?? + defaultSeasonToMonthApproximations[s + 1]); } } } @@ -323,7 +326,8 @@ class DateParser { /// Turn [year] into a four digit number if necessary and possible. int _toYear(int year) { - if (year.toString().length < 4 && _fourDigitOffsets.firstKeyAfter(year) != null) { + if (year.toString().length < 4 && + _fourDigitOffsets.firstKeyAfter(year) != null) { return year + _fourDigitOffsets[_fourDigitOffsets.firstKeyAfter(year)]; } else { return year; diff --git a/lib/name.dart b/lib/name.dart index c7f8e08..97e3c13 100644 --- a/lib/name.dart +++ b/lib/name.dart @@ -7,7 +7,10 @@ import 'src/name/parsed_name.dart'; /// The values given as parameters to this function will never be null. If a name does not have a /// given component, an empty string will be given to this function's corresponding parameter. typedef NameParserOutput = T Function(String family, - {String given, String droppingParticle, String nonDroppingParticle, String suffix}); + {String given, + String droppingParticle, + String nonDroppingParticle, + String suffix}); /// A parser designed to extract a person's name from an arbitrary string. The [parse] method /// will create a [T] object with the person's family name, given name, particles (dropping and @@ -48,7 +51,8 @@ class NameParser { /// - `jr`, `sr` /// - Roman numerals `i` - `xiii` /// - `esq`, `cpa`, `dc`, `dds`, `vm`, `jd`, `md`, `phd` - static const String defaultSuffixes = r'^([js]r|[vx]?i{0,3}|i[vx]|esq|cpa|dc|dds|vm|[jm]d|phd)$'; + static const String defaultSuffixes = + r'^([js]r|[vx]?i{0,3}|i[vx]|esq|cpa|dc|dds|vm|[jm]d|phd)$'; /// A [RegExp] pattern matching a variety of name particles. /// @@ -91,9 +95,14 @@ class NameParser { Pattern particles = defaultParticles, Pattern punctuation = defaultPunctuation}) { _nameParserOutput = nameParserOutput; - _suffixes = suffixes is String ? RegExp(suffixes, caseSensitive: false) : suffixes; - _particles = particles is String ? RegExp(particles, caseSensitive: false) : particles; - _punctuation = punctuation is String ? RegExp(punctuation, caseSensitive: false) : punctuation; + _suffixes = + suffixes is String ? RegExp(suffixes, caseSensitive: false) : suffixes; + _particles = particles is String + ? RegExp(particles, caseSensitive: false) + : particles; + _punctuation = punctuation is String + ? RegExp(punctuation, caseSensitive: false) + : punctuation; } /// Static basic constructor for [NameParser] that restricts the output of [parse] to be a @@ -153,7 +162,8 @@ class NameParser { .replaceAll(_punctuation, '') .contains(_suffixes)) { List lastParts = nonSuffixParts.removeLast().split(_whitespace); - while (lastParts.last.replaceAll(_punctuation, '').contains(_suffixes)) { + while ( + lastParts.last.replaceAll(_punctuation, '').contains(_suffixes)) { suffixParts.addFirst(lastParts.removeLast()); } nonSuffixParts.add(lastParts.join(' ')); @@ -199,7 +209,8 @@ class NameParser { // dropping. DoubleLinkedQueue nonDroppingParticleParts = DoubleLinkedQueue(); DoubleLinkedQueue droppingParticleParts = DoubleLinkedQueue(); - while (spaceParts.isNotEmpty && spacePartsNoPunctuation.last.contains(_particles)) { + while (spaceParts.isNotEmpty && + spacePartsNoPunctuation.last.contains(_particles)) { var particle = spacePartsNoPunctuation.removeLast(); if (particle.toLowerCase() != particle) { nonDroppingParticleParts.addFirst(spaceParts.removeLast()); diff --git a/lib/src/date/parsed_date.dart b/lib/src/date/parsed_date.dart index a50f895..73cb4b8 100644 --- a/lib/src/date/parsed_date.dart +++ b/lib/src/date/parsed_date.dart @@ -32,7 +32,10 @@ class ParsedDate { /// Evaluate if the [other] object is the same as this, by type and field equality. @override bool operator ==(other) => - other is ParsedDate && year == other.year && month == other.month && day == other.day; + other is ParsedDate && + year == other.year && + month == other.month && + day == other.day; /// Alias [toString] to [diagnosticString], forcing default parameters. @override diff --git a/lib/src/name/parsed_name.dart b/lib/src/name/parsed_name.dart index c76e84f..8de59b8 100644 --- a/lib/src/name/parsed_name.dart +++ b/lib/src/name/parsed_name.dart @@ -20,7 +20,10 @@ class ParsedName { /// Constructor for [ParsedName], where any null parameters will have the empty string used in /// their place. ParsedName(String family, - {String given, String droppingParticle, String nonDroppingParticle, String suffix}) { + {String given, + String droppingParticle, + String nonDroppingParticle, + String suffix}) { _family = family ?? ''; _given = given ?? ''; _droppingParticle = droppingParticle ?? ''; @@ -31,7 +34,10 @@ class ParsedName { /// Static method constructor for [ParsedName] so that this class can be constructed via /// reference. static ParsedName constantConstructor(String family, - {String given, String droppingParticle, String nonDroppingParticle, String suffix}) => + {String given, + String droppingParticle, + String nonDroppingParticle, + String suffix}) => ParsedName(family, given: given, droppingParticle: droppingParticle, @@ -76,9 +82,11 @@ class ParsedName { [ if (given.isNotEmpty && givenLabel.isNotEmpty) givenLabel, if (given.isNotEmpty) given, - if (droppingParticle.isNotEmpty && droppingParticleLabel.isNotEmpty) droppingParticleLabel, + if (droppingParticle.isNotEmpty && droppingParticleLabel.isNotEmpty) + droppingParticleLabel, if (droppingParticle.isNotEmpty) droppingParticle, - if (nonDroppingParticle.isNotEmpty && nonDroppingParticleLabel.isNotEmpty) + if (nonDroppingParticle.isNotEmpty && + nonDroppingParticleLabel.isNotEmpty) nonDroppingParticleLabel, if (nonDroppingParticle.isNotEmpty) nonDroppingParticle, if (family.isNotEmpty && familyLabel.isNotEmpty) familyLabel, diff --git a/test/date_test.dart b/test/date_test.dart index 325a37e..9a2c867 100644 --- a/test/date_test.dart +++ b/test/date_test.dart @@ -8,20 +8,29 @@ main() { group('NameParser', () { group('.parse(String input)', () { test('handles singular compact dates with default settings', () { - expect(DateParser.basic().parse('1/2/2000'), equals([ParsedDate(2000, 1, 2)])); - expect(DateParser.basic().parse('12/31/1999'), equals([ParsedDate(1999, 12, 31)])); + expect(DateParser.basic().parse('1/2/2000'), + equals([ParsedDate(2000, 1, 2)])); + expect(DateParser.basic().parse('12/31/1999'), + equals([ParsedDate(1999, 12, 31)])); }); test('handles multiple compact dates with the default settings', () { expect(DateParser.basic().parse('10-30-2010 - 11-15-2010'), equals([ParsedDate(2010, 10, 30), ParsedDate(2010, 11, 15)])); - expect(DateParser.basic().parse('1/3/1803-2/4/2020-10/3/2400'), - equals([ParsedDate(1803, 1, 3), ParsedDate(2020, 2, 4), ParsedDate(2400, 10, 3)])); + expect( + DateParser.basic().parse('1/3/1803-2/4/2020-10/3/2400'), + equals([ + ParsedDate(1803, 1, 3), + ParsedDate(2020, 2, 4), + ParsedDate(2400, 10, 3) + ])); }); test('handles singular normal dates', () { - expect(DateParser.basic().parse('2 January 2010'), equals([ParsedDate(2010, 1, 2)])); - expect(DateParser.basic().parse('Jul 2nd, 2005'), equals([ParsedDate(2005, 7, 2)])); + expect(DateParser.basic().parse('2 January 2010'), + equals([ParsedDate(2010, 1, 2)])); + expect(DateParser.basic().parse('Jul 2nd, 2005'), + equals([ParsedDate(2005, 7, 2)])); }); test('handles multiple normal dates', () { @@ -32,24 +41,35 @@ main() { expect(DateParser.basic().parse('October 1st, 2010-2015'), equals([ParsedDate(2010, 10, 1), ParsedDate(2015, 10, 1)])); expect( - DateParser.basic().parse('From the 1st of January, 1999, to the 2nd of March, 2000, ' + DateParser.basic().parse( + 'From the 1st of January, 1999, to the 2nd of March, 2000, ' 'until some time in 2001'), - equals([ParsedDate(1999, 1, 1), ParsedDate(2000, 3, 2), ParsedDate(2001, 3, 2)])); + equals([ + ParsedDate(1999, 1, 1), + ParsedDate(2000, 3, 2), + ParsedDate(2001, 3, 2) + ])); }); test('creates output using as much info as it has', () { expect(DateParser.basic().parse('2010'), equals([ParsedDate(2010)])); - expect(DateParser.basic().parse('1900-2000'), equals([ParsedDate(1900), ParsedDate(2000)])); - expect(DateParser.basic().parse('January 2010'), equals([ParsedDate(2010, 1)])); + expect(DateParser.basic().parse('1900-2000'), + equals([ParsedDate(1900), ParsedDate(2000)])); + expect(DateParser.basic().parse('January 2010'), + equals([ParsedDate(2010, 1)])); expect(DateParser.basic().parse('October to December, 2019'), equals([ParsedDate(2019, 10), ParsedDate(2019, 12)])); }); test('has default handling of seasons', () { - expect(DateParser.basic().parse('Spring 2000'), equals([ParsedDate(2000, 3)])); - expect(DateParser.basic().parse('Summer 2000'), equals([ParsedDate(2000, 6)])); - expect(DateParser.basic().parse('Fall 2000'), equals([ParsedDate(2000, 9)])); - expect(DateParser.basic().parse('Winter 2000'), equals([ParsedDate(2000, 12)])); + expect(DateParser.basic().parse('Spring 2000'), + equals([ParsedDate(2000, 3)])); + expect(DateParser.basic().parse('Summer 2000'), + equals([ParsedDate(2000, 6)])); + expect(DateParser.basic().parse('Fall 2000'), + equals([ParsedDate(2000, 9)])); + expect(DateParser.basic().parse('Winter 2000'), + equals([ParsedDate(2000, 12)])); }); test('returns an empty list if the input is null or empty', () { @@ -59,16 +79,24 @@ main() { test('returns an empty list if the input cannot be parsed', () { final samples = ['garbage', 'january', '10', 'summer 10']; - expect( - samples.map((s) => DateParser.basic().parse(s)), everyElement(equals([]))); + expect(samples.map((s) => DateParser.basic().parse(s)), + everyElement(equals([]))); }); - test('adds and removes digits as needed to standardize the numerical representation', () { - expect(DateParser.basic().parse('0001/00002/90'), equals([ParsedDate(1990, 1, 2)])); + test( + 'adds and removes digits as needed to standardize the numerical representation', + () { + expect(DateParser.basic().parse('0001/00002/90'), + equals([ParsedDate(1990, 1, 2)])); expect(DateParser.basic().parse('0002-0003 January 2019'), equals([ParsedDate(2019, 1, 2), ParsedDate(2019, 1, 3)])); - expect(DateParser.basic().parse('1/1/1 to 10/10/10 to 11/11/11'), - equals([ParsedDate(2001, 1, 1), ParsedDate(2010, 10, 10), ParsedDate(2011, 11, 11)])); + expect( + DateParser.basic().parse('1/1/1 to 10/10/10 to 11/11/11'), + equals([ + ParsedDate(2001, 1, 1), + ParsedDate(2010, 10, 10), + ParsedDate(2011, 11, 11) + ])); }); test('can have customized month parsing', () { @@ -89,57 +117,89 @@ main() { final Map customSeasonToMonth = Map.from(DateParser.defaultSeasonToMonthApproximations); customSeasonToMonth[1] = 21; - expect(DateParser.basic(seasonToMonth: customSeasonToMonth).parse('spring 2020'), + expect( + DateParser.basic(seasonToMonth: customSeasonToMonth) + .parse('spring 2020'), equals([ParsedDate(2020, 21)])); }); test('ignores seasons if it cannot translate them', () { - expect(DateParser.basic(seasons: null).parse('spring 2019').first.month, isNull); + expect(DateParser.basic(seasons: null).parse('spring 2019').first.month, + isNull); }); test('ignores seasons if it cannot encode them as months', () { - expect(DateParser.basic(seasonToMonth: null).parse('spring 2019').first.month, isNull); + expect( + DateParser.basic(seasonToMonth: null) + .parse('spring 2019') + .first + .month, + isNull); }); test('can have custom digit suffixes', () { - expect(DateParser.basic(digitSuffixes: 'foo').parse('1foo january 2000'), + expect( + DateParser.basic(digitSuffixes: 'foo').parse('1foo january 2000'), equals([ParsedDate(2000, 1, 1)])); }); test('can ignore digit suffixes by setting the pattern to null', () { - expect(DateParser.basic(digitSuffixes: null).parse('1st jan 2000').first.day, isNull); + expect( + DateParser.basic(digitSuffixes: null) + .parse('1st jan 2000') + .first + .day, + isNull); }); test('can have custom expansion of years to four digits', () { final Map customToFourDigits = {50: 2000, 100: 1900}; - expect(DateParser.basic(fourDigitOffsets: customToFourDigits).parse('10/15/45').first.year, + expect( + DateParser.basic(fourDigitOffsets: customToFourDigits) + .parse('10/15/45') + .first + .year, equals(2045)); }); test( 'can have expansion of years to four digits disabled by setting the parameter to ' 'null', () { - expect(DateParser.basic(fourDigitOffsets: null).parse('10/15/45').first.year, equals(45)); + expect( + DateParser.basic(fourDigitOffsets: null) + .parse('10/15/45') + .first + .year, + equals(45)); }); test('can interpret compact dates as being day-first', () { - expect(DateParser.basic(compactDateFormat: CompactDateFormat.dayFirst).parse('11/12/13'), + expect( + DateParser.basic(compactDateFormat: CompactDateFormat.dayFirst) + .parse('11/12/13'), equals([ParsedDate(2013, 12, 11)])); }); test('can interpret compact dates as being year-first', () { - expect(DateParser.basic(compactDateFormat: CompactDateFormat.yearFirst).parse('11/12/13'), + expect( + DateParser.basic(compactDateFormat: CompactDateFormat.yearFirst) + .parse('11/12/13'), equals([ParsedDate(2011, 12, 13)])); }); test('overrides year-first if the year is obviously last', () { - expect(DateParser.basic(compactDateFormat: CompactDateFormat.yearFirst).parse('11/12/2013'), + expect( + DateParser.basic(compactDateFormat: CompactDateFormat.yearFirst) + .parse('11/12/2013'), equals([ParsedDate(2013, 12, 11)])); }); test('overrides day/month-first if the year is obviously first', () { - expect(DateParser.basic().parse('2011/12/13'), equals([ParsedDate(2011, 12, 13)])); - expect(DateParser.basic(compactDateFormat: CompactDateFormat.dayFirst).parse('2011/12/13'), + expect(DateParser.basic().parse('2011/12/13'), + equals([ParsedDate(2011, 12, 13)])); + expect( + DateParser.basic(compactDateFormat: CompactDateFormat.dayFirst) + .parse('2011/12/13'), equals([ParsedDate(2011, 12, 13)])); }); @@ -152,7 +212,8 @@ main() { }); test('can output in custom formats', () { - DateParserOutput customOutput = (int year, [int month, int day]) => year; + DateParserOutput customOutput = + (int year, [int month, int day]) => year; expect(DateParser(customOutput).parse('March 2004'), equals([2004])); }); }); @@ -198,7 +259,8 @@ main() { test('is the same as diagnosticString', () { final r = Random(); for (int i = 0; i < 3; i++) { - var result = ParsedDate(r.nextInt(2500), r.nextInt(12), r.nextInt(28)); + var result = + ParsedDate(r.nextInt(2500), r.nextInt(12), r.nextInt(28)); expect(result.toString(), result.diagnosticString()); } }); @@ -207,14 +269,16 @@ main() { group('diagnosticString', () { test('only includes values when it should', () { expect(ParsedDate(2000).diagnosticString(), '[Year]: 2000'); - expect(ParsedDate(2000, 10).diagnosticString(), '[Month]: 10 [Year]: 2000'); - expect(ParsedDate(2000, 10, 10).diagnosticString(), '[Day]: 10 [Month]: 10 [Year]: 2000'); + expect(ParsedDate(2000, 10).diagnosticString(), + '[Month]: 10 [Year]: 2000'); + expect(ParsedDate(2000, 10, 10).diagnosticString(), + '[Day]: 10 [Month]: 10 [Year]: 2000'); }); test('works with optional parameters', () { expect( - ParsedDate(1, 1, 1) - .diagnosticString(separator: '', dayLabel: '', monthLabel: '.', yearLabel: '.'), + ParsedDate(1, 1, 1).diagnosticString( + separator: '', dayLabel: '', monthLabel: '.', yearLabel: '.'), '1.1.1'); }); }); diff --git a/test/name_test.dart b/test/name_test.dart index f15cde6..91e3b7b 100644 --- a/test/name_test.dart +++ b/test/name_test.dart @@ -44,7 +44,9 @@ main() { expect(result.family, 'Warren'); }); - test('rotates the first comma-separated portion to the end to handle , ', () { + test( + 'rotates the first comma-separated portion to the end to handle , ', + () { var result = NameParser.basic().parse('Warren, Jack Ramsey'); expect(result.given, 'Jack Ramsey'); expect(result.family, 'Warren'); @@ -68,7 +70,8 @@ main() { expect(result.suffix, isEmpty); }); - test('handles particles properly with partial , ordering', () { + test('handles particles properly with partial , ordering', + () { var result = NameParser.basic().parse('La Fontaine, Jean de'); expect(result.given, 'Jean'); expect(result.family, 'Fontaine'); @@ -77,7 +80,8 @@ main() { expect(result.suffix, isEmpty); }); - test('handles particles properly with partial , ordering', () { + test('handles particles properly with partial , ordering', + () { var result = NameParser.basic().parse('La Fontaine, Jean de'); expect(result.given, 'Jean'); expect(result.family, 'Fontaine'); @@ -86,7 +90,8 @@ main() { expect(result.suffix, isEmpty); }); - test('handles particles properly with partial , ordering with an odd comma', + test( + 'handles particles properly with partial , ordering with an odd comma', () { var result = NameParser.basic().parse('La Fontaine, Jean, de'); expect(result.given, 'Jean'); @@ -96,7 +101,8 @@ main() { expect(result.suffix, isEmpty); }); - test('handles particles properly with minimal , ordering', () { + test('handles particles properly with minimal , ordering', + () { var result = NameParser.basic().parse('Fontaine, Jean de La'); expect(result.given, 'Jean'); expect(result.family, 'Fontaine'); @@ -129,7 +135,8 @@ main() { expect(result.nonDroppingParticle, 'De'); }); - test('handles uppercase particles as non-dropping in , ', () { + test('handles uppercase particles as non-dropping in , ', + () { var result = NameParser.basic().parse('De Kooning, Willem'); expect(result.given, 'Willem'); expect(result.family, 'Kooning'); @@ -151,35 +158,43 @@ main() { expect(result.suffix, 'II'); }); - test('handles suffixes in , when the suffix follows last', () { + test('handles suffixes in , when the suffix follows last', + () { var result = NameParser.basic().parse('Mary II, Elizabeth Alexandra'); expect(result.given, 'Elizabeth Alexandra'); expect(result.family, 'Mary'); expect(result.suffix, 'II'); }); - test('handles suffixes in , when the suffix follows last with a comma', () { + test( + 'handles suffixes in , when the suffix follows last with a comma', + () { var result = NameParser.basic().parse('Mary, II, Elizabeth Alexandra'); expect(result.given, 'Elizabeth Alexandra'); expect(result.family, 'Mary'); expect(result.suffix, 'II'); }); - test('handles suffixes in , when the suffix is at the end', () { + test('handles suffixes in , when the suffix is at the end', + () { var result = NameParser.basic().parse('Mary, Elizabeth Alexandra II'); expect(result.given, 'Elizabeth Alexandra'); expect(result.family, 'Mary'); expect(result.suffix, 'II'); }); - test('handles suffixes in , when the suffix is at the end with a comma', () { + test( + 'handles suffixes in , when the suffix is at the end with a comma', + () { var result = NameParser.basic().parse('Mary, Elizabeth Alexandra, II'); expect(result.given, 'Elizabeth Alexandra'); expect(result.family, 'Mary'); expect(result.suffix, 'II'); }); - test('handles all-caps names as expected, with particles being non-dropping', () { + test( + 'handles all-caps names as expected, with particles being non-dropping', + () { var result = NameParser.basic().parse('WILLEM DE KOONING'); expect(result.given, 'WILLEM'); expect(result.family, 'KOONING'); @@ -253,8 +268,8 @@ main() { group('diagonisticString()', () { test('fills its default parameters', () { - var result = - NameParser.basic().parse('given1 given2 de van Di La family1 family2 Jr. III PhD.'); + var result = NameParser.basic() + .parse('given1 given2 de van Di La family1 family2 Jr. III PhD.'); expect( result.diagnosticString(), '[Given]: given1 given2 [Dropping Particle]: de van [Non-dropping Particle]: Di La ' @@ -262,8 +277,8 @@ main() { }); test('can have customized labels/separators', () { - var result = - NameParser.basic().parse('given1 given2 de van Di La family1 family2 Jr. III PhD.'); + var result = NameParser.basic() + .parse('given1 given2 de van Di La family1 family2 Jr. III PhD.'); expect( result.diagnosticString( separator: '-',