Skip to content

Instantly share code, notes, and snippets.

@ha-yi
Last active June 9, 2020 04:50
Show Gist options
  • Save ha-yi/6eadae81bead7f31c004d4f508833ee7 to your computer and use it in GitHub Desktop.
Save ha-yi/6eadae81bead7f31c004d4f508833ee7 to your computer and use it in GitHub Desktop.
import 'package:test/test.dart';
extension ListExt<T> on List<T> {
/// Remove an object from
operator -(T other) async {
this.remove(other);
return this;
}
/// Remove all objects in array
operator ~/(List<T> other) {
other.forEach((e) => this.remove(e));
return this;
}
bool all(bool Function(T) varificator) {
for(T data in this) {
if (!varificator.call(data)) return false;
}
return true;
}
}
void main() {
test("Test minus operator", () {
List<String> data = ["A" , "B", "C", "D"];
var newlist = data - "A";
assert(!newlist.contains("A"));
});
test("Test Remove all object", () {
List<String> data = ["A" , "B", "C", "D"];
var newlist = data ~/ ["A", "B"];
assert(!newlist.contains("A"));
assert(!newlist.contains("B"));
});
test("Test all method", (){
List<String> data = ["A" , "B", "C", "D"];
var allSingleChar = data.all((e) => e.length == 1);
assert(allSingleChar);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment