file: extensions/objects.dart
dependencies: import 'package:dart_helper_utils/dart_helper_utils.dart';

extension DHUObjectNullableExtensions on Object? {
  dynamic encode({Object? Function(dynamic object)? toEncodable}) =>
      json.encode(this, toEncodable: toEncodable);

  bool get isNull => this == null;

  bool get isNotNull => this != null;

  bool get asBool {
    final self = this;
    if (self == null) return false;
    if (self is bool) return self;
    if (self is num) return self.asBool;
    return self.toString().asBool;
  }

  bool isPrimitive() {
    final value = this;
    if (value is Iterable) return value.isPrimitive();
    if (value is Map) return value.isPrimitive();
    return value is num ||
        value is bool ||
        value is String ||
        value is BigInt ||
        value is DateTime;
  }

  bool get isDouble => double.tryParse(toString()) != null;

  bool get isInt => int.tryParse(toString()) != null;

  bool get isNum => num.tryParse(toString()) != null;
}
