detectNamingConvention method

NamingConvention? detectNamingConvention(
  1. Map json
)

Detects the naming convention used in a JSON object. Returns the first matching convention, or null if no match is found.

Implementation

NamingConvention? detectNamingConvention(Map<dynamic, dynamic> json) {
  if (json.isEmpty) return null;

  // Count matches for each convention
  final scores = <NamingConvention, int>{};
  for (var convention in namingConventions) {
    scores[convention] = 0;
  }

  for (var key in json.keys) {
    if (key is! String) continue;

    for (var convention in namingConventions) {
      if (convention.matches(key)) {
        scores[convention] = (scores[convention] ?? 0) + 1;
      }
    }
  }

  // Return the convention with the highest score
  var maxScore = 0;
  NamingConvention? bestConvention;
  scores.forEach((convention, score) {
    if (score > maxScore) {
      maxScore = score;
      bestConvention = convention;
    }
  });

  return bestConvention;
}