returnFirstArg

returnFirstArg

  • (arg: any) => arg
  1. Return the first argument

    returnFirstArg('a', 'b') // is 'a'
    returnFirstArg() // is undefined
    returnFirstArg('a', 'b') // is 'a'
    returnFirstArg() // is undefined
    
    returnFirstArg('a', 'b') // is 'a'
    returnFirstArg() // is undefined
    returnFirstArg('a', 'b') // is 'a'
    returnFirstArg() // is undefined
    
  2. Developers commonly use this as a default option. Below, we're coding a game where heroes craft various swords. When crafting a sword, let's take a function that modifies the default sword attributes. This argument will leave them unmodified by default.

    const returnAsIs = returnFirstArg
    
    const craftSword = (modify = returnAsIs) => {
      const defaultAttributes = {
        prefix: '',
        weight: 5,
        damage: 7,
        speed: 3,
      }
      const attrs = modify(defaultAttributes)
      return {
        name: 'sword',
        ...attrs,
      }
    }
    
    const lighten = attrs => ({
      prefix: 'light',
      weight: attrs.weight - 2,
      damage: attrs.damage - 2,
      speed: attrs.speed + 2,
    })
    
    const sword = craftSword()
    console.log(sword)
    
    const lightSword = craftSword(lighten)
    console.log(lightSword)
    const returnAsIs = returnFirstArg
    
    type Attributes = {
      prefix: string
      weight: number
      damage: number
      speed: number
    }
    
    type Modify = (attrs: Attributes) => Attributes
    
    const craftSword = (modify: Modify = returnAsIs) => {
      const defaultAttributes = {
        prefix: '',
        weight: 5,
        damage: 7,
        speed: 3,
      }
      const attrs = modify(defaultAttributes)
      return {
        name: 'sword',
        ...attrs,
      }
    }
    
    const lighten: Modify = attrs => ({
      prefix: 'light',
      weight: attrs.weight - 2,
      damage: attrs.damage - 2,
      speed: attrs.speed + 2,
    })
    
    const sword = craftSword()
    console.log(sword)
    
    const lightSword = craftSword(lighten)
    console.log(lightSword)
    
    const returnAsIs = returnFirstArg
    
    const craftSword = (modify = returnAsIs) => {
      const defaultAttributes = {
        prefix: '',
        weight: 5,
        damage: 7,
        speed: 3,
      }
      const attrs = modify(defaultAttributes)
      return {
        name: 'sword',
        ...attrs,
      }
    }
    
    const lighten = attrs => ({
      prefix: 'light',
      weight: attrs.weight - 2,
      damage: attrs.damage - 2,
      speed: attrs.speed + 2,
    })
    
    const sword = craftSword()
    console.log(sword)
    
    const lightSword = craftSword(lighten)
    console.log(lightSword)
    const returnAsIs = returnFirstArg
    
    type Attributes = {
      prefix: string
      weight: number
      damage: number
      speed: number
    }
    
    type Modify = (attrs: Attributes) => Attributes
    
    const craftSword = (
      modify: Modify = returnAsIs
    ) => {
      const defaultAttributes = {
        prefix: '',
        weight: 5,
        damage: 7,
        speed: 3,
      }
      const attrs = modify(defaultAttributes)
      return {
        name: 'sword',
        ...attrs,
      }
    }
    
    const lighten: Modify = attrs => ({
      prefix: 'light',
      weight: attrs.weight - 2,
      damage: attrs.damage - 2,
      speed: attrs.speed + 2,
    })
    
    const sword = craftSword()
    console.log(sword)
    
    const lightSword = craftSword(lighten)
    console.log(lightSword)