-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
you_dart 0.0.3 you_flutter 0.0.3
- Loading branch information
Showing
28 changed files
with
2,308 additions
and
201 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
/// 基础包,不依赖其他业务代码 | ||
Types types = Types(); | ||
|
||
|
||
@experimental | ||
typedef Convert<FROM, TO> = TO Function(FROM from); | ||
|
||
@experimental | ||
base mixin class Types { | ||
// 集合的直接类型转换会报错: | ||
// exception : return params.map((e)=>e.build()).toList() as T | ||
// 可以利用原类型集合复制出来做基础,再填充,然后转型就不会错了。 | ||
R castList<R>({required Iterable from, required List to}) { | ||
assert(to is R, "arg to:$to , type should be $R"); | ||
//copy same type list | ||
var result = to.sublist(0, 0); | ||
// or | ||
// var result = to.toList()..clear(); | ||
|
||
// fill | ||
for (var e in from) { | ||
result.add(e); | ||
} | ||
|
||
//cast, no exception ,because : to is R == true | ||
return result as R; | ||
} | ||
|
||
@experimental | ||
R castSet<R>({required Iterable from, required Set to}) { | ||
assert(to is R, "arg to:$to , type should be $R"); | ||
//copy same type list | ||
var result = to.toSet(); | ||
// or | ||
// var result = to.toList()..clear(); | ||
|
||
// fill | ||
for (var e in from) { | ||
result.add(e); | ||
} | ||
|
||
//cast, no exception ,because : to is R == true | ||
return result as R; | ||
} | ||
|
||
@experimental | ||
String simpleName(Type type) { | ||
String str = type.toString(); | ||
int index = str.indexOf("<"); | ||
if (index < 0) return str; | ||
return str.substring(0, index); | ||
} | ||
} | ||
|
||
extension type StringExt(String str) { | ||
String safeSubstring(int start, [int? end]) { | ||
end ??= str.length; | ||
end = end <= str.length ? end : str.length; | ||
try { | ||
return str.substring(start, end); | ||
} catch (e) { | ||
throw Exception("$e, string $this"); | ||
} | ||
} | ||
} | ||
|
||
/// 范型参数只有在类型内才能看见,比如Map Signal内的key, value类型外部是无法看见的, | ||
/// 只能通过钩子传出来 | ||
@experimental | ||
final class TypeHook<T> { | ||
const TypeHook(); | ||
|
||
bool isType<Super>() { | ||
return <T>[] is List<Super> || <T>[] is List<Super?>; | ||
} | ||
|
||
bool isSuper<Sub>() { | ||
return <Sub>[] is List<T> || <Sub>[] is List<T?>; | ||
} | ||
|
||
/// 判断类型T是否是nullable的: | ||
/// expect(isNullable<int>(), isFalse); | ||
/// expect(isNullable<int?>(), isTrue); | ||
bool isNullable() { | ||
return null is T; | ||
} | ||
|
||
bool isTypeOf(obj) { | ||
return obj is T; | ||
} | ||
} | ||
|
||
@experimental | ||
class Unique { | ||
final String name; | ||
Unique(this.name); | ||
|
||
String shortHash(Object? object) { | ||
return object.hashCode.toUnsigned(20).toRadixString(16).padLeft(5, '0'); | ||
} | ||
|
||
@override | ||
String toString() => '[#$name:${shortHash(this)}]'; | ||
|
||
} |
Oops, something went wrong.