Skip to content

Commit 377f8f0

Browse files
committed
refactor: rename class Rand to RandOf; removal of RandDig and RandHex
Closes #31 Breaking Changes
1 parent 4729b9c commit 377f8f0

15 files changed

+102
-180
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ and this project adheres to [Dart Package Versioning](https://dart.dev/tools/pub
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- classes RandText, RandTextSrc, and RandTextLen — [29](https://github.com/dartoos-dev/dartoos/issues/29).
13+
14+
### Changed
15+
16+
- rename class Rand to RandOf — **Breaking Change**.
17+
18+
### Removed
19+
20+
- RandDig and RandHex classes — **Breaking Change**s.
21+
1022
## [0.3.1] - 2021-12-15
1123

1224
### Added

example/crypto/hash/hmac_sha256_benchmark.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void main() {
2020
const len = 25000000;
2121
const alphabet =
2222
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
23-
final data = BytesOf.text(Rand(len, alphabet)).value;
23+
final data = BytesOf.text(RandOf(alphabet, len)).value;
2424
final key = BytesOf.utf8('Dartoos vs Crypto').value;
2525
print('\nLength of the data to be hashed: ${data.lengthInBytes} bytes.');
2626

example/crypto/hash/sha256_benchmark.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void main() {
2121
const len = 25000000;
2222
const alphabet =
2323
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
24-
final data = BytesOf.text(Rand(len, alphabet)).value;
24+
final data = BytesOf.text(RandOf(alphabet, len)).value;
2525
print('\nLength of the data to be hashed: ${data.lengthInBytes} bytes.');
2626

2727
print('\n--- Hashing elapsed times ---');

example/crypto/hash/sha512_benchmark.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void main() {
2121
const len = 25000000;
2222
const alphabet =
2323
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
24-
final data = BytesOf.text(Rand(len, alphabet)).value;
24+
final data = BytesOf.text(RandOf(alphabet, len)).value;
2525
print('\nLength of the data to be hashed: ${data.lengthInBytes} bytes.');
2626

2727
print('\n--- SHA-512 — Elapsed times for hasing ---');

example/encoding/base64_benchmark.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void main() {
2626
const len = 50000000;
2727
const alphabet =
2828
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
29-
final bytes = dartoos.BytesOf.text(dartoos.Rand(len, alphabet)).value;
29+
final bytes = dartoos.BytesOf.text(dartoos.RandOf(alphabet, len)).value;
3030
print('\nLength of the data to be encoded: ${bytes.length} bytes.');
3131

3232
print('\n--- Encoding elapsed times ---');

example/rand/rand_benchmark.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import 'dart:math';
44

5-
import 'package:dartoos/src/rand/rand.dart';
5+
import 'package:dartoos/rand.dart';
66

77
/// Dartoos random text generator vs. Dart's built-in generator.
88
///
@@ -37,15 +37,14 @@ void main() {
3737

3838
print('Start...');
3939
final watch = Stopwatch()..start();
40-
final dartText = randText(len, src, index);
40+
final dartText = randText(src, len, index);
4141
final elapsedDartTime = watch.elapsedMilliseconds;
4242
print('\nDart elapsed time....: $elapsedDartTime milliseconds');
4343
watch.stop();
4444
watch.reset();
4545

4646
watch.start();
47-
final rand = Rand(len, src, index);
48-
final dartoosText = rand();
47+
final dartoosText = RandOf(src, len, index).value;
4948
final elapsedDartoosTime = watch.elapsedMilliseconds;
5049
print('Dartoos elapsed time.: $elapsedDartoosTime milliseconds');
5150
watch.stop();
@@ -58,7 +57,7 @@ void main() {
5857
}
5958

6059
/// Common approach to generate random Strings in Dart.
61-
String randText(int len, String chars, Random index) {
60+
String randText(String chars, int len, Random index) {
6261
final buffer = StringBuffer();
6362
for (var i = 0; i < len; ++i) {
6463
buffer.write(chars[index.nextInt(chars.length)]);

lib/rand.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/// Set of classes that generate random data — Randomizers.
22
library rand;
33

4-
export 'src/rand/rand.dart';
5-
export 'src/rand/rand_dig.dart';
6-
export 'src/rand/rand_hex.dart';
4+
export 'src/rand/rand_of.dart';
75
export 'src/rand/rand_text.dart';

lib/src/rand/rand_dig.dart

Lines changed: 0 additions & 24 deletions
This file was deleted.

lib/src/rand/rand_hex.dart

Lines changed: 0 additions & 29 deletions
This file was deleted.

lib/src/rand/rand.dart renamed to lib/src/rand/rand_of.dart

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,45 @@ import '../text/text.dart';
77
/// eligible characters.
88
///
99
/// For a cryptographically secure random number, see [Rand.secure].
10-
class Rand with Text {
10+
class RandOf with Text {
1111
/// Texts of length [len] with randomly selected characters from [src].
1212
///
1313
/// [len] >= 0; src must not be empty.
1414
///
15-
/// For a cryptographically secure random number generator, see [Rand.secure].
16-
Rand(int len, String src, [Random? index])
15+
/// For a cryptographically secure random number generator, see [R.secure].
16+
RandOf(String src, int len, [Random? index])
1717
: assert(len >= 0, 'Error: negative length.'),
1818
assert(src.isNotEmpty, 'Error: empty source of eligible characters.'),
1919
_len = len,
2020
_src = src,
2121
_index = index ?? Random();
2222

23-
/// Cryptographically secure texts of length [len] with randomly selected
23+
/// Fixed-length texts with randomly selected digits [0–9].
24+
///
25+
/// This is the ideal class for generating verification codes.
26+
///
27+
/// ```dart
28+
/// final fourDigitCode = RandOf.dig(4).value;
29+
/// ```
30+
RandOf.dig(int len, [Random? index]) : this('0123456789', len, index);
31+
32+
/// Fixed-length strings with randomly selected hex digits [0–9a–f].
33+
RandOf.hex(int len, [Random? index]) : this('0123456789abcdef', len, index);
34+
35+
/// Cryptographically secure strings of length [len] with randomly selected
2436
/// characters from [src].
2537
///
2638
/// [len] >= 0; src must not be empty.
2739
///
2840
/// It uses an instance of [Random.secure] as the index randomizer.
29-
Rand.secure(int len, String src) : this(len, src, Random.secure());
41+
RandOf.secure(String src, int len) : this(src, len, Random.secure());
3042

31-
final int _len;
3243
final String _src;
44+
final int _len;
3345
final Random _index;
3446

35-
/// Generates fixed-length texts with randomly selected characters from the
36-
/// source of eligible characters.
47+
/// Returns a fixed-length [String] with randomly selected characters from the
48+
/// predefined source of elegible characters.
3749
@override
3850
String call() {
3951
final Uint16List codes = Uint16List(_len);

0 commit comments

Comments
 (0)