Commit 30a43bbf authored by Luan Nico's avatar Luan Nico 💬 Committed by Jochum van der Ploeg
Browse files

Add getOrPut method to Map

parent 41de4ade
Pipeline #283162342 passed with stage
in 2 minutes and 4 seconds
## 0.5.2
- Add `getOrPut` to `Map`
## 0.5.1
- Add `mapNotNull` and `filterNotNull` to `Iterable`
......
......@@ -5,6 +5,7 @@ import 'dart:collection';
part 'src/collections/associate.dart';
part 'src/collections/get_or_null.dart';
part 'src/collections/get_or_put.dart';
part 'src/collections/grouping_by.dart';
part 'src/collections/chunked.dart';
part 'src/collections/filter_not_null.dart';
......
part of collections;
extension GetOrPut<K, V> on Map<K, V> {
/// Returns the value for the given key. If the key is not found in the map,
/// calls the defaultValue function, puts its result into the map under the
/// given key and returns it.
///
/// **Note**: If the type `V` is nullable and `defaultValue` returns null, it
/// will not be stored as a value for `key`.
V getOrPut(K key, V Function(K value) defaultValue) {
final value = this[key];
if (value == null) {
final answer = defaultValue(key);
this[key] = answer;
return answer;
} else {
return value;
}
}
}
name: dartlin
description: Dartlin is a helper library that provides readable methods with which you can write cleaner looking code.
version: 0.5.1
version: 0.5.2
homepage: https://gitlab.com/wolfenrain/dartlin
environment:
......
import 'package:flutter_test/flutter_test.dart';
import 'package:dartlin/collections.dart';
void main() {
group('getOrPut', () {
test('Should work with non-null map', () {
final map = <String, int>{};
// make sure the return value is non-null by adding 1
expect(map.getOrPut('x', (_) => 2) + 1, 3);
// cached value is there!
expect(map.getOrPut('x', (_) => 10) + 1, 3);
});
test('Should work with nullable map', () {
final map = <String, int?>{};
expect(map.getOrPut('x', (_) => 2), 2);
// subsequent calls to getOrPut do not evaluate the default value
// since the first getOrPut has already stored value 2 in the map
expect(map.getOrPut('x', (_) => 3), 2);
// however null value mapped to a key is treated the same as the missing value
expect(map.getOrPut('y', (_) => null), null);
// so in that case the default value is evaluated
expect(map.getOrPut('y', (_) => 42), 42);
});
test('Should receive the key as the lambda parameter', () {
final doubles = <int, int>{};
expect(doubles.getOrPut(4, (key) => key * 2), 8);
expect(doubles.getOrPut(5, (key) => key * 2), 10);
// make sure values are stored
expect(doubles.getOrPut(4, (key) => key * 100), 8);
expect(doubles.getOrPut(5, (key) => key * 100), 10);
// new value
expect(doubles.getOrPut(1, (key) => key * 2), 2);
});
test('Should invoke lambda exactly once', () {
final map = <int, int>{};
var i = 0;
final lambda = (_) => i++;
expect(i, 0);
map.getOrPut(0, lambda);
expect(i, 1);
map.getOrPut(0, lambda);
expect(i, 1);
// new value
map.getOrPut(1, lambda);
expect(i, 2);
});
});
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment