Updated merge_map

This commit is contained in:
thomashii 2021-05-18 21:27:19 +08:00
parent 26d9936a3f
commit 9dfc847840
7 changed files with 41 additions and 18 deletions

View file

@ -1,3 +1,6 @@
# 2.0.2
* Resolve static analysis warnings
# 2.0.1 # 2.0.1
* Updated README * Updated README

View file

@ -1,5 +1,5 @@
# angel3_merge_map # angel3_merge_map
[![version](https://img.shields.io/badge/pub-v2.0.1-brightgreen)](https://pub.dartlang.org/packages/angel3_merge_map) [![version](https://img.shields.io/badge/pub-v2.0.2-brightgreen)](https://pub.dartlang.org/packages/angel3_merge_map)
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety) [![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion) [![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)

View file

@ -0,0 +1,4 @@
include: package:pedantic/analysis_options.yaml
analyzer:
strong-mode:
implicit-casts: false

View file

@ -1,14 +1,19 @@
import 'package:angel3_merge_map/angel3_merge_map.dart'; import 'package:angel3_merge_map/angel3_merge_map.dart';
void main() { void main() {
// ignore: omit_local_variable_types
Map map1 = {'hello': 'world'}; Map map1 = {'hello': 'world'};
// ignore: omit_local_variable_types
Map map2 = { Map map2 = {
'foo': {'bar': 'baz', 'this': 'will be overwritten'} 'foo': {'bar': 'baz', 'this': 'will be overwritten'}
}; };
// ignore: omit_local_variable_types
Map map3 = { Map map3 = {
'foo': {'john': 'doe', 'this': 'overrides previous maps'} 'foo': {'john': 'doe', 'this': 'overrides previous maps'}
}; };
Map merged = mergeMap([map1, map2, map3]); var merged = mergeMap([map1, map2, map3]);
print(merged); print(merged);
// {hello: world, foo: {bar: baz, john: doe, this: overrides previous maps}} // {hello: world, foo: {bar: baz, john: doe, this: overrides previous maps}}

View file

@ -1,7 +1,7 @@
/// Exposes the [mergeMap] function, which... merges Maps. /// Exposes the [mergeMap] function, which... merges Maps.
library angel3_merge_map; library angel3_merge_map;
_copyValues<K, V>( dynamic _copyValues<K, V>(
Map<K, V> from, Map<K, V?>? to, bool recursive, bool acceptNull) { Map<K, V> from, Map<K, V?>? to, bool recursive, bool acceptNull) {
for (var key in from.keys) { for (var key in from.keys) {
if (from[key] is Map<K, V> && recursive) { if (from[key] is Map<K, V> && recursive) {
@ -25,8 +25,8 @@ _copyValues<K, V>(
/// then if the value on a map is `null`, it will be ignored, and /// then if the value on a map is `null`, it will be ignored, and
/// that `null` will not be copied. /// that `null` will not be copied.
Map<K, V> mergeMap<K, V>(Iterable<Map<K, V>> maps, Map<K, V> mergeMap<K, V>(Iterable<Map<K, V>> maps,
{bool recursive: true, bool acceptNull: false}) { {bool recursive = true, bool acceptNull = false}) {
Map<K, V> result = <K, V>{}; var result = <K, V>{};
maps.forEach((Map<K, V> map) { maps.forEach((Map<K, V> map) {
_copyValues(map, result, recursive, acceptNull); _copyValues(map, result, recursive, acceptNull);
}); });

View file

@ -1,8 +1,9 @@
name: angel3_merge_map name: angel3_merge_map
version: 2.0.1 version: 2.0.2
description: Combine multiple Maps into one. Equivalent to Object.assign in JS. description: Combine multiple Maps into one. Equivalent to Object.assign in JS.
homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/merge_map homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/merge_map
environment: environment:
sdk: '>=2.12.0 <3.0.0' sdk: '>=2.12.0 <3.0.0'
dev_dependencies: dev_dependencies:
test: ^1.17.4 test: ^1.17.4
pedantic: ^1.11.0

View file

@ -1,9 +1,9 @@
import "package:angel3_merge_map/angel3_merge_map.dart"; import 'package:angel3_merge_map/angel3_merge_map.dart';
import "package:test/test.dart"; import 'package:test/test.dart';
void main() { void main() {
test('can merge two simple maps', () { test('can merge two simple maps', () {
Map merged = mergeMap([ var merged = mergeMap([
{'hello': 'world'}, {'hello': 'world'},
{'hello': 'dolly'} {'hello': 'dolly'}
]); ]);
@ -11,7 +11,7 @@ void main() {
}); });
test("the last map's values supersede those of prior", () { test("the last map's values supersede those of prior", () {
Map merged = mergeMap([ var merged = mergeMap([
{'letter': 'a'}, {'letter': 'a'},
{'letter': 'b'}, {'letter': 'b'},
{'letter': 'c'} {'letter': 'c'}
@ -19,16 +19,18 @@ void main() {
expect(merged['letter'], equals('c')); expect(merged['letter'], equals('c'));
}); });
test("can merge two once-nested maps", () { test('can merge two once-nested maps', () {
// ignore: omit_local_variable_types
Map map1 = { Map map1 = {
'hello': 'world', 'hello': 'world',
'foo': {'nested': false} 'foo': {'nested': false}
}; };
// ignore: omit_local_variable_types
Map map2 = { Map map2 = {
'goodbye': 'sad life', 'goodbye': 'sad life',
'foo': {'nested': true, 'it': 'works'} 'foo': {'nested': true, 'it': 'works'}
}; };
Map merged = mergeMap([map1, map2]); var merged = mergeMap([map1, map2]);
expect(merged['hello'], equals('world')); expect(merged['hello'], equals('world'));
expect(merged['goodbye'], equals('sad life')); expect(merged['goodbye'], equals('sad life'));
@ -36,57 +38,65 @@ void main() {
expect(merged['foo']['it'], equals('works')); expect(merged['foo']['it'], equals('works'));
}); });
test("once-nested map supersession", () { test('once-nested map supersession', () {
// ignore: omit_local_variable_types
Map map1 = { Map map1 = {
'hello': 'world', 'hello': 'world',
'foo': {'nested': false} 'foo': {'nested': false}
}; };
// ignore: omit_local_variable_types
Map map2 = { Map map2 = {
'goodbye': 'sad life', 'goodbye': 'sad life',
'foo': {'nested': true, 'it': 'works'} 'foo': {'nested': true, 'it': 'works'}
}; };
// ignore: omit_local_variable_types
Map map3 = { Map map3 = {
'foo': {'nested': 'supersession'} 'foo': {'nested': 'supersession'}
}; };
Map merged = mergeMap([map1, map2, map3]); var merged = mergeMap([map1, map2, map3]);
expect(merged['foo']['nested'], equals('supersession')); expect(merged['foo']['nested'], equals('supersession'));
}); });
test("can merge two twice-nested maps", () { test('can merge two twice-nested maps', () {
// ignore: omit_local_variable_types
Map map1 = { Map map1 = {
'a': { 'a': {
'b': {'c': 'd'} 'b': {'c': 'd'}
} }
}; };
// ignore: omit_local_variable_types
Map map2 = { Map map2 = {
'a': { 'a': {
'b': {'c': 'D', 'e': 'f'} 'b': {'c': 'D', 'e': 'f'}
} }
}; };
Map merged = mergeMap([map1, map2]); var merged = mergeMap([map1, map2]);
expect(merged['a']['b']['c'], equals('D')); expect(merged['a']['b']['c'], equals('D'));
expect(merged['a']['b']['e'], equals('f')); expect(merged['a']['b']['e'], equals('f'));
}); });
test("twice-nested map supersession", () { test('twice-nested map supersession', () {
// ignore: omit_local_variable_types
Map map1 = { Map map1 = {
'a': { 'a': {
'b': {'c': 'd'} 'b': {'c': 'd'}
} }
}; };
// ignore: omit_local_variable_types
Map map2 = { Map map2 = {
'a': { 'a': {
'b': {'c': 'D', 'e': 'f'} 'b': {'c': 'D', 'e': 'f'}
} }
}; };
// ignore: omit_local_variable_types
Map map3 = { Map map3 = {
'a': { 'a': {
'b': {'e': 'supersession'} 'b': {'e': 'supersession'}
} }
}; };
Map merged = mergeMap([map1, map2, map3]); var merged = mergeMap([map1, map2, map3]);
expect(merged['a']['b']['c'], equals('D')); expect(merged['a']['b']['c'], equals('D'));
expect(merged['a']['b']['e'], equals('supersession')); expect(merged['a']['b']['e'], equals('supersession'));