genUiCatalogWeight function

GenUiCatalogWeight genUiCatalogWeight(
  1. Map<String, Object?> catalogJson
)

Measures what catalogJson costs, component by component.

catalogJson is the document genUiCatalogJson produces.

Implementation

GenUiCatalogWeight genUiCatalogWeight(Map<String, Object?> catalogJson) {
  const encoder = JsonEncoder();
  final int total = encoder.convert(catalogJson).length;
  final Map<String, Object?> components =
      (catalogJson['components'] as Map?)?.cast<String, Object?>() ??
      const <String, Object?>{};

  final entries = <String, int>{
    for (final entry in components.entries)
      entry.key: encoder.convert(entry.value).length,
  };
  final sorted = entries.entries.toList()
    ..sort((a, b) => b.value.compareTo(a.value));

  final int componentTotal = entries.values.fold(0, (sum, size) => sum + size);
  return GenUiCatalogWeight(
    total: total,
    byComponent: <String, int>{
      for (final entry in sorted) entry.key: entry.value,
    },
    shared: total - componentTotal,
  );
}