Line data Source code
1 : part of flutter_data; 2 : 3 : extension IterableX<T> on Iterable<T> { 4 1 : @protected 5 : @visibleForTesting 6 2 : T? get safeFirst => isNotEmpty ? first : null; 7 : 8 1 : @protected 9 : @visibleForTesting 10 2 : bool containsFirst(T model) => safeFirst == model; 11 : 12 1 : @protected 13 : @visibleForTesting 14 1 : List<T> toImmutableList() => List.unmodifiable(this); 15 : } 16 : 17 : extension _DataModelListX on Iterable<DataModel> { 18 1 : String toShortLog() { 19 4 : final ids = map((m) => m.id).toSet(); 20 1 : return ids.isEmpty 21 : ? 'none' 22 2 : : (ids.length > 5 23 4 : ? '${ids.take(5).toSet()} (and ${ids.length - 5} more)' 24 1 : : ids) 25 1 : .toString(); 26 : } 27 : } 28 : 29 : extension IterableNullX<T> on Iterable<T?> { 30 1 : @protected 31 : @visibleForTesting 32 3 : Iterable<T> get filterNulls => where((elem) => elem != null).cast(); 33 : } 34 : 35 : extension StringUtilsX on String { 36 1 : String capitalize() => 37 5 : isEmpty ? '' : '${this[0].toUpperCase()}${substring(1)}'; 38 : 39 1 : String decapitalize() => 40 5 : isEmpty ? '' : '${this[0].toLowerCase()}${substring(1)}'; 41 : 42 2 : String pluralize() => inflection.pluralize(this); 43 : 44 2 : String singularize() => inflection.singularize(this); 45 : 46 2 : Uri get asUri => Uri.parse(this); 47 : 48 1 : String namespaceWith(String prefix) { 49 : assert(!prefix.contains(':')); 50 1 : return '$prefix:$this'; 51 : } 52 : 53 1 : String typifyWith(String type) { 54 : assert(!type.contains('#')); 55 1 : if (isEmpty) { 56 : return type; 57 : } 58 1 : return '$type#$this'; 59 : } 60 : 61 1 : String denamespace() { 62 : // need to re-join with : in case there were other :s in the text 63 3 : return (split(':')..removeAt(0)).join(':'); 64 : } 65 : 66 1 : String detypify() { 67 : // need to re-join with # in case there were other #s in the id 68 3 : return (split('#')..removeAt(0)).join('#'); 69 : } 70 : } 71 : 72 : extension MapUtilsX<K, V> on Map<K, V> { 73 1 : @protected 74 : @visibleForTesting 75 3 : Map<K, V> operator &(Map<K, V>? more) => {...this, ...?more}; 76 : 77 1 : @protected 78 : @visibleForTesting 79 1 : Map<K, V> get filterNulls => { 80 1 : for (final e in entries) 81 4 : if (e.value != null) e.key: e.value 82 : }; 83 : } 84 : 85 : extension UriUtilsX on Uri { 86 1 : Uri operator /(String path) { 87 5 : return replace(path: path_helper.posix.normalize('/${this.path}/$path')); 88 : } 89 : 90 2 : Uri operator &(Map<String, dynamic> params) => params.isNotEmpty 91 1 : ? replace( 92 3 : queryParameters: queryParameters & _flattenQueryParameters(params)) 93 : : this; 94 : } 95 : 96 1 : Map<String, String> _flattenQueryParameters(Map<String, dynamic> params) { 97 4 : return params.entries.fold<Map<String, String>>({}, (acc, e) { 98 2 : if (e.value is Map<String, dynamic>) { 99 3 : for (final e2 in (e.value as Map<String, dynamic>).entries) { 100 6 : acc['${e.key}[${e2.key}]'] = e2.value.toString(); 101 : } 102 : } else { 103 4 : acc[e.key] = e.value.toString(); 104 : } 105 : return acc; 106 : }); 107 : }