Skip to content

Commit

Permalink
A1Range.all and A1Partial.all singetons and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sjhorn committed Apr 29, 2024
1 parent d6bcfd7 commit 95d96bc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 1.0.13

- Bug fix for hasRow, hasColumn for whole columns/rows ie. A:A, 1:1 cases
- Add features to check if a partial represents a whole row or column or either
- Add A1Range.all and A1Partial.all to support these single use-cases

## 1.0.12

Expand Down
12 changes: 12 additions & 0 deletions lib/src/a1_partial.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import 'package:a1/a1.dart';

class A1Partial implements Comparable {
/// Empty/all [A1Partial]
static A1Partial all = A1Partial(null, null);

/// letters of the A1 or null for all columns
final String? letters;

Expand Down Expand Up @@ -163,4 +166,13 @@ class A1Partial implements Comparable {

/// if both letters and digits are null this selects all
bool get isAll => letters == null && digits == null;

/// if this partial represents a whole column ie. no row specified
bool get isWholeColumn => row == null && column != null;

/// if this partial represents a whole row ie. no column specified
bool get isWholeRow => column == null && row != null;

/// if this partial represents either a whole row or column
bool get isWholeRowOrColumn => isWholeColumn || isWholeRow;
}
7 changes: 7 additions & 0 deletions lib/src/a1_range.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import 'package:a1/a1.dart';
import 'package:a1/src/grammer/a1_notation.dart';

class A1Range implements Comparable<A1Range> {
/// All range
static final A1Range all =
A1Range._(A1Partial.all, A1Partial.all, anchor: 'A1'.a1);
static final A1Notation _a1n = A1Notation();
static final _parser = _a1n.buildFrom(_a1n.range()).end();

Expand Down Expand Up @@ -190,6 +193,10 @@ class A1Range implements Comparable<A1Range> {

/// Does this [A1Range] have this [A1] at one of its corners.
bool hasCorner(A1 a1) {
if (this == all && a1 == 'a1'.a1) {
return true;
}

if (from.a1 == a1 || to.a1 == a1) {
return true;
}
Expand Down
12 changes: 12 additions & 0 deletions test/a1_partial_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,17 @@ void main() {
expect(a1p3.hashCode, equals(A1Partial(null, 1).hashCode));
expect(a1p4.hashCode, equals(A1Partial(null, null).hashCode));
});
test('whole row, column or either', () {
expect(A1Partial('A', null).isWholeColumn, isTrue);
expect(A1Partial('A', null).isWholeRow, isFalse);
expect(A1Partial('A', null).isWholeRowOrColumn, isTrue);
expect(A1Partial(null, 1).isWholeColumn, isFalse);
expect(A1Partial(null, 1).isWholeRow, isTrue);
expect(A1Partial(null, 1).isWholeRowOrColumn, isTrue);
});

test('all singleton', () {
expect(A1Partial.all, equals(A1Partial(null, null)));
});
});
}
8 changes: 8 additions & 0 deletions test/a1_range_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import 'package:test/test.dart';

void main() {
group('Creating an A1Range', () {
test('from singleton all', () {
expect(A1Range.all.from.column, isNull);
expect(A1Range.all.from.row, isNull);
expect(A1Range.all.to.column, isNull);
expect(A1Range.all.to.row, isNull);
expect(A1Range.all.anchor, equals('a1'.a1));
});
test('by parsing a string with full a1s', () {
final a1r = A1Range.parse('A1:B2');
expect(a1r, isA<A1Range>());
Expand Down Expand Up @@ -57,6 +64,7 @@ void main() {
expect(a1r.hasCorner('C2'.a1), isTrue);
expect(a1r.hasCorner('A5'.a1), isTrue);
expect(a1r.hasCorner('C5'.a1), isTrue);
expect(A1Range.all.hasCorner('a1'.a1), isTrue);

expect('A:B'.a1Range.hasCorner('A1'.a1), isTrue);
expect('A:A'.a1Range.hasCorner('A1'.a1), isTrue);
Expand Down

0 comments on commit 95d96bc

Please sign in to comment.