Skip to content
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

Adding "radix" to toInt and toIntOrNUll #43

Merged
merged 3 commits into from Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/src/range.dart
Expand Up @@ -60,9 +60,7 @@ class IntRange extends IterableBase<int> {

@override
Iterator<int> get iterator => _IntRangeIterator(_first, _last, stepSize);
}
shinayser marked this conversation as resolved.
Show resolved Hide resolved

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);
Expand Down
18 changes: 16 additions & 2 deletions lib/src/string.dart
Expand Up @@ -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;

Expand Down
19 changes: 11 additions & 8 deletions test/string_test.dart
Expand Up @@ -79,6 +79,12 @@ void main() {
expect('123456789'.toInt(), 123456789);
});

test('.toInt(radix)', () {
shinayser marked this conversation as resolved.
Show resolved Hide resolved
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);
Expand Down Expand Up @@ -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,
simc marked this conversation as resolved.
Show resolved Hide resolved
'57edf4a22be3c955ac49da2e2107b67a');
});
});
}