2020-06-03 23:02:21 +02:00
|
|
|
library _youtube_explode.extensions;
|
|
|
|
|
2020-04-18 23:22:13 +02:00
|
|
|
import '../reverse_engineering/cipher/cipher_operations.dart';
|
2020-02-20 19:50:10 +01:00
|
|
|
|
|
|
|
/// Utility for Strings.
|
|
|
|
extension StringUtility on String {
|
2020-06-03 13:18:37 +02:00
|
|
|
/// Returns null if this string is whitespace.
|
|
|
|
String get nullIfWhitespace => trim().isEmpty ? null : this;
|
|
|
|
|
2020-02-20 19:50:10 +01:00
|
|
|
/// Returns true if the string is null or empty.
|
|
|
|
bool get isNullOrWhiteSpace {
|
|
|
|
if (this == null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (trim().isEmpty) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-03 13:18:37 +02:00
|
|
|
/// Returns null if this string is a whitespace.
|
|
|
|
String substringUntil(String separator) => substring(0, indexOf(separator));
|
|
|
|
|
|
|
|
///
|
|
|
|
String substringAfter(String separator) =>
|
2020-06-05 16:17:08 +02:00
|
|
|
substring(indexOf(separator) + separator.length);
|
2020-06-03 13:18:37 +02:00
|
|
|
|
2020-02-20 19:50:10 +01:00
|
|
|
static final _exp = RegExp(r'\D');
|
|
|
|
|
|
|
|
/// Strips out all non digit characters.
|
2020-06-03 13:18:37 +02:00
|
|
|
String stripNonDigits() => replaceAll(_exp, '');
|
2020-10-27 14:44:11 +01:00
|
|
|
|
|
|
|
///
|
|
|
|
String extractJson() {
|
|
|
|
var buffer = StringBuffer();
|
|
|
|
var depth = 0;
|
|
|
|
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
var ch = this[i];
|
|
|
|
var chPrv = i > 0 ? this[i - 1] : '';
|
|
|
|
|
|
|
|
buffer.write(ch);
|
|
|
|
|
|
|
|
if (ch == '{' && chPrv != '\\') {
|
|
|
|
depth++;
|
|
|
|
} else if (ch == '}' && chPrv != '\\') {
|
|
|
|
depth--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (depth == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
2020-02-20 19:50:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// List decipher utility.
|
2020-06-03 13:18:37 +02:00
|
|
|
extension ListDecipher on Iterable<CipherOperation> {
|
2020-03-20 11:26:52 +01:00
|
|
|
/// Apply every CipherOperation on the [signature]
|
2020-02-20 19:50:10 +01:00
|
|
|
String decipher(String signature) {
|
|
|
|
for (var operation in this) {
|
|
|
|
signature = operation.decipher(signature);
|
|
|
|
}
|
|
|
|
|
|
|
|
return signature;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// List Utility.
|
2020-06-13 22:54:53 +02:00
|
|
|
extension ListFirst<E> on Iterable<E> {
|
2020-02-20 19:50:10 +01:00
|
|
|
/// Returns the first element of a list or null if empty.
|
|
|
|
E get firstOrNull {
|
|
|
|
if (length == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 13:18:37 +02:00
|
|
|
|
|
|
|
/// Uri utility
|
|
|
|
extension UriUtility on Uri {
|
|
|
|
/// Returns a new Uri with the new query parameters set.
|
|
|
|
Uri setQueryParam(String key, String value) {
|
|
|
|
var query = Map<String, String>.from(queryParameters);
|
|
|
|
|
|
|
|
query[key] = value;
|
|
|
|
|
|
|
|
return replace(queryParameters: query);
|
|
|
|
}
|
|
|
|
}
|
2020-06-05 16:17:08 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
extension GetOrNull<K, V> on Map<K, V> {
|
2020-06-05 20:08:04 +02:00
|
|
|
/// Get a value from a map
|
2020-06-05 16:17:08 +02:00
|
|
|
V getValue(K key) {
|
|
|
|
var v = this[key];
|
|
|
|
if (v == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
extension GetOrNullMap on Map {
|
2020-06-05 20:08:04 +02:00
|
|
|
/// Get a map inside a map
|
2020-06-05 16:17:08 +02:00
|
|
|
Map<String, dynamic> get(String key) {
|
|
|
|
var v = this[key];
|
|
|
|
if (v == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|