Interfaces for ordering, equality, hashing, type conversions, functional maps, interpolators, scales, iterators, builders, key-value maps, caches, and assertions
npm install @swim/utilThe Swim Util library provides interfaces for ordering, equality, hashing,
type conversions, functional maps, interpolators, scales, iterators, builders,
key-value maps, caches, and assertions.
Swim Util exports Comparable, Equals, and HashCode interfaces that can
be implemented by ordered, equatable, and hash-able classes, respectively.
``typescript`
export interface Comparable
compareTo(that: T): number;
}
export interface Equals {
equals(that: unknown): boolean;
}
export interface HashCode extends Equals {
hashCode(): number;
}
The exported Objects object supports generic comparison, equality testing,
and hashing of arbitrary JavaScript values, including primitives, arrays, and
objects.
Objects.compare(x: unknown, y: unknown): 0 | 1 | -1 returns the relativex
sort order of two comparable values. If implements Comparable, thenObjects.compare delegates to x's compareTo method. If x and y arex
both numbers, or both strings, they are compared lexicographically. If y
and are both arrays, then each corresponding element is compared, in turn,Objects.compare
using . If x and y are both objects, then each entry isObjects.compare
compared first by key, then by value, using . Values of
incompatible types sort in a deterministic order based on type.
Objects.equal(x: unknown, y: unknown): boolean returns true if two valuesx
are equivalent. If implements Equals, then Objects.equal delegates tox's equals method. If x and y are both primitives, then they arex
compared by value. If and y are both arrays, then each correspondingObjects.equal
element is tested for equality, in turn, using . If x and yObjects.equal
are both objects, then each entry is tested for equality furst by key, then by
value, using .
Objects.hash(x: unknown): number returns a consistent hash code for x.x
If implements HashCode, then Objects.hash delegate's to x'shashCode method. If x is a primitive, it is hashed using the Murmur3x
hashing algorithm. If is an array, each element is hashed individuallyObjects.hash
using , and the hash codes of all elements get mixed together.x
If is an object, each entry has its key and value hashed usingObjects.hash, and the hash codes of all entries get mixed together.
The exported Murmur3 object implements the 32-bit
MurmurHash algorithm, version 3.
The exported Builder interface abstracts over construction of collections.PairBuilder
And the interface abstracts over construction of key-value maps,
and other pair-containing collections.
`typescript`
export interface Builder {
push(...inputs: I[]): void;
build(): O;
}
export interface PairBuilder
add(key: K, value: V): void;
build(): O;
}
Swim Util defines three key-value map interfaces: an ES6-compatible MapOrderedMap
interface, as well as an interface, and a ReducedMap interface.OrderedMap
An has its entries sorted by key order. A ReducedMap is anOrderedMap that memoizes partial combinations of sub-elements to support
efficient, incremental reduction of continuously mutating datasets.
The exported Assert interface provides a common API for constraint testingassert
and contract enforcement. The exported singleton provides a defaultAssert implementation that throws AssertException` on assert failure.