Skip to content

Commit

Permalink
Release v0.0.4 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdizarepour committed Jun 5, 2021
2 parents 421ea4f + 641f48b commit 4cdf38b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .packages
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# For more info see: https://dart.dev/go/dot-packages-deprecation
#
# Generated by pub on 2021-06-05 23:50:04.634348.
# Generated by pub on 2021-06-05 23:53:09.894730.
_fe_analyzer_shared:file:///Users/mehdi/.pub-cache/hosted/pub.dartlang.org/_fe_analyzer_shared-19.0.0/lib/
analyzer:file:///Users/mehdi/.pub-cache/hosted/pub.dartlang.org/analyzer-1.3.0/lib/
args:file:///Users/mehdi/.pub-cache/hosted/pub.dartlang.org/args-2.0.0/lib/
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.0.4

* Fix #1
* Set and Unset to an empty map

## 0.0.3+3

* Edit readme
Expand Down
8 changes: 4 additions & 4 deletions lib/src/set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
/// map = set(map, 'a.b', 2);
/// ```
Map<String, dynamic> set<T>(
Map<String, dynamic> map,
Map<String, dynamic>? map,
String path,
T value,
) {
List<String> keys = path.split('.');

if (keys.length == 1) {
return Map<String, dynamic>.from({
...(map is Map ? map : {}),
...map ?? {},
keys.removeAt(0): value,
});
}

return Map<String, dynamic>.from({
...(map is Map ? map : {}),
keys[0]: set(map[keys.removeAt(0)], keys.join('.'), value),
...map ?? {},
keys[0]: set(map![keys.removeAt(0)] ?? {}, keys.join('.'), value),
});
}
8 changes: 4 additions & 4 deletions lib/src/unset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
/// Map map = {'a': {'b': 1}, 'c': 2};
/// map = set(map, 'c'); // {'a': {'b': 1}}
/// ```
Map<String, dynamic> unset(Map<String, dynamic> map, String path) {
Map<String, dynamic> unset(Map<String, dynamic>? map, String path) {
List<String> keys = path.split('.');

if (keys.length == 1) {
map.remove(keys.removeAt(0));
map!.remove(keys.removeAt(0));

return map;
}

return Map<String, dynamic>.from({
...(map is Map ? map : {}),
keys[0]: unset(map[keys.removeAt(0)], keys.join('.')),
...map ?? {},
keys[0]: unset(map![keys.removeAt(0)] ?? {}, keys.join('.')),
});
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: gato
description: Gato is a dart utility library inspired by javascript lodash library.
version: 0.0.3+3
version: 0.0.4
author: Mehdi Zarepour
homepage: https://github.com/mehdizarepour/gato
repository: https://github.com/mehdizarepour/gato
Expand Down
14 changes: 14 additions & 0 deletions test/gato.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ void main() {
map = set<String>(map, 'a.b', 'gato');
expect(map['a']!['b'], 'gato');
});

test('to an empty map', () {
Map<String, dynamic> map = {};

map = set(map, 'a.c.d.e', 2);
expect(map['a']!['c']['d']['e'], 2);
});
});

test('unset by path', () {
Expand All @@ -57,6 +64,13 @@ void main() {
map = unset(map, 'a.b');
expect(map['a']!['b'], null);
});

test('unset on empty map', () {
Map<String, dynamic> map = {};

map = unset(map, 'a.b');
expect(map['a']!['b'], null);
});
}

class Person {
Expand Down

0 comments on commit 4cdf38b

Please sign in to comment.