lexicographical_order 1.3.0
lexicographical_order: ^1.3.0 copied to clipboard
A string generator that helps to implement a lexicographical order(lexical order). this makes reordering transactions faster and simpler.
Lexicographical Order #
A string generator that helps to implement a lexicographical order(lexical order).
This makes reordering transactions faster and simpler.
Installing #
dependencies:
lexicographical_order:
import 'package:lexicographical_order/lexicographical_order.dart';
Usage #
-
Get a string between two strings in a lexicographical order.
final mid = between(prev: 'B', next: 'D'); assert( areListsEqual( [mid, 'D', 'B']..sort(), ['B', mid, 'D'], ), );
-
Generate order keys.
final keyCount = 100; final orderKeys = generateOrderKeys(keyCount);
This is useful for the following cases:
-
between
cannot be used because the table (collection) is empty.Future<void> addTodo(CreateTodo command) async { final String orderKey = todos.isEmpty ? generateOrderKeys(1).first // <== : between(prev: todos.last.orderKey); final todo = await todoRepository.create(command, orderKey); todos.add(todo); }
-
when migrating to an efficient reorderable system.
Future<void> migrateToLexicalOrderSystem(Table table) async { final itemCount = table.count(); final orderKeys = generateOrderKeys(itemCount); /* omitted */ }
-
Caution #
The between
function accepts only allowed characters as arguments #
more precisely, the following code is used inside between
.
LexOrderValidator().checkBetweenArgs(prev: prev, next: next);
The LexOrderValidator
checks the follwing constraints:
-
both
prev
andnext
must be empty or composed of alphabets. -
next
must not be 'A' -
prev
andnext
must not be equal to each other. -
if both
prev
andnext
are not empty,prev
must not succeednext
in the lexicographical order. for example:between(prev: 'C', next: 'B');
In debug mode, you get an exception if you violate the above constraints. but not in release mode. you can check the constraints manually by using LexOrderValidator
.