Skip to content

Commit

Permalink
v1.0.9
Browse files Browse the repository at this point in the history
- Added extensions:
  - `String: `encodeLatin1`, `encodeUTF8`, `truncate`.
  - `Uint8List`: `copyAsUnmodifiable`, `asUnmodifiableView`, `toStringLatin1/bytes`, `toStringUTF8/bytes`,
     `setUint8/16/32/64`, `setInt8/16/32/64`.
  - `List<int>`: `toUint8List`, `asUint8List`, `compareWith`.
  - `int`: `isSafeInteger`, `checkSafeInteger`, `int16/32/64ToBytes`, `uInt16/32/64ToBytes`.
  - `BigInt`: `isSafeInteger`, `checkSafeInteger`.
- Improved documentation.
- Fix typo: renamed extension with `UInt` to `Uint` to follow Dart style.
  • Loading branch information
gmpassos committed Dec 16, 2021
1 parent fc44920 commit fdcb877
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- `Uint8List`: `copyAsUnmodifiable`, `asUnmodifiableView`, `toStringLatin1/bytes`, `toStringUTF8/bytes`,
`setUint8/16/32/64`, `setInt8/16/32/64`.
- `List<int>`: `toUint8List`, `asUint8List`, `compareWith`.
- `int`: `isSafeInteger`, `checkSafeInteger`.
- `int`: `isSafeInteger`, `checkSafeInteger`, `int16/32/64ToBytes`, `uInt16/32/64ToBytes`.
- `BigInt`: `isSafeInteger`, `checkSafeInteger`.
- Improved documentation.
- Fix typo: renamed extension with `UInt` to `Uint` to follow Dart style.
Expand Down
22 changes: 22 additions & 0 deletions lib/src/statistics_extension_num.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,28 @@ extension IntExtension on int {
var s = toString();
return s.numericalPadLeft(width);
}
/// Converts `this` as a `Int16` to bytes ([Uint8List]).
Uint8List int16ToBytes() =>
Uint8List(2)..asByteData().setInt16(0, this, Endian.big);
/// Converts `this` as a `Uint16` to bytes ([Uint8List]).
Uint8List uInt16ToBytes() =>
Uint8List(2)..asByteData().setUint16(0, this, Endian.big);
/// Converts `this` as a `Int32` to bytes ([Uint8List]).
Uint8List int32ToBytes() =>
Uint8List(4)..asByteData().setInt32(0, this, Endian.big);
/// Converts `this` as a `Uint32` to bytes ([Uint8List]).
Uint8List uInt32ToBytes() =>
Uint8List(4)..asByteData().setUint32(0, this, Endian.big);
/// Converts `this` as a `Int64` to bytes ([Uint8List]).
Uint8List int64ToBytes() => Uint8List(8)..setInt64(this);
/// Converts `this` as a `Uint64` to bytes ([Uint8List]).
Uint8List uInt64ToBytes() => Uint8List(8)..setUint64(this);
}
/// extension for [BigInt].
Expand Down
12 changes: 12 additions & 0 deletions test/statistics_extension_num_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ void main() {
expect((-123).toUint8List32Reversed(),
equals(Uint8List.fromList([133, 255, 255, 255])));

expect(0xFF01.uInt16ToBytes(), equals(Uint8List.fromList([255, 1])));
expect(0xFF01FF01.uInt32ToBytes(),
equals(Uint8List.fromList([255, 1, 255, 1])));
expect(0x01FF01FF01FF01.uInt64ToBytes(),
equals(Uint8List.fromList([0, 1, 255, 1, 255, 1, 255, 1])));

expect(0xFF01.int16ToBytes(), equals(Uint8List.fromList([255, 1])));
expect(0xFF01FF01.int32ToBytes(),
equals(Uint8List.fromList([255, 1, 255, 1])));
expect(0x01FF01FF01FF01.int64ToBytes(),
equals(Uint8List.fromList([0, 1, 255, 1, 255, 1, 255, 1])));

expect((0xFFFFFFFF), equals(4294967295));

expect((0xFFFFFFFF).toUint8List32(),
Expand Down

0 comments on commit fdcb877

Please sign in to comment.