Skip to content

Commit

Permalink
Merge pull request #122 from leisim/package-collection-compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy authored Mar 14, 2021
2 parents ed7edf5 + 74025dd commit 1e6eecb
Show file tree
Hide file tree
Showing 13 changed files with 402 additions and 80 deletions.
4 changes: 2 additions & 2 deletions lib/dartx.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import 'dart:convert';
import 'dart:math';
import 'dart:typed_data';

import 'package:characters/characters.dart';
import 'package:collection/collection.dart' hide DelegatingList;
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';
Expand Down
21 changes: 20 additions & 1 deletion lib/src/comparable.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
part of dartx;

/// Provides comparison operators for [Comparable] types.
extension ComparableX<T extends Comparable<T>> on T {
extension ComparableSmallerExtension<T extends Comparable<T>> on T {
bool operator <(T other) => compareTo(other) < 0;
}

extension ComparableSmallerEqualsExtension<T extends Comparable<T>> on T {
bool operator <=(T other) => compareTo(other) <= 0;
}

extension ComparableBiggerExtension<T extends Comparable<T>> on T {
bool operator >(T other) => compareTo(other) > 0;
}

extension ComparableBiggerEqualsExtension<T extends Comparable<T>> on T {
bool operator >=(T other) => compareTo(other) >= 0;
}

extension ComparableCoerceInExtension<T extends Comparable<T>> on T {
/// Ensures that this value lies in the specified range
/// [minimumValue]..[maximumValue].
///
Expand All @@ -22,26 +33,34 @@ extension ComparableX<T extends Comparable<T>> on T {
if (maximumValue != null && this > maximumValue) return maximumValue;
return this;
}
}

extension ComparableCoerceAtLeastExtension<T extends Comparable<T>> on T {
/// Ensures that this value is not less than the specified [minimumValue].
///
/// @return this value if it's greater than or equal to the [minimumValue]
/// or the [minimumValue] otherwise.
T coerceAtLeast(T minimumValue) => this < minimumValue ? minimumValue : this;
}

extension ComparableCoerceAtMostExtension<T extends Comparable<T>> on T {
/// Ensures that this value is not greater than the specified [maximumValue].
///
/// @return this value if it's less than or equal to the [maximumValue]
/// or the [maximumValue] otherwise.
T coerceAtMost(T maximumValue) => this > maximumValue ? maximumValue : this;
}

extension ComparableBetweenExtension<T extends Comparable<T>> on T {
/// Returns true when between [first] and [endInclusive]. The order of the
/// arguments doesn't matter.
///
/// Alias for `first.rangeTo(endInclusive).contains(this)`
bool between(T first, T endInclusive) =>
first.rangeTo(endInclusive).contains(this);
}

extension ComparableInRangeExtension<T extends Comparable<T>> on T {
/// Returns true if in the [range].
bool inRange(Range<T> range) => range.contains(this);
}
4 changes: 3 additions & 1 deletion lib/src/comparator.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
part of dartx;

extension CompararatorX<T> on Comparator<T> {
extension CompararatorComposeExtensions<T> on Comparator<T> {
/// return a new comparator,
/// that sorts the items first by the criteria of this comparator,
/// then by the criteria of the given comparator
Expand All @@ -13,7 +13,9 @@ extension CompararatorX<T> on Comparator<T> {
return then(a, b);
};
}
}

extension CompararatorReverseExtensions<T> on Comparator<T> {
/// reverse the sort order of this comparator
Comparator<T> reverse() => (a, b) => this(b, a);
}
54 changes: 43 additions & 11 deletions lib/src/function.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,82 +6,114 @@ typedef Function2<A, B, R> = R Function(A a, B b);
typedef Function3<A, B, C, R> = R Function(A a, B b, C c);
typedef Function4<A, B, C, D, R> = R Function(A a, B b, C c, D d);

extension Function0X<R> on Function0<R> {
extension Function0InvokeExtension<R> on Function0<R> {
/// Invokes this function and returns it's return value.
@Deprecated('Use `call()`')
R invoke() => this();
}

extension Function1X<A, R> on Function1<A, R> {
extension Function1InvokeExtensions<A, R> on Function1<A, R> {
/// Invokes this function and returns it's return value.
R invoke(A first) => this(first);
}

extension Function1PartialExtensions<A, R> on Function1<A, R> {
Function0<R> partial(A first) => () => this(first);
}

extension Function2X<A, B, R> on Function2<A, B, R> {
extension Function2InvokeExtension<A, B, R> on Function2<A, B, R> {
/// Invokes this function and returns it's return value.
R invoke(A first, B second) => this(first, second);
}

Function1<A, Function1<B, R>> curry() =>
(A first) => (B second) => this(first, second);

extension Function2PartialExtension<A, B, R> on Function2<A, B, R> {
Function1<B, R> partial(A first) => (B second) => this(first, second);
}

extension Function2Partial2Extension<A, B, R> on Function2<A, B, R> {
Function0<R> partial2(A first, B second) => () => this(first, second);
}

extension Function2FlipExtension<A, B, R> on Function2<A, B, R> {
Function2<B, A, R> flip() => (B second, A first) => this(first, second);
}

extension Curry2X<A, B, R> on Function1<A, Function1<B, R>> {
extension Function2CurryExtension<A, B, R> on Function2<A, B, R> {
Function1<A, Function1<B, R>> curry() =>
(A first) => (B second) => this(first, second);
}

extension Function2UncurryExtension<A, B, R> on Function1<A, Function1<B, R>> {
Function2<A, B, R> uncurry() => (A first, B second) => this(first)(second);
}

extension Function3X<R, A, B, C> on Function3<A, B, C, R> {
extension Function3InvokeExtension<R, A, B, C> on Function3<A, B, C, R> {
/// Invokes this function and returns it's return value.
R invoke(A first, B second, C third) => this(first, second, third);
}

extension Function3CurryExtension<R, A, B, C> on Function3<A, B, C, R> {
Function1<A, Function1<B, Function1<C, R>>> curry() =>
(A first) => (B second) => (C third) => this(first, second, third);
}

extension Function3PartialExtension<R, A, B, C> on Function3<A, B, C, R> {
Function2<B, C, R> partial(A first) =>
(B second, C third) => this(first, second, third);
}

extension Function3Partial2Extension<R, A, B, C> on Function3<A, B, C, R> {
Function1<C, R> partial2(A first, B second) =>
(C third) => this(first, second, third);
}

extension Function3Partial3Extension<R, A, B, C> on Function3<A, B, C, R> {
Function0<R> partial3(A first, B second, C third) =>
() => this(first, second, third);
}

extension Curry3X<A, B, C, R> on Function1<A, Function1<B, Function1<C, R>>> {
extension Function3UncurryExtension<A, B, C, R>
on Function1<A, Function1<B, Function1<C, R>>> {
Function3<A, B, C, R> uncurry() =>
(A first, B second, C third) => this(first)(second)(third);
}

extension Function4X<R, A, B, C, D> on Function4<A, B, C, D, R> {
extension Function4InvokeExtension<R, A, B, C, D> on Function4<A, B, C, D, R> {
/// Invokes this function and returns it's return value.
R invoke(A first, B second, C third, D fourth) =>
this(first, second, third, fourth);
}

extension Function4CurryExtension<R, A, B, C, D> on Function4<A, B, C, D, R> {
Function1<A, Function1<B, Function1<C, Function1<D, R>>>> curry() =>
(A first) => (B second) =>
(C third) => (D fourth) => this(first, second, third, fourth);
}

extension Function4PartialExtension<R, A, B, C, D> on Function4<A, B, C, D, R> {
Function3<B, C, D, R> partial(A first) =>
(B second, C third, D fourth) => this(first, second, third, fourth);
}

extension Function4Partial2Extension<R, A, B, C, D>
on Function4<A, B, C, D, R> {
Function2<C, D, R> partial2(A first, B second) =>
(C third, D fourth) => this(first, second, third, fourth);
}

extension Function4Partial3Extension<R, A, B, C, D>
on Function4<A, B, C, D, R> {
Function1<D, R> partial3(A first, B second, C third) =>
(D fourth) => this(first, second, third, fourth);
}

extension Function4Partial4Extension<R, A, B, C, D>
on Function4<A, B, C, D, R> {
Function0<R> partial4(A first, B second, C third, D fourth) =>
() => this(first, second, third, fourth);
}

extension Curry4X<A, B, C, D, R>
extension Function4UncurryExtension<A, B, C, D, R>
on Function1<A, Function1<B, Function1<C, Function1<D, R>>>> {
Function4<A, B, C, D, R> uncurry() =>
(A first, B second, C third, D fourth) =>
Expand Down
8 changes: 7 additions & 1 deletion lib/src/io/directory.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
part of dartx_io;

extension DirectoryX on Directory {
extension DirectorySubDirExtension on Directory {
Directory subdir(String part1,
[String? part2,
String? part3,
Expand All @@ -12,7 +12,9 @@ extension DirectoryX on Directory {
path_helper.join(path, part1, part2, part3, part4, part5, part6, part7),
);
}
}

extension DirectoryCopyRecursivelyExtension on Directory {
/// Copies all of the files in this directory to [target].
///
/// This is similar to `cp -R <from> <to>`:
Expand Down Expand Up @@ -43,7 +45,9 @@ extension DirectoryX on Directory {
}
}
}
}

extension DirectoryContainsExtension on Directory {
/// Checks if this directory contains the [entity].
///
/// The [entity] can be a [File] or a [Directory].
Expand All @@ -58,7 +62,9 @@ extension DirectoryX on Directory {
return entities.any(
(element) => FileSystemEntity.identicalSync(entity.path, element.path));
}
}

extension DirectoryContainsSyncExtension on Directory {
/// Checks if this directory contains the [entity].
///
/// The [entity] can be a [File] or a [Directory].
Expand Down
7 changes: 6 additions & 1 deletion lib/src/io/file.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
part of dartx_io;

extension FileX on File {
extension FileAppendBytesExtension on File {
/// Appends an array of [bytes] to the content of this file.
Future<void> appendBytes(List<int> bytes) async {
final raf = await open(mode: FileMode.writeOnlyAppend);
await raf.writeFrom(bytes);
await raf.close();
}
}

extension FileAppendStringExtension on File {
/// Appends a [string] to the content of this file using UTF-8 or the
/// specified [encoding].
Future<void> appendString(String string, {Encoding encoding = utf8}) async {
final raf = await open(mode: FileMode.writeOnlyAppend);
await raf.writeString(string);
await raf.close();
}
}

extension FileForEachBlockExtension on File {
/// Reads file by byte blocks and calls [action] for each block read.
///
/// This functions passes the byte buffer to the [action] function.
Expand All @@ -24,6 +28,7 @@ extension FileX on File {
Future<void> forEachBlock(
int blockSize, void Function(Uint8List buffer) action) async {
final raf = await open();
// ignore: literal_only_boolean_expressions
while (true) {
final buffer = await raf.read(blockSize);
if (buffer.length == blockSize) {
Expand Down
12 changes: 11 additions & 1 deletion lib/src/io/file_system_entity.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
part of dartx_io;

extension FileSystemEntityX on FileSystemEntity {
extension FileSystemEntityNameExtension on FileSystemEntity {
/// Gets the part of [path] after the last separator.
/// ```dart
/// File('path/to/foo.dart').name; // -> 'foo.dart'
Expand All @@ -12,7 +12,9 @@ extension FileSystemEntityX on FileSystemEntity {
/// Directory('path/to/').name; // -> 'to'
/// ```
String get name => path_helper.basename(path);
}

extension FileSystemEntityNameWithoutExtensionExtension on FileSystemEntity {
/// Gets the part of [path] after the last separator, and without any trailing
/// file extension.
/// ```dart
Expand All @@ -24,7 +26,9 @@ extension FileSystemEntityX on FileSystemEntity {
/// File('path/to/foo.dart/').nameWithoutExtension; // -> 'foo'
/// ```
String get nameWithoutExtension => path_helper.basenameWithoutExtension(path);
}

extension FileSystemEntityDirNameExtension on FileSystemEntity {
/// Gets the part of [path] before the last separator.
/// ```dart
/// File().dirname('path/to/foo.dart'); // -> 'path/to'
Expand All @@ -49,7 +53,9 @@ extension FileSystemEntityX on FileSystemEntity {
/// Directory('').dirName; // -> '.'
/// ```
String get dirName => path_helper.dirname(path);
}

extension FileSystemEntityIsWithinExtension on FileSystemEntity {
/// Returns `true` if this entity is a path beneath `parent`, and `false`
/// otherwise.
/// ```dart
Expand All @@ -58,7 +64,9 @@ extension FileSystemEntityX on FileSystemEntity {
/// Directory('/root/path').isWithin(Directory('/root/path')) // -> false
/// ```
bool isWithin(Directory parent) => path_helper.isWithin(parent.path, path);
}

extension FileSystemEntityWithNameExtension on FileSystemEntity {
///Returns a new [File] with the `name` part changed
///```dart
///File('path/to/foo.dart').withName('bar.txt'); // -> File('path/to/bar.txt')
Expand All @@ -67,7 +75,9 @@ extension FileSystemEntityX on FileSystemEntity {
FileSystemEntity withName(String newName) {
return File('$dirName${Platform.pathSeparator}$newName');
}
}

extension FileSystemEntityExtensionExtension on FileSystemEntity {
///Returns the file extension of the [path], the portion of the `name`
///from the last '.' to the end (including the '.' itself).
///```dart
Expand Down
Loading

0 comments on commit 1e6eecb

Please sign in to comment.