Skip to content

Commit

Permalink
Merge branch 'master' into feature/strings
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
#	lib/src/string.dart
#	test/string_test.dart
  • Loading branch information
passsy committed Apr 12, 2022
2 parents ff15d8d + b1e352e commit 2d58b25
Show file tree
Hide file tree
Showing 16 changed files with 166 additions and 102 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.0
- Promote to stable version
- Remove deprecated extensions

## 0.8.0

- [PR-136](https://github.com/leisim/dartx/pull/136) New: Multiple extensions for `Map`. `all()`, `any()`, `count()`, `filter()`, `filterKeys()`, `filterNot`, `filterValues`, `getOrElse()`, `mapEntries()`, `mapKeys()`, `mapValues()`, `maxBy()`, `maxWith()`, `minBy()`, `minWith`, `none()`, `toList()`, `toMap()`, `orEmpty()`
Expand Down
57 changes: 35 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ final increasingSubSequences = list.chunkWhile((a, b) => a + 1 == b);
`splitWhen` is the opposite of `chunkWhile` that starts a new chunk every time
the predicate _didn't_ match.

## String
## int

### buildString()

Expand All @@ -107,25 +107,26 @@ final word = buildString((sb) {
}
});
// 0123456789
```
```

### .capitalize
### .ordinal

Returns a copy of the string having its first letter uppercased, or the original string, if it's empty or already starts with an upper case letter.
Returns an ordinal number of `String` type for any integer

```dart
final word = 'abcd'.capitalize(); // Abcd
final anotherWord = 'Abcd'.capitalize(); // Abcd
final a = 1.ordinal(); // 1st
final b = 108.ordinal(); // 108th
```

### .chars - DEPRECATED
## String

Use `.characters` from the official characters package.
### .capitalize

Get a list of single character strings from a string. Supports emojis.
Returns a copy of the string having its first letter uppercased, or the original string, if it's empty or already starts with an upper case letter.

```dart
final chars = 'family👨‍👨‍👧‍👦'.chars; // ['f', 'a', 'm', 'i', 'l', 'y', '👨‍👨‍👧‍👦']
final word = 'abcd'.capitalize(); // Abcd
final anotherWord = 'Abcd'.capitalize(); // Abcd
```

### .decapitalize
Expand Down Expand Up @@ -231,6 +232,30 @@ final isBlank = ' '.isNullOrEmpty; // false
final isLineBreak = '\n'.isNullOrEmpty; // false
```

### .isNullOrBlank

Returns `true` if the String is either `null` or blank.

```dart
final isNull = null.isNullOrBlank; // true
final isEmpty = ''.isNullOrBlank; // true
final isBlank = ' '.isNullOrBlank; // true
final isLineBreak = '\n'.isNullOrBlank; // true
final isFoo = ' foo '.isNullOrBlank; // false
```

### .isNotNullOrBlank

Returns `true` if the String is neither `null` nor blank.

```dart
final isNull = null.isNullOrBlank; // true
final isEmpty = ''.isNullOrBlank; // true
final isBlank = ' '.isNullOrBlank; // true
final isLineBreak = '\n'.isNullOrBlank; // true
final isFoo = ' foo '.isNullOrBlank; // true
```

### .isUpperCase

Returns `true` if the entire string is upper case.
Expand Down Expand Up @@ -426,18 +451,6 @@ for (final i in 10.rangeTo(2).step(2)) {

## Function

### .invoke() - DEPRECATED

Use `call()` instead. This is very useful for `null` checks.

```dart
final func = (String value) {
print(value);
}
func?.call('hello world');
```

### .partial(), .partial2() ...

Applies some of the required arguments to a function and returns a function which takes the remaining arguments.
Expand Down
1 change: 1 addition & 0 deletions lib/dartx.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ part 'src/arithmetic.dart';
part 'src/comparable.dart';
part 'src/comparator.dart';
part 'src/function.dart';
part 'src/int.dart';
part 'src/iterable.dart';
part 'src/iterable_num.dart';
part 'src/list.dart';
Expand Down
6 changes: 0 additions & 6 deletions lib/src/function.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ 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 Function0InvokeExtension<R> on Function0<R> {
/// Invokes this function and returns it's return value.
@Deprecated('Use `call()`')
R invoke() => this();
}

extension Function1InvokeExtensions<A, R> on Function1<A, R> {
/// Invokes this function and returns it's return value.
R invoke(A first) => this(first);
Expand Down
29 changes: 29 additions & 0 deletions lib/src/int.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
part of dartx;

extension Ordinals<T extends int> on T {
/// Returns an ordinal number of `String` type for any integer
///
/// ```dart
/// 101.ordinal(); // 101st
///
/// 999218.ordinal(); // 999218th
/// ```
String ordinal() {
final onesPlace = this % 10;
final tensPlace = ((this / 10).floor()) % 10;
if (tensPlace == 1) {
return '${this}th';
} else {
switch (onesPlace) {
case 1:
return '${this}st';
case 2:
return '${this}nd';
case 3:
return '${this}rd';
default:
return '${this}th';
}
}
}
}
14 changes: 7 additions & 7 deletions lib/src/iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension IterableThirdItem<E> on Iterable<E> {
E get third => elementAt(2);
}

extension IterableForthItem<E> on Iterable<E> {
extension IterableFourthItem<E> on Iterable<E> {
/// Fourth element.
///
/// ```dart
Expand Down Expand Up @@ -277,8 +277,8 @@ extension IterableSortedBy<E> on Iterable<E> {
///
/// **Note:** The actual sorting is performed when an element is accessed for
/// the first time.
_SortedList<E> sortedBy(Comparable Function(E element) selector) {
return _SortedList<E>._withSelector(this, selector, 1, null);
SortedList<E> sortedBy(Comparable Function(E element) selector) {
return SortedList<E>._withSelector(this, selector, 1, null);
}
}

Expand All @@ -292,8 +292,8 @@ extension IterableSortedByDescending<E> on Iterable<E> {
///
/// **Note:** The actual sorting is performed when an element is accessed for
/// the first time.
_SortedList<E> sortedByDescending(Comparable Function(E element) selector) {
return _SortedList<E>._withSelector(this, selector, -1, null);
SortedList<E> sortedByDescending(Comparable Function(E element) selector) {
return SortedList<E>._withSelector(this, selector, -1, null);
}
}

Expand All @@ -306,8 +306,8 @@ extension IterableSortedWith<E> on Iterable<E> {
///
/// **Note:** The actual sorting is performed when an element is accessed for
/// the first time.
_SortedList<E> sortedWith(Comparator<E> comparator) {
return _SortedList<E>._(this, comparator);
SortedList<E> sortedWith(Comparator<E> comparator) {
return SortedList<E>._(this, comparator);
}
}

Expand Down
18 changes: 9 additions & 9 deletions lib/src/sorted_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ Comparator<E> _getComparator<E>(
return parent?.compose(newComparator) ?? newComparator;
}

class _SortedList<E> extends _DelegatingList<E> {
class SortedList<E> extends _DelegatingList<E> {
final Iterable<E> _source;
final Comparator<E> _comparator;
List<E>? _sortedResults;

_SortedList._(
SortedList._(
this._source,
this._comparator,
);

_SortedList._withSelector(
SortedList._withSelector(
this._source,
Comparable Function(E element) selector,
int order,
Expand All @@ -44,8 +44,8 @@ class _SortedList<E> extends _DelegatingList<E> {
///
/// **Note:** The actual sorting is performed when an element is accessed for
/// the first time.
_SortedList<E> thenBy(Comparable Function(E element) selector) {
return _SortedList<E>._withSelector(this, selector, 1, _comparator);
SortedList<E> thenBy(Comparable Function(E element) selector) {
return SortedList<E>._withSelector(this, selector, 1, _comparator);
}

/// Returns a new list with all elements sorted according to previously
Expand All @@ -54,17 +54,17 @@ class _SortedList<E> extends _DelegatingList<E> {
///
/// **Note:** The actual sorting is performed when an element is accessed for
/// the first time.
_SortedList<E> thenByDescending(Comparable Function(E element) selector) {
return _SortedList<E>._withSelector(this, selector, -1, _comparator);
SortedList<E> thenByDescending(Comparable Function(E element) selector) {
return SortedList<E>._withSelector(this, selector, -1, _comparator);
}

/// Returns a new list with all elements sorted according to previously
/// defined order and specified [comparator].
///
/// **Note:** The actual sorting is performed when an element is accessed for
/// the first time.
_SortedList<E> thenWith(Comparator<E> comparator) {
return _SortedList<E>._(this, _comparator.compose(comparator));
SortedList<E> thenWith(Comparator<E> comparator) {
return SortedList<E>._(this, _comparator.compose(comparator));
}

@override
Expand Down
22 changes: 10 additions & 12 deletions lib/src/string.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@ part of dartx;
const _ascii = 0x007f;
const _latin1 = 0x00ff;

extension StringCharsExtension on String {
/// The characters of a string.
///
/// A character is a Unicode Grapheme cluster represented by a substring of
/// the original string.
///
/// Please use [StringCharacters].characters
/// https://github.com/dart-lang/characters/blob/10527437926f1b454edf9912fe700aa2506b1c3d/lib/src/extensions.dart#L9
@Deprecated('Use .characters from the official characters package')
Iterable<String> get chars => characters.Characters(this);
}

extension StringCapitalizeExtension on String {
/// Returns a copy of this string having its first letter uppercased, or the
/// original string, if it's empty or already starts with an upper case
Expand Down Expand Up @@ -267,6 +255,16 @@ extension NullableStringIsNotNullOrEmptyExtension on String? {
bool get isNotNullOrEmpty => !isNullOrEmpty;
}

extension NullableStringIsNullOrBlankExtension on String? {
/// Returns `true` if the string is either `null` or blank.
bool get isNullOrBlank => this?.isBlank ?? true;
}

extension NullableStringIsNotNullOrBlankExtension on String? {
/// Returns `true` if the string is neither null nor blank.
bool get isNotNullOrBlank => !isNullOrBlank;
}

extension NullableStringOrEmptyExtension on String? {
/// Returns the string if it is not `null`, or the empty string otherwise.
String orEmpty() => this ?? '';
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dartx
description: Superpowers for Dart. Collection of useful static extension methods.
version: 0.8.0
version: 1.0.0
homepage: https://github.com/leisim/dartx

environment:
Expand Down
9 changes: 0 additions & 9 deletions test/function_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ import 'package:test/test.dart';

void main() {
group('Function', () {
group('Function0X', () {
final func = () => 5;

test('.invoke', () {
// ignore: deprecated_member_use_from_same_package
expect(func.invoke(), 5);
});
});

group('Function1X', () {
final func = (String s) => s;

Expand Down
27 changes: 27 additions & 0 deletions test/int_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:dartx/dartx.dart';
import 'package:test/test.dart';

void main() {
group(
'intX',
() {
test(
'.ordinal()',
() {
expect(0.ordinal(), '0th');
expect(1.ordinal(), '1st');
expect(2.ordinal(), '2nd');
expect(3.ordinal(), '3rd');
expect(4.ordinal(), '4th');
expect(10.ordinal(), '10th');
expect(101.ordinal(), '101st');
expect(102.ordinal(), '102nd');
expect(103.ordinal(), '103rd');
expect(111.ordinal(), '111th');
expect(112.ordinal(), '112th');
expect(113.ordinal(), '113th');
},
);
},
);
}
Loading

0 comments on commit 2d58b25

Please sign in to comment.