instanceMemberGet method

  1. @override
dynamic instanceMemberGet(
  1. dynamic instance,
  2. String id, {
  3. bool ignoreUndefined = false,
})
override

Default HTExternalClass constructor. Fetch a instance member of the Dart class by the id, in the form of

object.key

Implementation

@override
dynamic instanceMemberGet(dynamic instance, String id,
    {bool ignoreUndefined = false}) {
  switch (id) {
    case 'toString':
      return ({object, positionalArgs, namedArgs}) => object.toString();
    case 'toJSON':
      return ({object, positionalArgs, namedArgs}) => jsonifyList(object);
    case 'random':
      final random = math.Random();
      if (instance.isNotEmpty) {
        return instance.elementAt(random.nextInt(instance.length));
      } else {
        return null;
      }
    case 'iterator':
      return instance.iterator;
    case 'map':
      return ({object, positionalArgs, namedArgs}) {
        HTFunction func = positionalArgs.first;
        return object.map((element) {
          return func.call(positionalArgs: [element]);
        });
      };
    case 'where':
      return ({object, positionalArgs, namedArgs}) {
        HTFunction func = positionalArgs.first;
        return object.where((element) {
          return func.call(positionalArgs: [element]) as bool;
        });
      };
    case 'expand':
      return ({object, positionalArgs, namedArgs}) {
        HTFunction func = positionalArgs.first;
        return instance.expand((element) {
          return func.call(positionalArgs: [element]) as Iterable;
        });
      };
    case 'contains':
      return ({object, positionalArgs, namedArgs}) =>
          object.contains(positionalArgs.first);
    case 'reduce':
      return ({object, positionalArgs, namedArgs}) {
        HTFunction func = positionalArgs.first;
        return object.reduce((value, element) {
          return func.call(positionalArgs: [value, element]);
        });
      };
    case 'fold':
      return ({object, positionalArgs, namedArgs}) {
        final initialValue = positionalArgs[0];
        HTFunction func = positionalArgs[1];
        return object.fold(initialValue, (value, element) {
          return func.call(positionalArgs: [value, element]);
        });
      };
    case 'every':
      return ({object, positionalArgs, namedArgs}) {
        HTFunction func = positionalArgs.first;
        return object.every((element) {
          return func.call(positionalArgs: [element]) as bool;
        });
      };
    case 'join':
      return ({object, positionalArgs, namedArgs}) =>
          object.join(positionalArgs.first);
    case 'any':
      return ({object, positionalArgs, namedArgs}) {
        HTFunction func = positionalArgs.first;
        return object.any((element) {
          return func.call(positionalArgs: [element]) as bool;
        });
      };
    case 'toList':
      return ({object, positionalArgs, namedArgs}) => object.toList();
    case 'toSet':
      return ({object, positionalArgs, namedArgs}) => object.toSet();
    case 'length':
      return instance.length;
    case 'isEmpty':
      return instance.isEmpty;
    case 'isNotEmpty':
      return instance.isNotEmpty;
    case 'take':
      return ({object, positionalArgs, namedArgs}) =>
          object.take(positionalArgs.first);
    case 'takeWhile':
      return ({object, positionalArgs, namedArgs}) {
        HTFunction func = positionalArgs.first;
        return object.takeWhile((element) {
          return func.call(positionalArgs: [element]) as bool;
        });
      };
    case 'skip':
      return ({object, positionalArgs, namedArgs}) =>
          object.skip(positionalArgs.first);
    case 'skipWhile':
      return ({object, positionalArgs, namedArgs}) {
        HTFunction func = positionalArgs.first;
        return object.skipWhile((element) {
          return func.call(positionalArgs: [element]) as bool;
        });
      };
    case 'first':
      return instance.isNotEmpty ? instance.first : null;
    case 'last':
      return instance.isNotEmpty ? instance.last : null;
    case 'single':
      return instance.single;
    case 'firstWhere':
      return ({object, positionalArgs, namedArgs}) {
        HTFunction func = positionalArgs.first;
        HTFunction? orElse = namedArgs['orElse'];
        return object.firstWhere((element) {
          return func.call(positionalArgs: [element]) as bool;
        }, orElse: () {
          return orElse != null ? orElse() : null;
        });
      };
    case 'lastWhere':
      return ({object, positionalArgs, namedArgs}) {
        HTFunction func = positionalArgs.first;
        HTFunction? orElse = namedArgs['orElse'];
        return object.lastWhere((element) {
          return func.call(positionalArgs: [element]) as bool;
        }, orElse: () {
          return orElse != null ? orElse() : null;
        });
      };
    case 'singleWhere':
      return ({object, positionalArgs, namedArgs}) {
        HTFunction func = positionalArgs.first;
        HTFunction? orElse = namedArgs['orElse'];
        return object.singleWhere((element) {
          return func.call(positionalArgs: [element]) as bool;
        }, orElse: () {
          return orElse != null ? orElse() : null;
        });
      };
    case 'elementAt':
      return ({object, positionalArgs, namedArgs}) =>
          object.elementAt(positionalArgs.first);
    default:
      throw HTError.undefined(id);
  }
}