diff --git a/.packages b/.packages index 4eeafb5..02ed69b 100644 --- a/.packages +++ b/.packages @@ -3,7 +3,7 @@ # # For more info see: https://dart.dev/go/dot-packages-deprecation # -# Generated by pub on 2021-04-04 22:34:30.987993. +# Generated by pub on 2021-06-05 23:42:12.234637. _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/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 0511b0a..38bf311 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.0.4 + +* Fix #1 +* Set and Unset to an empty map + ## 0.0.3+3 * Edit readme diff --git a/lib/src/set.dart b/lib/src/set.dart index b310781..eb2acdf 100644 --- a/lib/src/set.dart +++ b/lib/src/set.dart @@ -9,7 +9,7 @@ /// map = set(map, 'a.b', 2); /// ``` Map set( - Map map, + Map? map, String path, T value, ) { @@ -17,13 +17,13 @@ Map set( if (keys.length == 1) { return Map.from({ - ...(map is Map ? map : {}), + ...map ?? {}, keys.removeAt(0): value, }); } return Map.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), }); } diff --git a/lib/src/unset.dart b/lib/src/unset.dart index 89d8f2c..ce1868a 100644 --- a/lib/src/unset.dart +++ b/lib/src/unset.dart @@ -8,17 +8,17 @@ /// Map map = {'a': {'b': 1}, 'c': 2}; /// map = set(map, 'c'); // {'a': {'b': 1}} /// ``` -Map unset(Map map, String path) { +Map unset(Map? map, String path) { List keys = path.split('.'); if (keys.length == 1) { - map.remove(keys.removeAt(0)); + map!.remove(keys.removeAt(0)); return map; } return Map.from({ - ...(map is Map ? map : {}), - keys[0]: unset(map[keys.removeAt(0)], keys.join('.')), + ...map ?? {}, + keys[0]: unset(map![keys.removeAt(0)] ?? {}, keys.join('.')), }); } diff --git a/pubspec.yaml b/pubspec.yaml index 5826bed..3348085 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 diff --git a/test/gato.dart b/test/gato.dart index 8526c2e..46581c0 100644 --- a/test/gato.dart +++ b/test/gato.dart @@ -47,6 +47,13 @@ void main() { map = set(map, 'a.b', 'gato'); expect(map['a']!['b'], 'gato'); }); + + test('to an empty map', () { + Map map = {}; + + map = set(map, 'a.c.d.e', 2); + expect(map['a']!['c']['d']['e'], 2); + }); }); test('unset by path', () { @@ -57,6 +64,13 @@ void main() { map = unset(map, 'a.b'); expect(map['a']!['b'], null); }); + + test('unset on empty map', () { + Map map = {}; + + map = unset(map, 'a.b'); + expect(map['a']!['b'], null); + }); } class Person {