| 1 | | | import '../exceptions/worker_exception.dart'; |
| 2 | | | import '../typedefs.dart'; |
| 3 | | | |
| 4 | | | abstract class Converter { |
| 5 | | 12 | const Converter(); |
| 6 | | | |
| 7 | | 11 | static bool isNumber<T>() => (T == int || T == double); |
| 8 | | | |
| 9 | | 10 | static bool isIdentity<T>(Cast<T> cast) => (cast == identity<T>); |
| 10 | | | |
| 11 | | 20 | static T identity<T>(dynamic x) => (x is T) |
| 12 | | | ? x |
| 13 | | 1 | : throw WorkerException( |
| 14 | | 2 | 'TypeError: ${x.runtimeType} is not a subtype of $T'); |
| 15 | | | |
| 16 | | 8 | static List<T> _castList<T>(dynamic x) => (x is List<T>) |
| 17 | | | ? x |
| 18 | | 2 | : ((x is List) ? x.cast<T>() : (x as Iterable).cast<T>().toList()); |
| 19 | | | |
| 20 | | 2 | static Cast<List<T>> _mapList<T>(Cast<T> op) => |
| 21 | | 6 | (x) => (x as Iterable).map(op).toList(); |
| 22 | | | |
| 23 | | 4 | static Set<T> _castSet<T>(dynamic x) => (x is Set<T>) |
| 24 | | | ? x |
| 25 | | 5 | : ((x is Set) ? x.cast<T>() : (x as Iterable).cast<T>().toSet()); |
| 26 | | | |
| 27 | | 1 | static Cast<Set<T>> _mapSet<T>(Cast<T> op) => |
| 28 | | 3 | (x) => (x as Iterable).map(op).toSet(); |
| 29 | | | |
| 30 | | 4 | static Map<K, V> _castMap<K, V>(dynamic x) => |
| 31 | | 5 | (x is Map<K, V>) ? x : (x as Map).cast<K, V>(); |
| 32 | | | |
| 33 | | 1 | static Cast<Map<K, V>> _mapMap<K, V>(Cast<K> kop, Cast<V> vop) => |
| 34 | | 6 | (x) => (x as Map).map((k, v) => MapEntry(kop(k), vop(v))); |
| 35 | | | |
| 36 | | 3 | static Cast<T?> allowNull<T extends Object>(Cast<T> op) => |
| 37 | | 7 | isIdentity(op) ? identity<T?> : ((x) => (x == null) ? null : op(x)); |
| 38 | | | |
| 39 | | | // non-nullable value |
| 40 | | | Cast<T> value<T extends Object>(); |
| 41 | | | |
| 42 | | | // list (of non-nullable T) |
| 43 | | 4 | Cast<List<T>> list<T extends Object>([Cast<T>? cast]) { |
| 44 | | 4 | final op = cast ?? value<T>(); |
| 45 | | 6 | return isIdentity<T>(op) ? _castList<T> : _mapList(op); |
| 46 | | | } |
| 47 | | | |
| 48 | | | // list (of nullable T) |
| 49 | | 1 | Cast<List<T?>> nlist<T extends Object>([Cast<T>? cast]) { |
| 50 | | 1 | final op = allowNull(cast ?? value<T>()); |
| 51 | | 2 | return isIdentity<T?>(op) ? _castList<T?> : _mapList(op); |
| 52 | | | } |
| 53 | | | |
| 54 | | | // set (of non-nullable T) |
| 55 | | 2 | Cast<Set<T>> set<T extends Object>([Cast<T>? cast]) { |
| 56 | | 2 | final op = cast ?? value<T>(); |
| 57 | | 3 | return isIdentity<T>(op) ? _castSet<T> : _mapSet<T>(op); |
| 58 | | | } |
| 59 | | | |
| 60 | | | // set (of nullable T) |
| 61 | | 1 | Cast<Set<T?>> nset<T extends Object>([Cast<T>? cast]) { |
| 62 | | 2 | var op = allowNull(cast ?? value<T>()); |
| 63 | | 2 | return isIdentity<T?>(op) ? _castSet<T?> : _mapSet<T?>(op); |
| 64 | | | } |
| 65 | | | |
| 66 | | | // map (of non-nullable K, V) |
| 67 | | 3 | Cast<Map<K, V>> map<K extends Object, V extends Object>( |
| 68 | | | {Cast<K>? kcast, Cast<V>? vcast}) { |
| 69 | | 6 | final kop = kcast ?? value<K>(), vop = vcast ?? value<V>(); |
| 70 | | 6 | return (isIdentity<K>(kop) && isIdentity<V>(vop)) |
| 71 | | | ? _castMap<K, V> |
| 72 | | 1 | : _mapMap<K, V>(kop, vop); |
| 73 | | | } |
| 74 | | | |
| 75 | | | // map (of non-nullable K, nullable V) |
| 76 | | 2 | Cast<Map<K, V?>> nmap<K extends Object, V extends Object>( |
| 77 | | | {Cast<K>? kcast, Cast<V>? vcast}) { |
| 78 | | 6 | final kop = kcast ?? value<K>(), vop = allowNull(vcast ?? value<V>()); |
| 79 | | 4 | return (isIdentity<K>(kop) && isIdentity<V?>(vop)) |
| 80 | | | ? _castMap<K, V?> |
| 81 | | 1 | : _mapMap<K, V?>(kop, vop); |
| 82 | | | } |
| 83 | | | } |