From 7dbe272786f2b5f358802db40d20dd32082ddad2 Mon Sep 17 00:00:00 2001 From: danylo-safonov-solid Date: Thu, 16 Nov 2023 13:46:58 +0200 Subject: [PATCH] extract --- lib/src/add_imports/better_map.dart | 11 +++++++++++ lib/src/add_imports/config.dart | 12 +++--------- 2 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 lib/src/add_imports/better_map.dart diff --git a/lib/src/add_imports/better_map.dart b/lib/src/add_imports/better_map.dart new file mode 100644 index 0000000..dfaddae --- /dev/null +++ b/lib/src/add_imports/better_map.dart @@ -0,0 +1,11 @@ +// This is an internal implementation so it is okay here. +// ignore_for_file: public_member_api_docs +typedef Predicate = bool Function(T); + +extension BetterMap on Map { + Map mapValues(V1 Function(V) f) => + map((k, v) => MapEntry(k, f(v))); + + MapEntry firstWhereValue(Predicate predicate) => + entries.firstWhere((e) => predicate(e.value)); +} diff --git a/lib/src/add_imports/config.dart b/lib/src/add_imports/config.dart index a88ca2d..f820565 100644 --- a/lib/src/add_imports/config.dart +++ b/lib/src/add_imports/config.dart @@ -2,6 +2,7 @@ // ignore_for_file: public_member_api_docs import 'package:collection/collection.dart'; +import 'package:deno_postgres_interop/src/add_imports/better_map.dart'; import 'package:deno_postgres_interop/src/add_imports/class_interop_data.dart'; import 'package:yaml/yaml.dart'; @@ -33,10 +34,8 @@ class Config { } } - String _filenameForClass(String classname) => classesMap.entries - .firstWhere( - (e) => e.value.any((interop) => interop.jsName == classname), - ) + String _filenameForClass(String classname) => classesMap + .firstWhereValue((v) => v.map((e) => e.jsName).contains(classname)) .key; String importStringForClass(String classname) { @@ -46,8 +45,3 @@ class Config { return 'import { $classname } from "$url";'; } } - -extension MapMapValues on Map { - Map mapValues(V1 Function(V) f) => - map((k, v) => MapEntry(k, f(v))); -}