prelude

@prelude/cmp · v5.0.1

SourceAPI referenceChangelognpm

Summary

String comparision result

Strict comparision result -1 | 0 | 1 is used instead of arbitrary number.

This preserves soundness in contrast to wide number type which includes NaN (also to lesser extent -Infinity, Infinity and -0 could affect soundness).

Similarly to languages like C using a - b as number comparision should be discouraged as it can create underflow issues. Ie. comparision result of Int32Array elements can't be stored as a - b result in Int32Array as it would require 33 bits of storage to correctly represent this result (ie. new Int32Array([ -2147483648 - 1 ]) // Int32Array(1) [ 2147483647 ]).

Narrow, low cardinality, precise type -1 | 0 | 1 is easier to store, match on and use in combinator functions and in general. It promotes soundness and avoids potential bugs on edge cases.

Cmp module

Usage

npm i -E @prelude/cmp
import * as Cmp from '@prelude/cmp'

console.log([
  'world',
  'hello'
].sort(Cmp.string))
// [ 'hello', 'world' ]

License

This package is dedicated to the public domain under CC0 1.0.