From fa79d29db504215e12b645904e2d17d16d47f9b9 Mon Sep 17 00:00:00 2001 From: Daniel Oliveira Date: Wed, 22 Jan 2020 16:46:03 -0300 Subject: [PATCH 1/3] Removing unnecessary extension. --- lib/src/range.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/src/range.dart b/lib/src/range.dart index 15dd366..c3d5c7b 100644 --- a/lib/src/range.dart +++ b/lib/src/range.dart @@ -60,9 +60,7 @@ class IntRange extends IterableBase { @override Iterator get iterator => _IntRangeIterator(_first, _last, stepSize); -} -extension IntRangeX on IntRange { /// Creates a [IntRange] with a different [stepSize], /// keeps first and last value IntRange step(int step) => IntRange(_first, _last, step: step); From e752a77bb3ebe0a03d8cd0c241d9a291fe8b9e5b Mon Sep 17 00:00:00 2001 From: Daniel Oliveira Date: Wed, 22 Jan 2020 17:13:35 -0300 Subject: [PATCH 2/3] Adding radix optional parameter to toInt and toIntOrNull. --- lib/src/string.dart | 18 ++++++++++++++++-- test/string_test.dart | 19 +++++++++++-------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/lib/src/string.dart b/lib/src/string.dart index 8c3b31b..7f0b5bd 100644 --- a/lib/src/string.dart +++ b/lib/src/string.dart @@ -96,11 +96,25 @@ extension StringX on String { bool get isInt => toIntOrNull() != null; /// Parses the string as an [int] number and returns the result. - int toInt() => int.parse(this); + /// + /// The [radix] must be in the range 2..36. The digits used are + /// first the decimal digits 0..9, and then the letters 'a'..'z' with + /// values 10 through 35. Also accepts upper-case letters with the same + /// values as the lower-case ones. + /// + /// If no [radix] is given then it defaults to 10. + int toInt({int radix}) => int.parse(this, radix: radix); /// Parses the string as an [int] number and returns the result or `null` if /// the string is not a valid representation of a number. - int toIntOrNull() => int.tryParse(this); + /// + /// The [radix] must be in the range 2..36. The digits used are + /// first the decimal digits 0..9, and then the letters 'a'..'z' with + /// values 10 through 35. Also accepts upper-case letters with the same + /// values as the lower-case ones. + /// + /// If no [radix] is given then it defaults to 10. + int toIntOrNull({int radix}) => int.tryParse(this, radix: radix); bool get isDouble => toDoubleOrNull() != null; diff --git a/test/string_test.dart b/test/string_test.dart index eb11d65..8a37648 100644 --- a/test/string_test.dart +++ b/test/string_test.dart @@ -79,6 +79,12 @@ void main() { expect('123456789'.toInt(), 123456789); }); + test('.toInt(radix)', () { + expect('100'.toInt(radix: 2), 4); + expect('100'.toInt(radix: 16), 256); + expect('FF'.toInt(radix: 16), 255); + }); + test('.toIntOrNull()', () { expect(''.toIntOrNull(), null); expect('a'.toIntOrNull(), null); @@ -129,18 +135,15 @@ void main() { expect('message digest'.md5, 'f96b697d7cb7938d525a2f31aaf161d0'); expect('เดโŒ›๏ค™ะ‘๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆ'.md5, 'c7834eff7c967101cfb65b8f6d15ad46'); expect( - 'abcdefghijklmnopqrstuvwxyz'.md5, - 'c3fcd3d76192e4007dfb496cca67e13b' - ); + 'abcdefghijklmnopqrstuvwxyz'.md5, 'c3fcd3d76192e4007dfb496cca67e13b'); expect( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.md5, - 'd174ab98d277d9f5a5611c2c9f419d9f' - ); + 'd174ab98d277d9f5a5611c2c9f419d9f'); expect( '12345678901234567890123456789012345678901234567890123456789012' - '345678901234567890'.md5, - '57edf4a22be3c955ac49da2e2107b67a' - ); + '345678901234567890' + .md5, + '57edf4a22be3c955ac49da2e2107b67a'); }); }); } From e5e87881af822645f4bbea267199d53b8a238fae Mon Sep 17 00:00:00 2001 From: Daniel Oliveira Date: Wed, 22 Jan 2020 18:39:50 -0300 Subject: [PATCH 3/3] Reverting IntRange modification (this belongs to another PR). Creating test group for toInt with/wthout radix. --- lib/src/range.dart | 2 ++ test/string_test.dart | 22 ++++++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/src/range.dart b/lib/src/range.dart index c3d5c7b..15dd366 100644 --- a/lib/src/range.dart +++ b/lib/src/range.dart @@ -60,7 +60,9 @@ class IntRange extends IterableBase { @override Iterator get iterator => _IntRangeIterator(_first, _last, stepSize); +} +extension IntRangeX on IntRange { /// Creates a [IntRange] with a different [stepSize], /// keeps first and last value IntRange step(int step) => IntRange(_first, _last, step: step); diff --git a/test/string_test.dart b/test/string_test.dart index 8a37648..7c8c54a 100644 --- a/test/string_test.dart +++ b/test/string_test.dart @@ -73,16 +73,18 @@ void main() { expect('123456789'.isInt, true); }); - test('.toInt()', () { - expect('0'.toInt(), 0); - expect('1'.toInt(), 1); - expect('123456789'.toInt(), 123456789); - }); - - test('.toInt(radix)', () { - expect('100'.toInt(radix: 2), 4); - expect('100'.toInt(radix: 16), 256); - expect('FF'.toInt(radix: 16), 255); + group('.toInt()', () { + test('with radix', () { + expect('100'.toInt(radix: 2), 4); + expect('100'.toInt(radix: 16), 256); + expect('FF'.toInt(radix: 16), 255); + }); + + test('without radix', () { + expect('0'.toInt(), 0); + expect('1'.toInt(), 1); + expect('123456789'.toInt(), 123456789); + }); }); test('.toIntOrNull()', () {