Skip to content

Commit

Permalink
A1Partial fromVector
Browse files Browse the repository at this point in the history
  • Loading branch information
sjhorn committed Apr 7, 2024
1 parent e930c48 commit 8d20515
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.6

- Enhanced A1Partial to accept a vector
- Moved the int to A1 Letters into an extension on int

## 1.0.5

- Added testing is an A1 is in an A1Range with contains method
Expand Down
39 changes: 24 additions & 15 deletions lib/src/a1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,10 @@ class A1 implements Comparable<A1> {
/// a1 = A1.fromVector(1,-1); // FormatException
/// ```
A1.fromVector(int column, int row) {
if (column < 0 || row < 0) {
throw FormatException('column & row must be positive ($column,$row)');
if (row.isNegative) {
throw FormatException('row $row must be positive');
}

final codeUnits = <int>[];
if (column < 26) {
codeUnits.add(65 + column);
} else {
var evaluationIndex = column;
while (evaluationIndex >= 26) {
codeUnits.add(65 + evaluationIndex % 26);
evaluationIndex = (evaluationIndex / 26 - 1).floor();
}
codeUnits.add(65 + evaluationIndex);
}
letters = String.fromCharCodes(codeUnits.reversed);
letters = column.a1Letters;
digits = row + 1;
}

Expand Down Expand Up @@ -227,6 +215,27 @@ class A1 implements Comparable<A1> {
extension A1Tools on int {
bool get isA1Letter => this >= 'A'.codeUnitAt(0) && this <= 'Z'.codeUnitAt(0);
bool get isA1Digit => this >= '0'.codeUnitAt(0) && this <= '9'.codeUnitAt(0);

/// Return the letters for an [A1] from this int or throw
/// a [FormatException].
String get a1Letters {
if (isNegative) {
throw FormatException('column $this must be positive');
}

final codeUnits = <int>[];
if (this < 26) {
codeUnits.add(65 + this);
} else {
var evaluationIndex = this;
while (evaluationIndex >= 26) {
codeUnits.add(65 + evaluationIndex % 26);
evaluationIndex = (evaluationIndex / 26 - 1).floor();
}
codeUnits.add(65 + evaluationIndex);
}
return String.fromCharCodes(codeUnits.reversed);
}
}

/// This extension allows an [A1] to be create from a [String]
Expand Down
18 changes: 15 additions & 3 deletions lib/src/a1_partial.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@ class A1Partial implements Comparable {
/// Examples:
/// ```dart
/// A1Partial a1p = A1Partial('A', 1); //A1
/// a1p = A1Partial('b'); // B <all rows>
/// a1p = A1Partial('1'); // 1 <all columns>
/// a1p = A1Partial(''); // <all columns & rows>
/// a1p = A1Partial('b', null); // B <all rows>
/// a1p = A1Partial(null, '1'); // 1 <all columns>
/// a1p = A1Partial(null, null); // <all columns & rows>
/// ```
A1Partial(String? letters, this.digits) : letters = letters?.toUpperCase();

/// Create a A1Partial from the partial vector
///
/// Examples:
/// ```dart
/// A1Partial a1p = A1Partial.fromVector(0, 0); //A1
/// a1p = A1Partial.fromVector(1, null); // B <all rows>
/// a1p = A1Partial.fromVector(null, 1); // 1 <all columns>
/// a1p = A1Partial.fromVector(null, null); // <all columns & rows>
/// ```
factory A1Partial.fromVector(int? column, int? row) =>
A1Partial(column?.a1Letters, row != null ? row + 1 : null);

/// If this partial has letters and digitis it get be returned as A1
/// otherwise a null is returned
///
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: a1
version: 1.0.5
version: 1.0.6

homepage: https://pub.dev/packages/a1
repository: https://github.com/sjhorn/a1
Expand Down
4 changes: 4 additions & 0 deletions test/a1_partial_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ void main() {
final a1p2 = A1Partial('B', null);
final a1p3 = A1Partial(null, 1);
final a1p4 = A1Partial(null, null);
final a1p5 = A1Partial.fromVector(1, 0);
test('vector matches parse', () {
expect(a1p1 == a1p5, isTrue);
});
test('compare A1 with A1,A,1,null', () {
expect(a1p1.compareTo(a1p1), equals(0));
expect(a1p1.compareTo(a1p2), equals(0));
Expand Down
1 change: 1 addition & 0 deletions test/a1_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void main() {
expect(a1.column, equals(25));
expect(a1.row, equals(2));
expect(a1.toString(), equals('Z3'));
expect(() => A1.fromVector(1, -1), throwsA(isA<FormatException>()));
});

test('throws from an invalid vector', () {
Expand Down

0 comments on commit 8d20515

Please sign in to comment.