Skip to content

Commit

Permalink
publish:pub.dev (#134)
Browse files Browse the repository at this point in the history
you_dart 0.0.3
you_flutter 0.0.3
  • Loading branch information
chen56 authored Apr 22, 2024
1 parent 99d5c2b commit a644e19
Show file tree
Hide file tree
Showing 28 changed files with 2,308 additions and 201 deletions.
327 changes: 176 additions & 151 deletions bake

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions packages/you_dart/example/you_dart_example.dart

This file was deleted.

108 changes: 108 additions & 0 deletions packages/you_dart/lib/src/core.dart
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)}]';

}
Loading

0 comments on commit a644e19

Please sign in to comment.