Skip to main content
Module

x/rimbu/mod.ts>Comp.objectComp

Rimbu is a TypeScript library focused on immutable, performant, and type-safe collections and other tools.
Go to Latest
function Comp.objectComp
import { Comp } from "https://deno.land/x/rimbu@0.14.0/mod.ts";
const { objectComp } = Comp;

Returns a Comp instance for objects that orders the object keys according to the given keyComp, and then compares the corresponding values using the given valueComp. Objects are then compared as follows: starting with the smallest key of either object:

  • if only one of the objects has the key, the object with the key is considered to be larger than the other
  • if both objects have the key, the values are compared with valueComp. If the values are not equal, this result is returned.

if the objects have the same keys with the same values, they are considered equal

Examples

Example 1

const c = Comp.objectComp();
console.log(c.compare({ a: 1 }, { a: 1 }))
// => 0
console.log(c.compare({ a: 1 }, { a: 2 }) < 0)
// => true
console.log(c.compare({ b: 5 }, { a: 2 }) < 0)
// => true
console.log(c.compare({ a: 1, b: 2 }, { b: 5 }) < 0)
// => true
console.log(c.compare({ a: 1, b: 2 }, { b: 2, a: 1 }))
// => 0

Parameters

optional
options: { keyComp?: Comp<any>; valueComp?: Comp<any>; }
  • (optional) the Comp instance used to order the object keys

Returns

Comp<Record<any, any>>