npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@swim/util

v4.0.0

Published

Interfaces for ordering, equality, hashing, type conversions, functional maps, interpolators, scales, iterators, builders, key-value maps, caches, and assertions

Downloads

560

Readme

Swim Swim Util Library

The Swim Util library provides interfaces for ordering, equality, hashing, type conversions, functional maps, interpolators, scales, iterators, builders, key-value maps, caches, and assertions.

Overview

Ordering, equality, and hashing

Swim Util exports Comparable, Equals, and HashCode interfaces that can be implemented by ordered, equatable, and hash-able classes, respectively.

export interface Comparable<T> {
  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 relative sort order of two comparable values. If x implements Comparable, then Objects.compare delegates to x's compareTo method. If x and y are both numbers, or both strings, they are compared lexicographically. If x and y are both arrays, then each corresponding element is compared, in turn, using Objects.compare. If x and y are both objects, then each entry is compared first by key, then by value, using Objects.compare. Values of incompatible types sort in a deterministic order based on type.

Objects.equal(x: unknown, y: unknown): boolean returns true if two values are equivalent. If x implements Equals, then Objects.equal delegates to x's equals method. If x and y are both primitives, then they are compared by value. If x and y are both arrays, then each corresponding element is tested for equality, in turn, using Objects.equal. If x and y are both objects, then each entry is tested for equality furst by key, then by value, using Objects.equal.

Objects.hash(x: unknown): number returns a consistent hash code for x. If x implements HashCode, then Objects.hash delegate's to x's hashCode method. If x is a primitive, it is hashed using the Murmur3 hashing algorithm. If x is an array, each element is hashed individually using Objects.hash, and the hash codes of all elements get mixed together. If x is an object, each entry has its key and value hashed using Objects.hash, and the hash codes of all entries get mixed together.

The exported Murmur3 object implements the 32-bit MurmurHash algorithm, version 3.

Builder interfaces

The exported Builder interface abstracts over construction of collections. And the PairBuilder interface abstracts over construction of key-value maps, and other pair-containing collections.

export interface Builder<I, O> {
  push(...inputs: I[]): void;
  build(): O;
}
export interface PairBuilder<K, V, O> {
  add(key: K, value: V): void;
  build(): O;
}

Map interfaces

Swim Util defines three key-value map interfaces: an ES6-compatible Map interface, as well as an OrderedMap interface, and a ReducedMap interface. An OrderedMap has its entries sorted by key order. A ReducedMap is an OrderedMap that memoizes partial combinations of sub-elements to support efficient, incremental reduction of continuously mutating datasets.

Assertions

The exported Assert interface provides a common API for constraint testing and contract enforcement. The exported assert singleton provides a default Assert implementation that throws AssertException on assert failure.