notationGlossaryComponentLine function

String notationGlossaryComponentLine(
  1. DesignSystemComponent component
)

component as the glossary of signatures lists it: Counter "label" "detail" @value min max step=1 icon onChange : number raised and lowered with minus and plus.

Implementation

String notationGlossaryComponentLine(DesignSystemComponent component) {
  final spec = component.spec;
  final strings = notationStrings(spec);
  final held = spec.props.where((p) => p.type.isVariable && p.key == 'value').firstOrNull ?? spec.props.where((p) => p.type.isVariable).firstOrNull;
  String kind(PropDef<Object?> p) {
    final v = p.defaultValue;
    switch (p.type.kind) {
      case PropKind.boolean:
        return '=${v == true}';
      case PropKind.integer || PropKind.number:
        return v is num ? '=${v == v.roundToDouble() ? v.round() : v}' : '';
      case PropKind.enumeration:
        final values = p.type.values;
        return values.length <= 4 && values.join('|').length <= 30 ? '=${values.join('|')}' : (v is String && v.isNotEmpty ? '=$v' : '');
      case PropKind.list:
        return '=[]';
      case PropKind.object || PropKind.json:
        return v is List ? '=[]' : (v is Map ? '={}' : '');
      case PropKind.value:
        return p.type.name == 'Color' ? '=#…' : '';
      case PropKind.string:
        return '';
    }
  }

  final parts = [
    spec.type,
    for (final k in strings) '"$k"',
    if (held != null) '@${held.key}',
    for (final p in spec.props)
      if (!strings.contains(p.key) && p.key != held?.key) p.type.isVariable ? '${p.key}=@' : (p.required ? '${p.key}!' : '${p.key}${kind(p)}'),
    for (final e in spec.events) e.key == 'onChanged' ? 'onChange' : e.key,
    if (spec.children == ChildModel.list || spec.slots.isNotEmpty) '...',
  ];
  return '${parts.join(' ')} : ${_gist(component.purpose)}';
}