More Dart — Literally

Pub Package Build Status Code Coverage GitHub Issues GitHub Forks GitHub Stars GitHub License

A modular, extensively tested suite of pure-Dart data structures, algorithms, and extensions with minimal official dependencies (characters, clock, collection, meta) and full tree-shaking via per-library imports. Works on Dart VM, JS, and Wasm.

Try the interactive web demos: Graph Laboratory · R-Tree Visualizer

Library Description
async Stream extensions: buffer, window, tap, whereType.
cache Async self-loading caches with LRU, FIFO, and duration-based expiry policies.
char_matcher Composable character matchers and Unicode 18.0 categories, scripts, and blocks.
collection BTreeMap/BTreeSet, RTree (R*/Guttman/Linear), Trie, BiMap, BitList, Multimap, Multiset, DisjointSet, FenwickTree, SortedList, OrderedList, TypeMap, combinatorics, and Iterable/String extensions.
comparator Composable comparators, binary search, sorting, and top-k selection.
diff Sequence diffing (Myers / SequenceMatcher) and unified/normal diff formatting.
feature Runtime environment detection (VM, JS, Wasm, assertions, safe integer bounds).
functional Optional, Either, function composition, currying, and scope helpers.
graph Directed/undirected graphs, Dijkstra/A*/Floyd–Warshall, PageRank, centrality, Louvain/modularity, planarity, matching, flow, Mermaid I/O, and layout engines (Force, Sugiyama, Stress, Spectral, Tree, Radial).
interval Continuous Interval and IntervalTree for range queries.
math Primes/sieves, GCD/LCM, factorials, binomials, hyperbolic functions, bit ops, and polynomials.
number Exact Fraction (rational), Complex, and Quaternion number types.
pragma Compiler pragmas for Dart VM, Dart2JS, and Dart2Wasm.
printer Composable formatters for numbers, dates, durations, padding, and alignment.
temporal DateTime truncation, periodical iterables, calendar math, and Duration conversions.
tuple Record/tuple extension methods up to arity 9.

And there are more to come ...

Highlights

Collections & Iterables

Powerful extensions and specialized data structures that complement dart:collection:

import 'package:more/collection.dart';

// Integer ranges with custom stepping
0.to(10, step: 2); // [0, 2, 4, 6, 8]

// Sliding windows and fixed chunks
[1, 2, 3, 4, 5].window(3); // [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
[1, 2, 3, 4, 5].chunked(2); // [[1, 2], [3, 4], [5]]

// Combinatorics: combinations, permutations, and power sets
['a', 'b', 'c'].combinations(2); // [['a', 'b'], ['a', 'c'], ['b', 'c']]

// Rich collections: B+Trees, R-Trees, Tries, Multimaps, BiMaps, BitLists, and Sorted/Ordered lists
final multimap = ListMultimap<String, int>()
  ..add('even', 2)
  ..add('even', 4)
  ..add('odd', 1);
print(multimap['even']); // [2, 4]

Graph Theory & Algorithms

Flexible graph data structures with built-in pathfinding, search, and topological analysis:

import 'package:more/graph.dart';

// Create a directed weighted graph
final graph = Graph<String, num>(isDirected: true)
  ..addEdge('A', 'B', value: 4)
  ..addEdge('A', 'C', value: 2)
  ..addEdge('C', 'B', value: 1);

// Find shortest path using Dijkstra's algorithm
final path = graph.shortestPath('A', 'B');
print(path?.vertices); // ['A', 'C', 'B']
print(path?.cost); // 3

// Topological sort and cycle detection
final sorted = graph.topological('A');
final hasCycle = graph.hasCycle();

Numbers & Mathematics

Exact rational arithmetic, complex numbers, and extended math functions on integers:

import 'package:more/math.dart';
import 'package:more/number.dart';

// Exact rational arithmetic
final a = Fraction(1, 3);
final b = Fraction(1, 6);
print(a + b); // Fraction(1, 2)
print(Fraction.tryParse('3/4')); // Fraction(3, 4)

// Complex numbers & quaternions
final c = Complex(3, 4);
print(c.abs()); // 5.0

// Math extensions on int and BigInt
12.gcd(8); // 4
5.factorial(); // 120
17.isProbablyPrime; // true
123.digits(); // [3, 2, 1]

Composable Printers

A fluent, composable model to format strings, numbers, and dates without complex templates:

import 'package:more/printer.dart';

// Formatted numbers with padding, prefix, and precision
final currency = FixedNumberPrinter(precision: 2).before(r'$').padLeft(8);
print(currency(12.5)); // '  $12.50'

// Human-readable binary/decimal quantities
final human = HumanNumberPrinter.binary();
print(human(1024 * 1024 * 3)); // '3 Mi'

// Configurable date and time formatting
final date = DateTimePrinter.date();
print(date(DateTime(2025, 1, 15))); // '2025-01-15'

Temporal & Intervals

Fluent manipulation of dates, durations, and continuous intervals:

import 'package:more/interval.dart';
import 'package:more/temporal.dart';

// Truncate dates to specific units
final now = DateTime(2025, 5, 12, 14, 23, 45);
print(now.truncateTo(TimeUnit.hour)); // 2025-05-12 14:00:00.000

// Continuous interval operations
final intA = Interval(1, 5); // 1..5
final intB = Interval(3, 7); // 3..7
print(intA.intersection(intB)); // 3..5

Unicode & Character Matching

Full Unicode 18.0.0 compliance featuring standard normalization algorithms and composable character classifiers:

import 'package:more/char_matcher.dart';
import 'package:more/collection.dart';

// Unicode Normalization (NFC, NFD, NFKC, NFKD)
'e\u0301le\u0300ve'.normalize(); // 'élève'
'①'.normalize(form: NormalizationForm.nfkc); // '1'
'ffi'.normalize(form: NormalizationForm.nfkc); // 'ffi'

// Match official Unicode categories, scripts, and properties
final greek = UnicodeCharMatcher.scriptGreek();
final whitespace = UnicodeCharMatcher.whiteSpace();

// Filter, collapse, and clean strings beyond basic ASCII
greek.retainFrom('Hello Ελληνικά!'); // 'Ελληνικά'
whitespace.collapseFrom('lots   of   spaces', ' '); // 'lots of spaces'

// Compose matchers with set operations
final latinOrDigits = UnicodeCharMatcher.scriptLatin() | CharMatcher.digit();
latinOrDigits.retainFrom('Hello 123! π'); // 'Hello123'

// Unicode-aware string splitting preserving surrogate pairs
'Dart 🎯'.toList(unicode: true); // ['D', 'a', 'r', 't', ' ', '🎯']

Caching Strategies

Self-loading asynchronous caches with LRU, FIFO, and duration-based expiry policies:

import 'package:more/cache.dart';

// Self-loading LRU cache with maximum size
final cache = Cache<String, int>.lru(
  loader: (key) => expensiveCalculation(key),
  maximumSize: 100,
);
final value = await cache.get('hello');

Misc

This library is open source, stable and well tested. Development happens on GitHub. Feel free to report issues or create a pull-request there. General questions are best asked on StackOverflow.

The package is hosted on dart packages. Up-to-date class documentation is created with every release.

Contributing

The goal of the library is to provide a loose collection of carefully curated utilities that are not provided by the Dart standard library. All features must be well tested. New features must have significant advantages over alternatives, such as code reduction, readability improvement, speed increase, memory reduction, or improved accuracy. In case of doubt, consider filing a feature request before filing a pull request.

History

This library started in April 2013 as I was working through the puzzles of Project Euler and encountered some missing features in Dart. Over time the code grew and became more useful in many other places, so I created this reusable library.

Some parts of this library are inspired by similar APIs in Google Guava (Google core libraries for Java) and Apache Commons (a repository of reusable Java components).

License

The MIT License, see LICENSE.

Libraries

Data Structures

cache Data Structures
Collection of different caching strategies and their expiry policy.
collection Data Structures
Extensions on iterables, lists, maps, and strings, along with new collection types: bi-map, bit-list, B+tree map/set, disjoint-set, Fenwick tree, multi-map, multi-set, ordered/sorted list, range, R-tree, trie, and type-map.
comparator Data Structures
Provides default Comparator constructors, and various static extension methods to modify and perform common operations on Comparator functions.
interval Data Structures
Provides a continuous interval.
tuple Data Structures
Tuple extension methods on generic records.

Graphs

graph Graphs
Graph-theory objects and algorithms.

Math & Numbers

math Math & Numbers
A collection of common mathematical functions.
number Math & Numbers
Support for fractional and complex arithmetic.
temporal Math & Numbers
Various extensions to the built-in date, time and duration objects.

Text & Formatting

char_matcher Text & Formatting
A first-class model of character classes, their composition and operations on strings.
diff Text & Formatting
Port of Python's difflib library to Dart providing tools for comparing lists.
printer Text & Formatting
Provides a first-class model to convert objects to strings using composition and highly configurable formatting primitives.

Functional & Async

async Functional & Async
Helpers to make async code better.
functional Functional & Async
A collection of functional programming structures.

Utilities

feature Utilities
Information about the runtime environment.
more Utilities
More Dart — Literally.
pragma Utilities
Pragmas for Dart compilers.