attributes.dart 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. typedef Attributes = Map<String, dynamic>;
  2. int hashAttributes(Attributes attributes) {
  3. return Object.hashAllUnordered(
  4. attributes.entries.map((e) => Object.hash(e.key, e.value)));
  5. }
  6. Attributes invertAttributes(Attributes? attr, Attributes? base) {
  7. attr ??= {};
  8. base ??= {};
  9. final Attributes baseInverted = base.keys.fold({}, (memo, key) {
  10. if (base![key] != attr![key] && attr.containsKey(key)) {
  11. memo[key] = base[key];
  12. }
  13. return memo;
  14. });
  15. return attr.keys.fold(baseInverted, (memo, key) {
  16. if (attr![key] != base![key] && base.containsKey(key)) {
  17. memo[key] = null;
  18. }
  19. return memo;
  20. });
  21. }
  22. Attributes? composeAttributes(Attributes? a, Attributes? b) {
  23. a ??= {};
  24. b ??= {};
  25. final Attributes attributes = {};
  26. attributes.addAll(Map.from(b)..removeWhere((_, value) => value == null));
  27. for (final entry in a.entries) {
  28. if (!b.containsKey(entry.key)) {
  29. attributes[entry.key] = entry.value;
  30. }
  31. }
  32. if (attributes.isEmpty) {
  33. return null;
  34. }
  35. return attributes;
  36. }