Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Pair<FirstType, SecondType>

Represents a generic pair of two Values. There is no meaning attached to values in this class. It can be used for any purpose. Pair exhibits values semantics, i.e. two pairs are equal if both components are equal. However, if any of those two values are instance of class, you'd better make the class explicitly distinguishable by inheriting KoconutEquatable. Please, check the example of 'equalsTo' method

see
-- Base --
KoconutPair, Entry, KoconutEntry

-- Protocol --
KoconutEquatable

Type parameters

  • FirstType

    The type of the first value.

  • SecondType

    The type of the second value.

Hierarchy

  • Pair

Implements

Index

Constructors

Accessors

Methods

Constructors

constructor

  • new Pair(firstElement: FirstType, secondElement: SecondType): Pair

Accessors

first

  • get first(): FirstType

second

  • get second(): SecondType

Methods

equalsTo

  • Class Pair implments KoconutEquatable. The 'equalsTo' method of this is basically check each individual element (first/second) are same or not. When the type of each element is child of KoconutEquatable, it'd be done by using its 'equalsTo' method. Otherwise, it'd be done simply by '==' operator.

    example
      // Case 1 -- All values are simply number or string
      const myPairCase1_01 = Pair.from([10, 20])
      const myPairCase1_02 = Pair.from([10, 20])
      console.log(`${myPairCase1_01.equalsTo(myPairCase1_02)}`)
      // ↑ true
    
      const myPairCase1_03 = Pair.from(["Apex", "Captain"])
      const myPairCase1_04 = Pair.from(["Apex", "Captain"])
      console.log(`${myPairCase1_03 == myPairCase1_04}`)
      // ↑ false
      console.log(`${myPairCase1_03.equalsTo(myPairCase1_04)}`)
      // ↑ true
    
      // Case 2 -- First Type is indistinguishable class
      class MyClass {
          private value : string
          constructor(value : string) {
              this.value = value
          }
      }
      const myPairCase2_01 = Pair.from([new MyClass("Apex"), "Captain"])
      const myPairCase2_02 = Pair.from([new MyClass("Apex"), "Captain"])
      console.log(`${myPairCase2_01.equalsTo(myPairCase2_02)}`)
      // ↑ false
    
      // Case 3 -- First Type is distinguishable class
      class MyDistinguishableClass implements KoconutEquatable {
          private value : string
          constructor(value : string) {
              this.value = value
          }
          equalsTo(other : MyDistinguishableClass) : boolean {
              return this.value == other.value
          }
      }
      const myPairCase3_01 = Pair.from([new MyDistinguishableClass("Apex"), "Captain"])
      const myPairCase3_02 = Pair.from([new MyDistinguishableClass("Apex"), "Captain"])
      console.log(`${myPairCase3_01.equalsTo(myPairCase3_02)}`)
      // ↑ true

    Parameters

    • other: Pair<FirstType, SecondType>

    Returns boolean | KoconutBoolean

toArray

  • toArray(): Array<FirstType | SecondType>
  • Turns this Pair instance into a simple array.

    example
    const myPair = Pair.from(["Apex","Captain"])
    console.log(myPair.toArray())
    // ↑ [ 'Apex', 'Captain' ]

    Returns Array<FirstType | SecondType>

toEntry

  • toEntry(): Entry<FirstType, SecondType>
  • Turns this Pair instance into a simple Entry

    example
    const myPair = Pair.from(["Apex","Captain"])
    console.log(myPair.toEntry())
    // ↑ Entry { keyElement: 'Apex', valueElement: 'Captain' }

    Returns Entry<FirstType, SecondType>

toString

  • toString(): string
  • Turns this Pair instance into a simple JSON object string.

    example
    const myPair = Pair.from(["Apex","Captain"])
    console.log(myPair.toString()) // Or, you can use console.log(`${myPair}`)
    // ↑ {"first":"Apex","second":"Captain"}

    Returns string

Static from

  • from<FirstType, SecondType>(pair: [FirstType, SecondType]): Pair<FirstType, SecondType>
  • Create a Pair instance by iterable two values pair.

    Type parameters

    • FirstType

    • SecondType

    Parameters

    • pair: [FirstType, SecondType]

      Values pair of first/second as iterable.

      const myPair = Pair.from(["Apex","Captain"])
      console.log(myPair)
      // ↑ Pair { firstElement: 'Apex', secondElement: 'Captain' }

    Returns Pair<FirstType, SecondType>

Generated using TypeDoc