Skip to content

Commit

Permalink
Add possibility to customize the grouping in fixed, scientific, human…
Browse files Browse the repository at this point in the history
… and ordinal number formatters.
  • Loading branch information
renggli committed Dec 24, 2024
1 parent a9be063 commit be23a57
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/src/printer/number/fixed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@ class FixedNumberPrinter<T extends num> extends Printer<T> {
this.padding = 0,
this.precision = 0,
this.separator = '',
this.separatorWidth = 3,
this.separatorOffset = 0,
Printer<T>? sign,
}) : assert(base <= characters.length, 'Not enough characters for base'),
sign = sign ?? SignNumberPrinter<T>.omitPositiveSign(),
_integer = const Printer<String>.standard()
.mapIf(padding > 0,
(printer) => printer.padLeft(padding, characters[0]))
.mapIf(separator.isNotEmpty,
(printer) => printer.separateRight(3, 0, separator)),
.mapIf(
separator.isNotEmpty,
(printer) => printer.separateRight(
separatorWidth, separatorOffset, separator)),
_fraction = const Printer<String>.standard()
.mapIf(precision > 0,
(printer) => printer.padLeft(precision, characters[0]))
.mapIf(separator.isNotEmpty,
(printer) => printer.separateLeft(3, 0, separator));
.mapIf(
separator.isNotEmpty,
(printer) => printer.separateLeft(
separatorWidth, separatorOffset, separator));

/// Round towards the nearest number that is a multiple of accuracy.
final double? accuracy;
Expand Down Expand Up @@ -62,6 +68,12 @@ class FixedNumberPrinter<T extends num> extends Printer<T> {
/// The separator character to be used to group digits.
final String separator;

/// The number of characters to be separated in a group.
final int separatorWidth;

/// The offset of characters to be separated in a group.
final int separatorOffset;

/// The printer used for negative or positive numbers.
final Printer<T> sign;

Expand Down Expand Up @@ -122,5 +134,7 @@ class FixedNumberPrinter<T extends num> extends Printer<T> {
..addValue(padding, name: 'padding')
..addValue(precision, name: 'precision')
..addValue(separator, name: 'separator')
..addValue(separatorWidth, name: 'separatorWidth')
..addValue(separatorOffset, name: 'separatorOffset')
..addValue(sign, name: 'sign');
}
20 changes: 20 additions & 0 deletions lib/src/printer/number/human.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class HumanNumberPrinter<T extends num> extends Printer<T> {
this.padding = 0,
this.precision = 0,
this.separator = '',
this.separatorWidth = 3,
this.separatorOffset = 0,
this.sign,
this.unitBase = 10,
this.unitOffset = 0,
Expand All @@ -102,6 +104,8 @@ class HumanNumberPrinter<T extends num> extends Printer<T> {
padding: padding,
precision: unitPrecision,
separator: separator,
separatorWidth: separatorWidth,
separatorOffset: separatorOffset,
sign: sign ?? const SignNumberPrinter<double>.omitPositiveSign(),
),
_scaled = FixedNumberPrinter(
Expand All @@ -113,6 +117,8 @@ class HumanNumberPrinter<T extends num> extends Printer<T> {
padding: padding,
precision: precision,
separator: separator,
separatorWidth: separatorWidth,
separatorOffset: separatorOffset,
sign: sign ?? const SignNumberPrinter<double>.omitPositiveSign(),
);

Expand All @@ -130,6 +136,8 @@ class HumanNumberPrinter<T extends num> extends Printer<T> {
int padding = 0,
int precision = 0,
String separator = '',
int separatorWidth = 3,
int separatorOffset = 0,
Printer<double>? sign,
int unitPrecision = 0,
bool unitPrefix = false,
Expand All @@ -144,6 +152,8 @@ class HumanNumberPrinter<T extends num> extends Printer<T> {
padding: padding,
precision: precision,
separator: separator,
separatorWidth: separatorWidth,
separatorOffset: separatorOffset,
sign: sign,
unitBase: decimalUnitBase,
unitOffset: decimalUnitOffset,
Expand All @@ -167,6 +177,8 @@ class HumanNumberPrinter<T extends num> extends Printer<T> {
int padding = 0,
int precision = 0,
String separator = '',
int separatorWidth = 3,
int separatorOffset = 0,
int unitPrecision = 0,
Printer<double>? sign,
bool unitPrefix = false,
Expand All @@ -181,6 +193,8 @@ class HumanNumberPrinter<T extends num> extends Printer<T> {
padding: padding,
precision: precision,
separator: separator,
separatorWidth: separatorWidth,
separatorOffset: separatorOffset,
sign: sign,
unitBase: binaryUnitBase,
unitOffset: binaryUnitOffset,
Expand Down Expand Up @@ -214,6 +228,12 @@ class HumanNumberPrinter<T extends num> extends Printer<T> {
/// The separator character to be used to group digits.
final String separator;

/// The number of characters to be separated in a group.
final int separatorWidth;

/// The offset of characters to be separated in a group.
final int separatorOffset;

/// The string to be prepended if the number is positive or negative.
final Printer<double>? sign;

Expand Down
10 changes: 10 additions & 0 deletions lib/src/printer/number/ordinal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ class OrdinalNumberPrinter extends Printer<int> {
this.characters = NumeralSystem.latin,
this.padding = 0,
this.separator = '',
this.separatorWidth = 3,
this.separatorOffset = 0,
this.sign,
}) : _number = FixedNumberPrinter<int>(
base: base,
characters: characters,
padding: padding,
separator: separator,
separatorWidth: separatorWidth,
separatorOffset: separatorOffset,
sign: sign);

/// The numeric base to which the number should be printed.
Expand All @@ -32,6 +36,12 @@ class OrdinalNumberPrinter extends Printer<int> {
/// The separator character to be used to group digits.
final String separator;

/// The number of characters to be separated in a group.
final int separatorWidth;

/// The offset of characters to be separated in a group.
final int separatorOffset;

/// The printer used for negative or positive numbers.
final Printer<int>? sign;

Expand Down
12 changes: 12 additions & 0 deletions lib/src/printer/number/scientific.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class ScientificNumberPrinter<T extends num> extends Printer<T> {
this.notation = notationString,
this.precision = 3,
this.separator = '',
this.separatorWidth = 3,
this.separatorOffset = 0,
this.significant = 1,
}) : _mantissa = FixedNumberPrinter<double>(
base: base,
Expand All @@ -32,6 +34,8 @@ class ScientificNumberPrinter<T extends num> extends Printer<T> {
padding: mantissaPadding,
precision: precision,
separator: separator,
separatorWidth: separatorWidth,
separatorOffset: separatorOffset,
sign: mantissaSign ??
const SignNumberPrinter<double>.omitPositiveSign(),
),
Expand All @@ -40,6 +44,8 @@ class ScientificNumberPrinter<T extends num> extends Printer<T> {
characters: characters,
padding: exponentPadding,
separator: separator,
separatorWidth: separatorWidth,
separatorOffset: separatorOffset,
sign: exponentSign ?? const SignNumberPrinter<int>.omitPositiveSign(),
);

Expand Down Expand Up @@ -79,6 +85,12 @@ class ScientificNumberPrinter<T extends num> extends Printer<T> {
/// The separator character to be used to group digits.
final String separator;

/// The number of characters to be separated in a group.
final int separatorWidth;

/// The offset of characters to be separated in a group.
final int separatorOffset;

/// The number of significant digits to be printed.
final int significant;

Expand Down
24 changes: 24 additions & 0 deletions test/graph_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3042,6 +3042,30 @@ void main() {
{'co', 'de', 'ka', 'ta'},
});
});
test('complete graph', () {
for (var i = 1; i < 25; i++) {
final graph = GraphFactory<int, void>(isDirected: false)
.complete(vertexCount: i);
expect(graph.findCliques().single, 0.to(i).toSet());
}
});
test('complete graph with missing edge', () {
const count = 10;
for (var x = 0; x < count; x++) {
for (var y = 0; y < count; y++) {
if (x == y) continue;
final graph = GraphFactory<int, void>(isDirected: false)
.complete(vertexCount: count);
graph.removeEdge(x, y);
expect(
graph.findCliques(),
unorderedEquals([
0.to(count).toSet()..remove(x),
0.to(count).toSet()..remove(y),
]));
}
}
});
test('directed graph error', () {
final graph = Graph<int, void>.directed();
expect(graph.findCliques, throwsGraphError);
Expand Down
39 changes: 39 additions & 0 deletions test/printer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ void main() {
expect(printer(1234), '1.234');
expect(printer(1234567), '1.234.567');
});
test('separator width', () {
final printer =
FixedNumberPrinter<int>(base: 2, separator: '_', separatorWidth: 8);
expect(printer(1234), '100_11010010');
expect(printer(1234567), '10010_11010110_10000111');
});
test('separator offset', () {
final printer = FixedNumberPrinter<int>(
base: 8, separator: '*', separatorWidth: 4, separatorOffset: 2);
expect(printer(1234), '23*22');
expect(printer(1234567), '4*5532*07');
});
test('sign', () {
final printer = FixedNumberPrinter<int>(
sign: const SignNumberPrinter<int>.negativeAndPositiveSign());
Expand Down Expand Up @@ -215,6 +227,16 @@ void main() {
expect(printer(12345.0), '12!345.000!000!00');
expect(printer(0.6789), '0.678!900!00');
});
test('separator width and offset', () {
final printer = FixedNumberPrinter<double>(
base: 2,
precision: 16,
separator: '_',
separatorWidth: 8,
separatorOffset: 4);
expect(printer(12345.0), '11_00000011_1001.0000_00000000_0000');
expect(printer(0.6789), '0.1010_11011100_1100');
});
test('sign', () {
final printer = FixedNumberPrinter<double>(
precision: 1,
Expand Down Expand Up @@ -469,6 +491,23 @@ void main() {
expect(printer(0.2), '2,000.000,0e-4');
expect(printer(0.00000000751), '7,510.000,0e-12');
});
test('separator with width and offset', () {
final printer = ScientificNumberPrinter(
base: 2,
precision: 8,
significant: 4,
separator: '_',
separatorWidth: 4,
separatorOffset: 2);
expect(printer(0), '0.00_0000_00e0');
expect(printer(2), '10_00.00_0000_00e-10');
expect(printer(300), '10_01.01_1000_00e1_01');
expect(printer(4321.768), '10_00.01_1100_01e10_01');
expect(printer(-53000), '-11_00.11_1100_01e11_00');
expect(printer(6720000000), '11_00.10_0001_00e111_01');
expect(printer(0.2), '11_00.11_0011_01e-1_10');
expect(printer(0.00000000751), '10_00.00_0100_00e-111_10');
});
test('significant', () {
final printer = ScientificNumberPrinter(significant: 3);
expect(printer(0), '0.000e0');
Expand Down

0 comments on commit be23a57

Please sign in to comment.