Skip to content
This repository was archived by the owner on Oct 12, 2024. It is now read-only.

Commit 91eb3b8

Browse files
committed
Organize source code structure and refactor
1 parent f9a1999 commit 91eb3b8

File tree

16 files changed

+148
-6
lines changed

16 files changed

+148
-6
lines changed

CHANGELOG.md

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
- Released @ 2024-06-23 04:28:16.242352Z
66
- Fix exports
7-
- Organize source code structure and refactor
87

98
## [0.57.0]
109

lib/src/_all_src.g.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ export 'location_utils/src/celestial_radius.dart';
5555
export 'location_utils/src/distance_unit.dart';
5656
export 'location_utils/src/location_utils.dart';
5757
export 'location_utils/src/t_location_components.dart';
58-
export 'patterns/src/extract_scopes.dart';
59-
export 'patterns/src/recursive_replace.dart';
60-
export 'patterns/src/replace_data.dart';
61-
export 'patterns/src/replace_patterns.dart';
58+
export 'string_patterns/src/extract_scopes.dart';
59+
export 'string_patterns/src/recursive_replace.dart';
60+
export 'string_patterns/src/replace_data.dart';
61+
export 'string_patterns/src/replace_patterns.dart';
6262
export 'streams/src/polling_stream.dart';
6363
export 'streams/src/stream_to_future.dart';
6464
export 'streams/src/to_future_on_stream_extension.dart';

lib/src/collections/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# XYZ Utils - Collections
2+
3+
## Description
4+
5+
A set of utilities related to Dart collections.

lib/src/core/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# XYZ Utils - Core
2+
3+
## Description
4+
5+
A set of core utilities to expand Dart.

lib/src/experimental/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# XYZ Utils - Experimental
2+
3+
## Description
4+
5+
A set of experimental utilities, not fit for release.

lib/src/http/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# XYZ Utuls - HTTP
2+
3+
## Description
4+
5+
A set of utilities related to HTTP.

lib/src/location_utils/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# XYZ Utils - Location Utils
2+
3+
## Description
4+
5+
A set of utilities related to GPS location.

lib/src/streams/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# XYZ Utils - Streams
2+
3+
## Description
4+
5+
A set of utilities related to Dart Streams.

lib/src/string_patterns/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# XYZ Utils - String Patterns
2+
3+
## Description
4+
5+
A set of utilities related to String patterns or regular expressions.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//.title
2+
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
3+
//
4+
// 🇽🇾🇿 & Dev
5+
//
6+
// Copyright Ⓒ Robert Mollentze, xyzand.dev
7+
//
8+
// Licensing details can be found in the LICENSE file in the root directory.
9+
//
10+
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
11+
//.title~
12+
13+
/// Replaces placeholders in a string with corresponding values from a provided
14+
/// map, supporting default values and custom delimiters.
15+
String replacePatterns(
16+
String input,
17+
Map data, {
18+
ReplacePatternsSettings settings = const ReplacePatternsSettings(),
19+
}) {
20+
var output = input;
21+
final regex = RegExp(
22+
'${RegExp.escape(settings.opening)}(.*?)${RegExp.escape(settings.closing)}',
23+
);
24+
final matches = regex.allMatches(input);
25+
for (final match in matches) {
26+
final fullMatch = match.group(0)!;
27+
final keyWithDefault = match.group(1)!;
28+
final parts = keyWithDefault.split(settings.delimiter);
29+
final e0 = parts.elementAtOrNull(0);
30+
final e1 = parts.elementAtOrNull(1);
31+
final key = (e1 ?? e0)!;
32+
final defaultValue = e0 ?? key;
33+
final data1 =
34+
settings.caseSensitive ? data : data.map((k, v) => MapEntry(k.toString().toLowerCase(), v));
35+
final key1 = settings.caseSensitive ? key : key.toLowerCase();
36+
final suggestedReplacementValue = data1[key1];
37+
final replacementValue =
38+
settings.callback?.call(key, suggestedReplacementValue, defaultValue) ??
39+
suggestedReplacementValue?.toString() ??
40+
defaultValue;
41+
output = output.replaceFirst(fullMatch, replacementValue);
42+
}
43+
44+
return output;
45+
}
46+
47+
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
48+
49+
extension ReplaceAllPatternsOnStringExtension on String {
50+
/// Replaces placeholders in this string with corresponding values from a
51+
/// provided map, supporting default values and custom delimiters.
52+
String replacePatterns(
53+
Map data, {
54+
ReplacePatternsSettings settings = const ReplacePatternsSettings(),
55+
}) {
56+
return _replacePatterns(
57+
this,
58+
data,
59+
settings: settings,
60+
).toString();
61+
}
62+
}
63+
64+
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
65+
66+
const _replacePatterns = replacePatterns;
67+
68+
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
69+
70+
class ReplacePatternsSettings {
71+
//
72+
//
73+
//
74+
75+
final String opening;
76+
final String closing;
77+
final String separator;
78+
final String delimiter;
79+
final bool caseSensitive;
80+
final String? Function(
81+
String key,
82+
dynamic suggestedReplacementValue,
83+
String defaultValue,
84+
)? callback;
85+
86+
//
87+
//
88+
//
89+
90+
const ReplacePatternsSettings({
91+
this.opening = '<<<',
92+
this.closing = '>>>',
93+
this.separator = '.',
94+
this.delimiter = '||',
95+
this.caseSensitive = true,
96+
this.callback,
97+
});
98+
}

lib/src/strings/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# XYZ Utils - Strings
2+
3+
## Description
4+
5+
A set of utilities related to Dart Strings.

lib/src/time/src/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# XYZ Utils - Time
2+
3+
## Description
4+
5+
A set of utilities related to Time or Duration.

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
name: xyz_utils
1414
description: This package provides a set of utilities that are commonly used in Dart apps. It is also the foundation for other packages in the XYZ ecosystem.
1515
repository: https://github.com/robmllze/xyz_utils
16-
version: 0.57.1
16+
version: 0.58.0
1717

1818
## -----------------------------------------------------------------------------
1919

0 commit comments

Comments
 (0)