mAssignOverrides

mAssignOverrides

  • (overrides: object) => (base: object) => base
    Or
  • (overrides: map) => (base: map) => base
  1. Assign overrides over a KeyedCollection.

    Consider assignOverrides instead.
    This is for the uncommon case where mutating base is required.

    const ab = { a: 1, b: 2 }
    const assignOverridesAB = mAssignOverrides(ab)
    const obj = { a: 2, c: 3 }
    const mutatedObj = assignOverridesAB(obj)
    
    console.log(mutatedObj === obj) // is true
    console.log(obj) // is { a: 1, c: 3, b: 2 }
    type ABC = {
      a: number
      b: number
      c: number
    }
    
    const ab = { a: 1, b: 2 }
    const assignOverridesAB = mAssignOverrides(ab)
    const obj = { a: 2, c: 3 } as ABC
    const mutatedObj = assignOverridesAB(obj)
    
    console.log(mutatedObj === obj) // is true
    console.log(obj) // is { a: 1, c: 3, b: 2 }
    
    const ab = { a: 1, b: 2 }
    const assignOverridesAB = mAssignOverrides(ab)
    const obj = { a: 2, c: 3 }
    const mutatedObj = assignOverridesAB(obj)
    
    console.log(mutatedObj === obj) // is true
    console.log(obj) // is { a: 1, c: 3, b: 2 }
    type ABC = {
      a: number
      b: number
      c: number
    }
    
    const ab = { a: 1, b: 2 }
    const assignOverridesAB = mAssignOverrides(ab)
    const obj = { a: 2, c: 3 } as ABC
    const mutatedObj = assignOverridesAB(obj)
    
    console.log(mutatedObj === obj) // is true
    console.log(obj) // is { a: 1, c: 3, b: 2 }
    
  2. Sometimes we want to assign required properties over a collection. Below, we have an outfit for our game character Jenkins. To join the pirate guild, we have to wear their pirate hat and eye patch. Let's join'em.

    const enforcedPirateWear = {
      hat: 'pirate hat',
      eyewear: 'eye patch',
    }
    
    const assignEnforcedClothing = mAssignOverrides(enforcedPirateWear)
    
    const JenkinsOutfit = {
      hat: 'propeller cap',
      eyewear: 'welding goggles',
      pants: 'loin cloth',
    }
    
    assignEnforcedClothing(JenkinsOutfit)
    console.log(JenkinsOutfit)
    // is {
    //   hat: pirate hat
    //   eyewear: eye patch
    //   pants: loin cloth
    // }
    const enforcedPirateWear = {
      hat: 'pirate hat',
      eyewear: 'eye patch',
    }
    
    const assignEnforcedClothing = mAssignOverrides(enforcedPirateWear)
    
    const JenkinsOutfit = {
      hat: 'propeller cap',
      eyewear: 'welding goggles',
      pants: 'loin cloth',
    }
    
    assignEnforcedClothing(JenkinsOutfit)
    console.log(JenkinsOutfit)
    // is {
    //   hat: pirate hat
    //   eyewear: eye patch
    //   pants: loin cloth
    // }
    
    const enforcedPirateWear = {
      hat: 'pirate hat',
      eyewear: 'eye patch',
    }
    
    const assignEnforcedClothing = mAssignOverrides(
      enforcedPirateWear
    )
    
    const JenkinsOutfit = {
      hat: 'propeller cap',
      eyewear: 'welding goggles',
      pants: 'loin cloth',
    }
    
    assignEnforcedClothing(JenkinsOutfit)
    console.log(JenkinsOutfit)
    // is {
    //   hat: pirate hat
    //   eyewear: eye patch
    //   pants: loin cloth
    // }
    const enforcedPirateWear = {
      hat: 'pirate hat',
      eyewear: 'eye patch',
    }
    
    const assignEnforcedClothing = mAssignOverrides(
      enforcedPirateWear
    )
    
    const JenkinsOutfit = {
      hat: 'propeller cap',
      eyewear: 'welding goggles',
      pants: 'loin cloth',
    }
    
    assignEnforcedClothing(JenkinsOutfit)
    console.log(JenkinsOutfit)
    // is {
    //   hat: pirate hat
    //   eyewear: eye patch
    //   pants: loin cloth
    // }