dartx 0.3.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 94

Dart CI Codecov dartx flutterx

If you miss an extension, please open an issue or pull request

Resources: #

On this page you can find some of the extensions. Take a look at the docs to see all of them.

Getting started ๐ŸŽ‰ #

Add the following to you pubspec.yaml and replace [version] with the latest version:

dependencies:
  dartx: ^[version]

After you import the library, you can use the extensions.

import 'package:dartx/dartx.dart';

var slice = [1, 2, 3, 4, 5].slice(1, -2); // [2, 3, 4]

Iterable #

.slice() #

Returns elements at indices between start (inclusive) and end (inclusive).

var list = [0, 1, 2, 3, 4, 5];
var last = list.slice(-1); // [5]
var lastHalf = list.slice(3); // [3, 4, 5]
var allButFirstAndLast = list.slice(1, -2); // [1, 2, 3, 4]

.sortedBy() & .thenBy() #

Sort lists by multiple properties.

var dogs = [
  Dog(name: "Tom", age: 3),
  Dog(name: "Charlie", age: 7),
  Dog(name: "Bark", age: 1),
  Dog(name: "Cookie", age: 4),
  Dog(name: "Charlie", age: 2),
];

var sorted = dogs
    .sortedBy((dog) => dog.name)
    .thenByDescending((dog) => dog.age);
// Bark, Cookie, Charlie (7), Charlie (2), Tom

.distinctBy() #

Get distinct elements from a list.

var list = ['this', 'is', 'a', 'test'];
var distinctByLength = list.distinctBy((it) => it.length); // ['this', 'is', 'a']

.flatten() #

Get a new lazy Iterable of all elements from all collections in a collection.

var nestedList = [[1, 2, 3], [4, 5, 6]];
var flattened = nestedList.flatten(); // [1, 2, 3, 4, 5, 6]

String #

.chars #

Get a list of single character strings from a string. Supports emojis.

var chars = 'family๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆ'.chars; // ['f', 'a', 'm', 'i', 'l', 'y', '๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆ']

.isBlank #

Returns true if this string is empty or consists solely of whitespace characters.

var notBlank = '   .'.isBlank; // false
var blank = '  '.isBlank; // true

.toIntOrNull() #

Parses the string as an ineger or returns null if it is not a number.

var number = '12345'.toIntOrNull(); // 12345
var notANumber = '123-45'.toIntOrNull(); // null

Time utils #

Dartx exports @jogboms great โฐ time.dart package so you can do the following:

int secondsInADay = 1.days.inSeconds;

Duration totalTime = [12.5.seconds, 101.milliseconds, 2.5.minutes].sum();

DateTime oneWeekLater = DateTime.now() + 1.week;

Check out โฐ time.dart for more information and examples.

num #

.coerceIn() #

Ensures that this value lies in the specified range.

var numberInRange = 123.coerceIn(0, 1000); // 123
var numberOutOfRange = -123.coerceIn(0, 1000); // 0

.toBytes() #

Converts this value to binary form.

range #

rangeTo #

Creates a range between two ints (upwards, downwards and with custom steps)

// upwards with default step size 1
for (var i in 1.rangeTo(5)) {
  print(i); // 1, 2, 3, 4, 5
}
// downwards with custom step
for (var i in 10.rangeTo(2).step(2)) {
  print(i); // 10, 8, 6, 4, 2
}

Function #

.invoke() - DEPRECATED #

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

var 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.

void greet(String firstName, String lastName) {
  print('Hi $firstName $lastName!');
}

var greetStark = greet.partial('Stark');
greetStark('Sansa'); // Hi Sansa Stark!
greetStark('Tony'); // Hi Tony Stark!

File #

.name #

Get the name and extension of a file.

var file = File('some/path/testFile.dart');
print(file.name); // testFile.dart
print(file.nameWithoutExtension); // testFile

.appendText() #

Append text to a file.

await File('someFile.json').appendText('{test: true}');

.isWithin() #

Checks if a file is inside a directory.

var dir = Directory('some/path');
File('some/path/file.dart').isWithin(dir); // true

License #

Copyright 2019 Simon Leier

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

0.3.0 #

  • PR-14 New int.rangeTo(int) extension
  • PR-24 Default implementations for Comparable <, <=, >= and > operators.
  • PR-13 Removed quiver dependency
  • PR-21 New String.md5 to calculate a MD5 digest
  • PR-25 New FileSystemEntity.withName(String newName) to get a File with a different name
  • PR-25 New FileSystemEntity.extension to get the file extension
  • PR-17 Callable.invoke() is now marked as Deprecated in favour of darts call() method. It will not be removed though, until darts .call() methods gets auto-completion.
  • PR-30 New Comparable.coerceIn(), Comparable.coerceAtLeast() and Comparable.coerceAtMost()
  • PR-31 Fixed num.toBytes()
  • PR-33 Fixed .thenBy() and .thenWith() extensions for Lists

Breaking #

  • PR-29 Iterable<E>.windowed - remove the required transform lambda

0.2.0 #

  • Moved to time.dart library for time extensions

0.1.2 #

  • Fixed docs
  • Added Function.curry(), Function.invoke(), Function.partial() and Function.flip()
  • 'string'.reversed now correctly handles grapheme clusters (like emoji)

Breaking #

  • 'string'.chars now returns the grapheme clusters instead of Runes

0.1.1 #

  • Bugfixes
  • More tests

0.1.0+1 #

  • Initial release

example/main.dart

import 'package:dartx/dartx.dart';

void main() {
  var list = [0, 1, 2, 3, 4, 5];
  var last = list.slice(-1); // [5]
  var lastHalf = list.slice(3); // [3, 4, 5]
  var allButFirstAndLast = list.slice(1, -2); // [1, 2, 3, 4]

  var dogs = [
    Dog(name: "Tom", age: 3),
    Dog(name: "Charlie", age: 7),
    Dog(name: "Bark", age: 1),
    Dog(name: "Cookie", age: 4),
    Dog(name: "Charlie", age: 2),
  ];

  var sorted =
      dogs.sortedBy((dog) => dog.name).thenByDescending((dog) => dog.age);
  // Bark, Cookie, Charlie (7), Charlie (2), Tom

  var words = ['this', 'is', 'a', 'test'];
  var distinctByLength =
      words.distinctBy((it) => it.length); // ['this', 'is', 'a']

  var nestedList = [
    [1, 2, 3],
    [4, 5, 6]
  ];
  var flattened = nestedList.flatten(); // [1, 2, 3, 4, 5, 6]
}

class Dog {
  final String name;
  final int age;

  Dog({this.name, this.age});
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dartx: ^0.3.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dartx/dartx.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
92
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
94
Learn more about scoring.

We analyzed this package on Mar 26, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health suggestions

Format lib/src/comparable.dart.

Run dartfmt to format lib/src/comparable.dart.

Format lib/src/io/file_system_entity.dart.

Run dartfmt to format lib/src/io/file_system_entity.dart.

Format lib/src/num.dart.

Run dartfmt to format lib/src/num.dart.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (characters).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0-dev.5.0 <3.0.0
characters >=0.3.0 <0.4.0 0.3.1 0.5.0
collection >=1.14.11 <1.15.0 1.14.12
crypto >=2.1.0 <2.2.0 2.1.4
path >=1.6.4 <1.7.0 1.6.4
time ^1.2.0 1.2.0
Transitive dependencies
charcode 1.1.3
convert 2.1.1
typed_data 1.1.6
Dev dependencies
pedantic ^1.8.0
test ^1.9.2