Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Compare<SourceType, TargetType>

The Compare class compares 2 objects to each other. It simplifies the process of querying the differences and similarities between the 2 objects being compared. The types of relationships between the objects is relative to how the source object compares to the target object. There are 4 types of relations that can be queried via properties of a Compare object:

  1. omittedProperties

    Properties that are present in the source object but not in the target object are considered "omitted" properties. As in they're properties that are present in the source object but omitted in the target object.

    const obj1 = { prop1: 1,
                   prop2: "foo" };
    
    const obj2 = { prop1: 1 };
    
    console.log(new Compare(obj1, obj2).omittedProperties);
    // prints: { prop2: "foo" }
    
  2. extraProperties

    Properties that are not present in the source object but are present in the target object are considered "extra" properties. As in they're extra properties that are present in the target object but not in the source object.

    const obj1 = { prop1: 1,
                   prop2: "foo" };
    
    const obj2 = { prop1: 1,
                   prop2: "foo",
                   prop3: true };
    
    console.log(new Compare(obj1, obj2).extraProperties);
    // prints: { prop3: true }
    
  3. sharedProperties

    Properties that are present in both the source and target object that are equivalent are considered "shared" properties. If there are keys that are present in both the source and target objects that are also mapped to equivalent values then the key-value pairs are considered shared properties.

    const obj1 = { prop1: 1,
                   prop2: "foo",
                   prop3: false };
    
    const obj2 = { prop1: 1,
                   prop2: "foo",
                   prop3: true };
    
    console.log(new Compare(obj1, obj2).sharedProperties);
    // prints: { prop1: 1,
    //           prop2: "foo" }
    
  4. alteredProperties

    Properties that are present in both the source and target object that contain differing values are considered "altered" properties. If there are keys that are present in both the source and target objects that are mapped to differing values then the key-value pairs are considered altered properties.

    const obj1 = { prop1: 1,
                   prop2: "foo",
                   prop3: false };
    
    const obj2 = { prop1: 1,
                   prop2: "foo",
                   prop3: true };
    
    console.log(new Compare(obj1, obj2).alteredProperties);
    // prints: { prop3: { sourceValue: false,
    //                    targetValue: true } }
    
classdesc
author

Snap

Type parameters

  • SourceType

    The type of object being compared to the target object.

  • TargetType

    The type of object the source object is being compared to.

Hierarchy

  • Compare

Index

Constructors

constructor

  • new Compare<SourceType, TargetType>(sourceObject: NonNullable<SourceType>, targetObject: NonNullable<TargetType>): Compare<SourceType, TargetType>
  • Constructs an instance of a Compare object. The only required arguments are 2 "comparable" non-null objects to compare to each other. As long as the passed arguments can be interpreted as sets of enumerable properties (key-value pairs) then they're considered comparable.

    remarks

    Strings are converted to string arrays where each character of the string is mapped to an index that corresponds to its position in the string. So it's possible to compare strings to each other (or other objects).

    throws

    {TypeError} If source or target object is an invalid type that can't be interpreted as an enumerable set of properties (key value pairs).

    Type parameters

    • SourceType

      The type of object being compared to the target object.

    • TargetType

      The type of object the source object is being compared to.

    Parameters

    • sourceObject: NonNullable<SourceType>

      The object being compared to the target object.

    • targetObject: NonNullable<TargetType>

      The object the source object is being compared to.

    Returns Compare<SourceType, TargetType>

    Returns instantiated Compare object.

Properties

Private Readonly #alteredProperties

#alteredProperties: Readonly<PropertyValueDifferences>

An object containing properties whose keys are present in both the source and target objects, but are mapped to differing values.

readonly

Private Readonly #alteredPropertiesCount

#alteredPropertiesCount: number

Number of properties that are present in both the source and target objects, but are mapped to differing values.

readonly

Private Readonly #extraProperties

#extraProperties: Readonly<{}>

An object containing the keys and their mapped values that are present in the target object but not the source object.

readonly

Private Readonly #extraPropsCount

#extraPropsCount: number

Number of properties the target object contains that aren't present in the source object being compared to it.

readonly

Private Readonly #hasAlteredProperties

#hasAlteredProperties: boolean

Boolean indicating if there are properties in the source and target object mapped to differing values.

readonly

Private Readonly #hasExtraProperties

#hasExtraProperties: boolean

Boolean indicating if there are properties in the target object that are not present in the source object.

readonly

Private Readonly #hasOmittedProperties

#hasOmittedProperties: boolean

Boolean indicating if there are properties in the source object that are not present in the target object.

readonly

Private Readonly #hasSharedProperties

#hasSharedProperties: boolean

Boolean indicating if there are equivalent properties in the source and target object.

readonly

Private Readonly #omittedProperties

#omittedProperties: Readonly<{}>

An object containing the keys and their mapped values that are present in the source object but not the target object.

readonly

Private Readonly #omittedPropsCount

#omittedPropsCount: number

Number of properties the source object contains that aren't present in the target object it's being compared to.

readonly

Private Readonly #sharedProperties

#sharedProperties: Readonly<{}>

An object containing properties that are present in both the source and target objects being compared that are equivalent. That is, Keys that are present in both the source and target objects that are mapped to equivalent values.

readonly

Private Readonly #sharedPropertiesCount

#sharedPropertiesCount: number

Number of properties that both the source and target object contain that are equivalent.

readonly

Private Readonly #srcObj

#srcObj: Readonly<SourceType>

Contains the source object passed to the constructor during instantiation.

readonly

Private Readonly #targetObj

#targetObj: Readonly<TargetType>

Contains the target object passed to the constructor during instantiation.

readonly

Readonly count

count: Query<number> = ...

Returns a number representing the amount of omitted, extra, shared, and/or altered properties that are present in the source and target objects being compared.

property

{number} count.omittedProperties() - number of properties that are present in the source object, but aren't present in the target object it's being compared to.

property

{number} count.extraProperties() - number of properties that are not present in the source object, but are present in the target object it's being compared to.

property

{number} count.sharedProperties() - number of properties that are present in both the source and target object that are equivalent.

property

{number} count.alteredProperties() - number of keys present in both the the source and target object that are mapped to differing values.

readonly

Readonly has

has: Query<boolean> = ...

Returns a boolean representing whether omitted, extra, shared, and/or altered properties are present in the source and target objects being compared.

property

{boolean} has.omittedProperties() - boolean indicating whether the source object contains properties that aren't present in the target object it's being compared to.

property

{boolean} has.extraProperties() - boolean indicating whether the source object does not contain properties that are present in the target object it's being compared to.

property

{boolean} has.sharedProperties() - boolean indicating whether the source and target object contain equivalent properties.

property

{boolean} has.alteredProperties() - boolean indicating whether the source and target object contain equivalent keys that are mapped to differing values.

readonly

Accessors

alteredProperties

  • Returns the properties that are present in both the source and target object that have differing values. That is, keys that are present in both the source and target object that are mapped to differing values.

    remarks

    The key of the property and property value from the source and target object is returned.

    Returns Readonly<PropertyValueDifferences>

    An object containing the keys that are present in the source and target object and the differing values they are mapped to in the source and target object.

extraProperties

  • get extraProperties(): Readonly<{}>
  • Returns the properties that are present in the target object but not the source object that's being compared to it.

    Returns Readonly<{}>

    An object containing the properties that are not present in the source object but are present in the target object.

omittedProperties

  • get omittedProperties(): Readonly<{}>
  • Returns the properties that are present in the source object but not the target object it's being compared to.

    Returns Readonly<{}>

    An object containing the properties that are present in the source object but not the target object.

sharedProperties

  • get sharedProperties(): Readonly<{}>
  • Returns the properties that are present in both the source and target object that are equivalent. That is, keys that are present in both the source and target object that are also mapped to equivalent values.

    Returns Readonly<{}>

    An object containing the properties that are present and equivalent in the source and target object.

source

  • get source(): Readonly<SourceType>
  • Returns the source object being compared to the target object. The returned object is frozen to prevent inadvertently mutating it.

    Returns Readonly<SourceType>

    The source object being compared to the target object.

target

  • get target(): Readonly<TargetType>
  • Returns the target object that the source object is being compared to. The returned object is frozen to prevent inadvertently mutating it.

    Returns Readonly<TargetType>

    The target object the source object is being compared to.

Generated using TypeDoc