Skip to content

Commit

Permalink
Organize imports and fix trailing commas
Browse files Browse the repository at this point in the history
  • Loading branch information
minhqdao committed Oct 7, 2021
1 parent a32a0c4 commit 7e5f5ed
Show file tree
Hide file tree
Showing 20 changed files with 265 additions and 151 deletions.
6 changes: 3 additions & 3 deletions lib/dartx.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import 'package:characters/characters.dart' as characters;
import 'package:collection/collection.dart' as collection;
import 'package:crypto/crypto.dart' as crypto;

export 'package:time/time.dart';
export 'package:characters/characters.dart';
export 'package:time/time.dart';

part 'src/comparable.dart';
part 'src/comparator.dart';
part 'src/function.dart';
part 'src/iterable_num.dart';
part 'src/iterable.dart';
part 'src/iterable_num.dart';
part 'src/list.dart';
part 'src/map.dart';
part 'src/num.dart';
part 'src/range.dart';
part 'src/sorted_list.dart';
part 'src/string.dart';
part 'src/range.dart';
6 changes: 4 additions & 2 deletions lib/src/comparable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ extension ComparableCoerceInExtension<T extends Comparable<T>> on T {
/// or [maximumValue] if this value is greater than [maximumValue].
T coerceIn(T minimumValue, [T? maximumValue]) {
if (maximumValue != null && minimumValue > maximumValue) {
throw ArgumentError('Cannot coerce value to an empty range: '
'maximum $maximumValue is less than minimum $minimumValue.');
throw ArgumentError(
'Cannot coerce value to an empty range: '
'maximum $maximumValue is less than minimum $minimumValue.',
);
}
if (this < minimumValue) return minimumValue;
if (maximumValue != null && this > maximumValue) return maximumValue;
Expand Down
32 changes: 20 additions & 12 deletions lib/src/io/directory.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
part of dartx_io;

extension DirectorySubDirExtension on Directory {
Directory subdir(String part1,
[String? part2,
String? part3,
String? part4,
String? part5,
String? part6,
String? part7]) {
Directory subdir(
String part1, [
String? part2,
String? part3,
String? part4,
String? part5,
String? part6,
String? part7,
]) {
return Directory(
path_helper.join(path, part1, part2, part3, part4, part5, part6, part7),
);
Expand Down Expand Up @@ -35,7 +37,9 @@ extension DirectoryCopyRecursivelyExtension on Directory {
await target.create(recursive: true);
await for (final file in list(recursive: true)) {
final copyTo = path_helper.join(
target.path, path_helper.relative(file.path, from: path));
target.path,
path_helper.relative(file.path, from: path),
);
if (file is Directory) {
await Directory(copyTo).create(recursive: true);
} else if (file is File) {
Expand All @@ -56,11 +60,14 @@ extension DirectoryContainsExtension on Directory {
/// Returns a [Future<bool>] holding the value.
///
/// For the sync method, see [containsSync()].
Future<bool> contains(FileSystemEntity entity,
{bool recursive = false}) async {
Future<bool> contains(
FileSystemEntity entity, {
bool recursive = false,
}) async {
final entities = list(recursive: recursive);
return entities.any(
(element) => FileSystemEntity.identicalSync(entity.path, element.path));
(element) => FileSystemEntity.identicalSync(entity.path, element.path),
);
}
}

Expand All @@ -76,7 +83,8 @@ extension DirectoryContainsSyncExtension on Directory {
bool containsSync(FileSystemEntity entity, {bool recursive = false}) {
final entities = listSync(recursive: recursive);
return entities.any(
(element) => FileSystemEntity.identicalSync(entity.path, element.path));
(element) => FileSystemEntity.identicalSync(entity.path, element.path),
);
}
}

Expand Down
4 changes: 3 additions & 1 deletion lib/src/io/file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ extension FileForEachBlockExtension on File {
///
/// You can use this function for huge files.
Future<void> forEachBlock(
int blockSize, void Function(Uint8List buffer) action) async {
int blockSize,
void Function(Uint8List buffer) action,
) async {
final raf = await open();
// ignore: literal_only_boolean_expressions
while (true) {
Expand Down
22 changes: 16 additions & 6 deletions lib/src/iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,9 @@ extension IterableFilterIndexedTo<E> on Iterable<E> {
/// Appends all elements matching the given [predicate] to the given
/// [destination].
void filterIndexedTo(
List<E> destination, bool Function(E element, int index) predicate) =>
List<E> destination,
bool Function(E element, int index) predicate,
) =>
whereIndexedTo(destination, predicate);
}

Expand All @@ -627,7 +629,9 @@ extension IterableFilterNotToIndexed<E> on Iterable<E> {
/// Appends all elements not matching the given [predicate] to the given
/// [destination].
void filterNotToIndexed(
List<E> destination, bool Function(E element, int index) predicate) =>
List<E> destination,
bool Function(E element, int index) predicate,
) =>
whereNotToIndexed(destination, predicate);
}

Expand All @@ -639,7 +643,8 @@ extension IterableFilterNotNull<E> on Iterable<E?> {
extension IterableWhereIndexed<E> on Iterable<E> {
/// Returns all elements that satisfy the given [predicate].
Iterable<E> whereIndexed(
bool Function(E element, int index) predicate) sync* {
bool Function(E element, int index) predicate,
) sync* {
var index = 0;
for (final element in this) {
if (predicate(element, index++)) {
Expand All @@ -665,7 +670,9 @@ extension IterableWhereIndexedTo<E> on Iterable<E> {
/// Appends all elements matching the given [predicate] to the given
/// [destination].
void whereIndexedTo(
List<E> destination, bool Function(E element, int index) predicate) {
List<E> destination,
bool Function(E element, int index) predicate,
) {
var index = 0;
for (final element in this) {
if (predicate(element, index++)) {
Expand All @@ -689,7 +696,8 @@ extension IterableWhereNot<E> on Iterable<E> {
extension IterableWhereNotIndexed<E> on Iterable<E> {
/// Returns all elements not matching the given [predicate].
Iterable<E> whereNotIndexed(
bool Function(E element, int index) predicate) sync* {
bool Function(E element, int index) predicate,
) sync* {
var index = 0;
for (final element in this) {
if (!predicate(element, index++)) {
Expand All @@ -715,7 +723,9 @@ extension IterableWhereNotToIndexed<E> on Iterable<E> {
/// Appends all elements not matching the given [predicate] to the given
/// [destination].
void whereNotToIndexed(
List<E> destination, bool Function(E element, int index) predicate) {
List<E> destination,
bool Function(E element, int index) predicate,
) {
var index = 0;
for (final element in this) {
if (!predicate(element, index++)) {
Expand Down
8 changes: 5 additions & 3 deletions lib/src/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ extension MapMapValues<K, V> on Map<K, V> {
extension MapMaxBy<K, V> on Map<K, V> {
/// Returns the first entry yielding the largest value of the given function or `null` if there are no entries.
MapEntry<K, V>? maxBy<R extends Comparable>(
R Function(MapEntry<K, V>) selector) {
R Function(MapEntry<K, V>) selector,
) {
final i = entries.iterator;
if (!i.moveNext()) return null;
MapEntry<K, V> maxElement = i.current;
Expand Down Expand Up @@ -192,7 +193,8 @@ extension MapMaxWith<K, V> on Map<K, V> {
extension MapMinBy<K, V> on Map<K, V> {
/// Returns the first entry yielding the smallest value of the given function or `null` if there are no entries.
MapEntry<K, V>? minBy<R extends Comparable>(
R Function(MapEntry<K, V>) selector) {
R Function(MapEntry<K, V>) selector,
) {
final i = entries.iterator;
if (!i.moveNext()) return null;
MapEntry<K, V> minElement = i.current;
Expand Down Expand Up @@ -278,7 +280,7 @@ class Pair<A, B> {
final B second;

@override
String toString() => "($first, $second)";
String toString() => '($first, $second)';

@override
bool operator ==(Object other) =>
Expand Down
6 changes: 4 additions & 2 deletions lib/src/num.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ extension NumCoerceInExtension<T extends num> on T {
/// ````
T coerceIn(T minimumValue, [T? maximumValue]) {
if (maximumValue != null && minimumValue > maximumValue) {
throw ArgumentError('Cannot coerce value to an empty range: '
'maximum $maximumValue is less than minimum $minimumValue.');
throw ArgumentError(
'Cannot coerce value to an empty range: '
'maximum $maximumValue is less than minimum $minimumValue.',
);
}
if (this < minimumValue) return minimumValue;
if (maximumValue != null && this > maximumValue) return maximumValue;
Expand Down
14 changes: 10 additions & 4 deletions lib/src/sorted_list.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
part of dartx;

Comparator<E> _getComparator<E>(
int order, Comparable Function(E element) selector,
{Comparator<E>? parent}) {
int order,
Comparable Function(E element) selector, {
Comparator<E>? parent,
}) {
int newComparator(E a, E b) {
return order * selector(a).compareTo(selector(b));
}
Expand Down Expand Up @@ -190,8 +192,12 @@ abstract class _DelegatingList<E> extends _DelegatingIterable<E>
delegate.setAll(index, iterable);

@override
void setRange(int start, int end, Iterable<E> iterable,
[int skipCount = 0]) =>
void setRange(
int start,
int end,
Iterable<E> iterable, [
int skipCount = 0,
]) =>
delegate.setRange(start, end, iterable, skipCount);

@override
Expand Down
74 changes: 44 additions & 30 deletions test/comparable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,61 @@ class _WrappedInt implements Comparable<_WrappedInt> {
void main() {
group('ComparableX', () {
test('.coerceIn()', () {
expect(DateTime(1984, 11, 19).coerceIn(DateTime(1984, 11, 1)),
DateTime(1984, 11, 19));
expect(
DateTime(1984, 11, 19).coerceIn(
DateTime(1984, 11, 1),
DateTime(1984, 11, 20),
),
DateTime(1984, 11, 19));
DateTime(1984, 11, 19).coerceIn(DateTime(1984, 11, 1)),
DateTime(1984, 11, 19),
);
expect(
DateTime(1984, 10, 28).coerceIn(
DateTime(1984, 11, 1),
DateTime(1984, 11, 20),
),
DateTime(1984, 11, 1));
DateTime(1984, 11, 19).coerceIn(
DateTime(1984, 11, 1),
DateTime(1984, 11, 20),
),
DateTime(1984, 11, 19),
);
expect(
DateTime(1984, 12, 1).coerceIn(
DateTime(1984, 11, 1),
DateTime(1984, 11, 20),
),
DateTime(1984, 11, 20));
DateTime(1984, 10, 28).coerceIn(
DateTime(1984, 11, 1),
DateTime(1984, 11, 20),
),
DateTime(1984, 11, 1),
);
expect(
DateTime(1984, 12, 1).coerceIn(
DateTime(1984, 11, 1),
DateTime(1984, 11, 20),
),
DateTime(1984, 11, 20),
);
expect(() => 10.coerceIn(3, 2), throwsArgumentError);
expect(
() => DateTime.now().coerceIn(
DateTime(1984, 11, 20),
DateTime(1984, 11, 1),
),
throwsArgumentError);
() => DateTime.now().coerceIn(
DateTime(1984, 11, 20),
DateTime(1984, 11, 1),
),
throwsArgumentError,
);
});

test('.coerceAtLeast()', () {
expect(DateTime(1984, 11, 19).coerceAtLeast(DateTime(1984, 1, 1)),
DateTime(1984, 11, 19));
expect(DateTime(1984, 11, 19).coerceAtLeast(DateTime(1984, 11, 20)),
DateTime(1984, 11, 20));
expect(
DateTime(1984, 11, 19).coerceAtLeast(DateTime(1984, 1, 1)),
DateTime(1984, 11, 19),
);
expect(
DateTime(1984, 11, 19).coerceAtLeast(DateTime(1984, 11, 20)),
DateTime(1984, 11, 20),
);
});

test('.coerceAtMost()', () {
expect(DateTime(1984, 11, 19).coerceAtMost(DateTime(1984, 11, 20)),
DateTime(1984, 11, 19));
expect(DateTime(1984, 11, 19).coerceAtMost(DateTime(1984, 11, 1)),
DateTime(1984, 11, 1));
expect(
DateTime(1984, 11, 19).coerceAtMost(DateTime(1984, 11, 20)),
DateTime(1984, 11, 19),
);
expect(
DateTime(1984, 11, 19).coerceAtMost(DateTime(1984, 11, 1)),
DateTime(1984, 11, 1),
);
});

test('.between()', () {
Expand Down
2 changes: 1 addition & 1 deletion test/directory.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:io';

import 'package:dartx/dartx_io.dart';
import 'package:path/path.dart';
import 'package:test/test.dart';
import 'package:dartx/dartx_io.dart';

void main() {
group('Directory', () {
Expand Down
2 changes: 1 addition & 1 deletion test/file_system_entity.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:io';

import 'package:test/test.dart';
import 'package:dartx/dartx_io.dart';
import 'package:test/test.dart';

void main() {
group('FileSystemEntity', () {
Expand Down
2 changes: 1 addition & 1 deletion test/function_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ignore_for_file: prefer_function_declarations_over_variables
import 'package:test/test.dart';
import 'package:dartx/dartx.dart';
import 'package:test/test.dart';

void main() {
group('Function', () {
Expand Down
2 changes: 1 addition & 1 deletion test/iterable_num_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:test/test.dart';
import 'package:dartx/dartx.dart';
import 'package:test/test.dart';

void main() {
group('IterableNumX', () {
Expand Down
Loading

0 comments on commit 7e5f5ed

Please sign in to comment.