getAccessorsForType function

List<Accessor> getAccessorsForType(
  1. VariableType type
)

Defines accessors for a variable based on its type.

Implementation

List<Accessor> getAccessorsForType(VariableType type) {
  switch (type) {
    case VariableType.text:
      return [
        Accessor(
          name: 'length',
          type: VariableType.integer,
          getValue: (value) => value?.toString().length,
        ),
        Accessor(
          name: 'upcase',
          type: VariableType.text,
          getValue: (value) => value?.toString().toUpperCase(),
        ),
        Accessor(
          name: 'downcase',
          type: VariableType.text,
          getValue: (value) => value?.toString().toUpperCase(),
        ),
        Accessor(
          name: 'capitalize',
          type: VariableType.text,
          getValue: (value) => value?.toString().capitalized,
        ),
        Accessor(
          name: 'isEmpty',
          type: VariableType.boolean,
          getValue: (value) => value?.toString().isEmpty,
        ),
        Accessor(
          name: 'isNotEmpty',
          type: VariableType.boolean,
          getValue: (value) => value?.toString().isNotEmpty,
        ),
        Accessor(
          name: 'isBlank',
          type: VariableType.boolean,
          getValue: (value) => value?.toString().trim().isEmpty,
        ),
      ];
    case VariableType.integer:
      return [
        Accessor(
          name: 'isEven',
          type: VariableType.boolean,
          getValue: (value) => value?.toInt()?.isEven,
        ),
        Accessor(
          name: 'isOdd',
          type: VariableType.boolean,
          getValue: (value) => value?.toInt()?.isOdd,
        ),
        Accessor(
          name: 'isFinite',
          type: VariableType.boolean,
          getValue: (value) => value?.toInt()?.isFinite,
        ),
        Accessor(
          name: 'isInfinite',
          type: VariableType.boolean,
          getValue: (value) => value?.toInt()?.isInfinite,
        ),
        Accessor(
          name: 'isNegative',
          type: VariableType.boolean,
          getValue: (value) => value?.toInt()?.isNegative,
        ),
        Accessor(
          name: 'isPositive',
          type: VariableType.boolean,
          getValue: (value) {
            final int? parsedValue = value?.toInt();
            if (parsedValue == null) return null;
            return !parsedValue.isNegative;
          },
        ),
      ];
    case VariableType.decimal:
      return [
        Accessor(
          name: 'whole',
          type: VariableType.integer,
          getValue: (value) => value?.toDouble()?.toInt(),
        ),
        Accessor(
          name: 'floor',
          type: VariableType.integer,
          getValue: (value) => value?.toDouble()?.floor(),
        ),
        Accessor(
          name: 'ceil',
          type: VariableType.integer,
          getValue: (value) => value?.toDouble()?.ceil(),
        ),
        Accessor(
          name: 'isFinite',
          type: VariableType.boolean,
          getValue: (value) => value?.toDouble()?.isFinite,
        ),
        Accessor(
          name: 'isInfinite',
          type: VariableType.boolean,
          getValue: (value) => value?.toDouble()?.isInfinite,
        ),
        Accessor(
          name: 'isNegative',
          type: VariableType.boolean,
          getValue: (value) => value?.toDouble()?.isNegative,
        ),
        Accessor(
          name: 'isPositive',
          type: VariableType.boolean,
          getValue: (value) {
            final newValue = value?.toDouble();
            if (newValue == null) return null;
            return !newValue.isNegative;
          },
        ),
        Accessor(
          name: 'isNaN',
          type: VariableType.boolean,
          getValue: (value) => value?.toDouble()?.isNaN,
        ),
      ];
    case VariableType.boolean:
      return [
        LeafAccessor(
          name: 'toggled',
          type: VariableType.boolean,
          getValue: (value) => !(value?.toBool() ?? false),
        ),
      ];
    case VariableType.map:
      return [
        Accessor(
          name: 'length',
          type: VariableType.integer,
          getValue: (value) => value?.toMap()?.length,
        ),
        Accessor(
          name: 'keys',
          type: VariableType.list,
          getValue: (value) => value?.toMap()?.keys,
        ),
        Accessor(
          name: 'values',
          type: VariableType.list,
          getValue: (value) => value?.toMap()?.values,
        ),
        Accessor(
          name: 'isEmpty',
          type: VariableType.boolean,
          getValue: (value) {
            final newValue = value?.toMap();
            if (newValue == null) return null;
            return newValue.isEmpty;
          },
        ),
        Accessor(
          name: 'isNotEmpty',
          type: VariableType.boolean,
          getValue: (value) {
            final newValue = value?.toMap();
            if (newValue == null) return null;
            return newValue.isNotEmpty;
          },
        ),
      ];
    case VariableType.list:
      return [
        Accessor(
          name: 'length',
          type: VariableType.integer,
          getValue: (value) => value?.toList()?.length,
        ),
        Accessor(
          name: 'isEmpty',
          type: VariableType.boolean,
          getValue: (value) {
            final newValue = value?.toMap();
            if (newValue == null) return null;
            return newValue.isEmpty;
          },
        ),
        Accessor(
          name: 'isNotEmpty',
          type: VariableType.boolean,
          getValue: (value) {
            final newValue = value?.toMap();
            if (newValue == null) return null;
            return newValue.isNotEmpty;
          },
        ),
      ];
    case VariableType.color:
      return [
        Accessor(
          name: 'red',
          type: VariableType.integer,
          getValue: (value) {
            final newValue = value.toColorRGBA();
            if (newValue == null) return null;
            return (newValue.r * 255).toInt();
          },
        ),
        Accessor(
          name: 'green',
          type: VariableType.integer,
          getValue: (value) {
            final newValue = value.toColorRGBA();
            if (newValue == null) return null;
            return (newValue.g * 255).toInt();
          },
        ),
        Accessor(
          name: 'blue',
          type: VariableType.integer,
          getValue: (value) {
            final newValue = value.toColorRGBA();
            if (newValue == null) return null;
            return (newValue.b * 255).toInt();
          },
        ),
        Accessor(
          name: 'alpha',
          type: VariableType.integer,
          getValue: (value) {
            final newValue = value.toColorRGBA();
            if (newValue == null) return null;
            return (newValue.a * 255).toInt();
          },
        ),
        Accessor(
          name: 'opacity',
          type: VariableType.integer,
          getValue: (value) {
            final newValue = value.toColorRGBA();
            if (newValue == null) return null;
            return (newValue.a).toInt();
          },
        ),
      ];
  }
}