Utilities for comparing (and contrasting) dart values:
#Example
import 'package:contrast/contrast.dart';
import 'dart:math' as math;
main() {
// Easily define comparison operators (see Point class below).
assert(Point.ZERO < Point.UNIT);
assert(Point.UNIT >= Point.ZERO);
assert(Point.ZERO == new Point(0, 0));
// Find extrema of arbitrary comparables.
// (dart:math's min/max only work with numbers)
assert(min(Point.ZERO, Point.UNIT) == Point.ZERO);
assert(max(Point.ZERO, Point.UNIT) == Point.UNIT);
assert(clamp(new Point(1, 2), Point.ZERO, Point.UNIT) == Point.UNIT);
assert(between(new Point(0.5, 0.5), Point.ZERO, Point.UNIT));
// Find extrema of iterables.
var points = [Point.UNIT, new Point(1, 2), Point.ZERO, new Point(0, 1)];
assert(minOf(points) == Point.ZERO);
assert(maxOf(points) == new Point(1, 2));
// Sort into a new List easily ( vs. iterable.toList()..sort() ).
var sortedPoints = sorted(points);
// Check ordering of iterables.
assert(isOrdered(sortedPoints));
assert(isStrictlyOrdered(sortedPoints));
// Define comparators using combinator helpers...
// By key function
var byMagnitude = by((point) => point.magnitude);
assert(max(Point.ZERO, new Point(1, 1),
compare: byMagnitude) == new Point(1, 1));
// Compound
assert(max(Point.ZERO, new Point(1, 1),
compare: compound([byMagnitude, Comparable.compare])) == new Point(1, 1));
// Reversed
assert(max(Point.ZERO, Point.UNIT, compare: reverse()) == Point.ZERO);
// Lexicographic
assert(min([Point.ZERO, Point.UNIT], [Point.ZERO, new Point(2, 2)],
compare: lexicographic()).last == Point.UNIT);
// Null safe
var maybePoints = [Point.UNIT, null, Point.ZERO, null];
assert(sorted(maybePoints, compare: nullsFirst()).first == null);
assert(sorted(maybePoints, compare: nullsLast()).last == null);
}
// Mixin `Comparable`based comparison operators with `Quantity`.
class Point extends Object with Quantity<Point> {
static Point ZERO = new Point(0, 0);
static Point UNIT = new Point(1, 1);
final num x, y;
num get magnitude => math.sqrt(x*x + y*y);
Point(this.x, this.y);
// Use comparator combinators`compound` and `by`.
static final _comparator = compound([by((p) => p.x), by((p) => p.y)]);
int compareTo(Point other) => _comparator(this, other);
toString() => '($x, $y)';
}
example/example.dart
import 'package:contrast/contrast.dart';
import 'dart:math' as math;
main() {
// Easily define comparison operators (see Point class below).
assert(Point.ZERO < Point.UNIT);
assert(Point.UNIT >= Point.ZERO);
assert(Point.ZERO == new Point(0, 0));
// Find extrema of arbitrary comparables.
// (dart:math's min/max only work with numbers)
assert(min(Point.ZERO, Point.UNIT) == Point.ZERO);
assert(max(Point.ZERO, Point.UNIT) == Point.UNIT);
assert(clamp(new Point(1, 2), Point.ZERO, Point.UNIT) == Point.UNIT);
assert(between(new Point(0.5, 0.5), Point.ZERO, Point.UNIT));
// Find extrema of iterables.
var points = [Point.UNIT, new Point(1, 2), Point.ZERO, new Point(0, 1)];
assert(minOf(points) == Point.ZERO);
assert(maxOf(points) == new Point(1, 2));
// Sort into a new List easily ( vs. iterable.toList()..sort() ).
var sortedPoints = sorted(points);
// Check ordering of iterables.
assert(isOrdered(sortedPoints));
assert(isStrictlyOrdered(sortedPoints));
// Define comparators using combinator helpers...
// By key function
var byMagnitude = by((point) => point.magnitude);
assert(max(Point.ZERO, new Point(1, 1),
compare: byMagnitude) == new Point(1, 1));
// Compound
assert(max(Point.ZERO, new Point(1, 1),
compare: compound([byMagnitude, Comparable.compare])) == new Point(1, 1));
// Reversed
assert(max(Point.ZERO, Point.UNIT, compare: reverse()) == Point.ZERO);
// Lexicographic
assert(min([Point.ZERO, Point.UNIT], [Point.ZERO, new Point(2, 2)],
compare: lexicographic()).last == Point.UNIT);
// Null safe
var maybePoints = [Point.UNIT, null, Point.ZERO, null];
assert(sorted(maybePoints, compare: nullsFirst()).first == null);
assert(sorted(maybePoints, compare: nullsLast()).last == null);
}
// Mixin `Comparable`based comparison operators with `Quantity`.
class Point extends Object with Quantity<Point> {
static Point ZERO = new Point(0, 0);
static Point UNIT = new Point(1, 1);
final num x, y;
num get magnitude => math.sqrt(x*x + y*y);
Point(this.x, this.y);
// Use comparator combinators`compound` and `by`.
static final _comparator = compound([by((p) => p.x), by((p) => p.y)]);
int compareTo(Point other) => _comparator(this, other);
toString() => '($x, $y)';
}
Add this to your package's pubspec.yaml file:
dependencies:
contrast: ^0.1.1
You can install packages from the command line:
with pub:
$ pub get
Alternatively, your editor might support pub get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:contrast/contrast.dart';
Version  Uploaded  Documentation  Archive 

0.1.1  Sep 23, 2014  
0.1.0  Sep 19, 2014 
Popularity:
Describes how popular the package is relative to other packages.
[more]

0

Health:
Code health derived from static analysis.
[more]



Maintenance:
Reflects how tidy and uptodate the package is.
[more]



Overall:
Weighted score of the above.
[more]

0

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.
Fix lib/src/extrema.dart
. (82.20 points)
Analysis of lib/src/extrema.dart
failed with 6 errors, including:
line 8 col 41: The method tearoff 'compare' has type '(Comparable<dynamic>, Comparable<dynamic>) → int' that isn't of expected type '(dynamic, dynamic) → int'. This means its parameter or return type does not match what is expected.
line 15 col 41: The method tearoff 'compare' has type '(Comparable<dynamic>, Comparable<dynamic>) → int' that isn't of expected type '(dynamic, dynamic) → int'. This means its parameter or return type does not match what is expected.
line 25 col 58: The method tearoff 'compare' has type '(Comparable<dynamic>, Comparable<dynamic>) → int' that isn't of expected type '(dynamic, dynamic) → int'. This means its parameter or return type does not match what is expected.
line 33 col 57: The method tearoff 'compare' has type '(Comparable<dynamic>, Comparable<dynamic>) → int' that isn't of expected type '(dynamic, dynamic) → int'. This means its parameter or return type does not match what is expected.
line 47 col 27: The method tearoff 'compare' has type '(Comparable<dynamic>, Comparable<dynamic>) → int' that isn't of expected type '(dynamic, dynamic) → int'. This means its parameter or return type does not match what is expected.
Fix lib/src/comparators.dart
. (76.27 points)
Analysis of lib/src/comparators.dart
failed with 5 errors:
line 14 col 27: The method tearoff 'compare' has type '(Comparable<dynamic>, Comparable<dynamic>) → int' that isn't of expected type '(dynamic, dynamic) → int'. This means its parameter or return type does not match what is expected.
line 45 col 27: The method tearoff 'compare' has type '(Comparable<dynamic>, Comparable<dynamic>) → int' that isn't of expected type '(dynamic, dynamic) → int'. This means its parameter or return type does not match what is expected.
line 58 col 27: The method tearoff 'compare' has type '(Comparable<dynamic>, Comparable<dynamic>) → int' that isn't of expected type '(dynamic, dynamic) → int'. This means its parameter or return type does not match what is expected.
line 80 col 45: The method tearoff 'compare' has type '(Comparable<dynamic>, Comparable<dynamic>) → int' that isn't of expected type '(dynamic, dynamic) → int'. This means its parameter or return type does not match what is expected.
line 93 col 44: The method tearoff 'compare' has type '(Comparable<dynamic>, Comparable<dynamic>) → int' that isn't of expected type '(dynamic, dynamic) → int'. This means its parameter or return type does not match what is expected.
Fix lib/src/ordered.dart
. (43.75 points)
Analysis of lib/src/ordered.dart
failed with 2 errors:
line 9 col 27: The method tearoff 'compare' has type '(Comparable<dynamic>, Comparable<dynamic>) → int' that isn't of expected type '(dynamic, dynamic) → int'. This means its parameter or return type does not match what is expected.
line 17 col 27: The method tearoff 'compare' has type '(Comparable<dynamic>, Comparable<dynamic>) → int' that isn't of expected type '(dynamic, dynamic) → int'. This means its parameter or return type does not match what is expected.
Fix additional 3 files with analysis or formatting issues. (50.38 points)
Additional issues in the following files:
lib/src/quantity.dart
(1 error, 1 hint)lib/src/sorted.dart
(1 error)lib/contrast.dart
(Run dartfmt
to format lib/contrast.dart
.)Add SDK constraint in pubspec.yaml
. (50 points)
For information about setting SDK constraint, please see https://www.dartlang.org/tools/pub/pubspec#sdkconstraints.
Fix platform conflicts. (20 points)
Low code quality prevents platform classification.
Package is too old. (100 points)
The package was released more than two years ago.
Maintain CHANGELOG.md
. (20 points)
Changelog entries help clients to follow the progress in your code.
The description is too short. (20 points)
Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.