mPrependOne

mPrependOne

  • (value: any) => (anArray: array) => anArray
  1. Prepend a value to an array

    Consider prependOne instead.
    This is for the uncommon case where mutating anArray is required.

    const arr = ['b', 'c']
    const prependA = mPrependOne('a')
    const mutatedArr = prependA(arr)
    
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is ['a', 'b', 'c']
    const arr = ['b', 'c']
    const prependA = mPrependOne('a')
    const mutatedArr = prependA(arr)
    
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is ['a', 'b', 'c']
    
    const arr = ['b', 'c']
    const prependA = mPrependOne('a')
    const mutatedArr = prependA(arr)
    
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is ['a', 'b', 'c']
    const arr = ['b', 'c']
    const prependA = mPrependOne('a')
    const mutatedArr = prependA(arr)
    
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is ['a', 'b', 'c']
    
  2. Sometimes we want to mutate an array by prepending a value. Below, we run a sandwich shop and have prep instructions. Apparently people aren't washing their hands and putting gloves on, so let's prepend that step as a reminder.

    const prepInstructions = {
      bacon: [
        'ensure it is fully thawed',
        'open bag with scissors',
        'place in cambro diagonally',
        'wrap the cambro with saran wrap and lid',
      ],
      tuna: [
        'place dried tuna into bowl',
        'break tuna apart',
        'add half a bag of mayo to bowl',
        'knead mayo into tuna',
      ],
    }
    
    const prependGloveStep = mPrependOne('wash hands and put on gloves')
    const addGloveStepToAll = forEach(prependGloveStep)
    addGloveStepToAll(prepInstructions)
    
    console.log(prepInstructions)
    // is {
    //   bacon: [
    //     wash hands and put on gloves
    //     ensure it is fully thawed
    //     open bag with scissors
    //     place in cambro diagonally
    //     wrap the cambro with saran wrap and lid
    //   ]
    //   tuna: [
    //     wash hands and put on gloves
    //     place dried tuna into bowl
    //     break tuna apart
    //     add half a bag of mayo to bowl
    //     knead mayo into tuna
    //   ]
    // }
    type PrepInstructions = Record<string, string[]>
    const prepInstructions: PrepInstructions = {
      bacon: [
        'ensure it is fully thawed',
        'open bag with scissors',
        'place in cambro diagonally',
        'wrap the cambro with saran wrap and lid',
      ],
      tuna: [
        'place dried tuna into bowl',
        'break tuna apart',
        'add half a bag of mayo to bowl',
        'knead mayo into tuna',
      ],
    }
    
    const prependGloveStep = mPrependOne('wash hands and put on gloves')<string[]>
    const addGloveStepToAll = forEach(prependGloveStep)<PrepInstructions>
    addGloveStepToAll(prepInstructions)
    
    console.log(prepInstructions)
    // is {
    //   bacon: [
    //     wash hands and put on gloves
    //     ensure it is fully thawed
    //     open bag with scissors
    //     place in cambro diagonally
    //     wrap the cambro with saran wrap and lid
    //   ]
    //   tuna: [
    //     wash hands and put on gloves
    //     place dried tuna into bowl
    //     break tuna apart
    //     add half a bag of mayo to bowl
    //     knead mayo into tuna
    //   ]
    // }
    
    const prepInstructions = {
      bacon: [
        'ensure it is fully thawed',
        'open bag with scissors',
        'place in cambro diagonally',
        'wrap the cambro with saran wrap and lid',
      ],
      tuna: [
        'place dried tuna into bowl',
        'break tuna apart',
        'add half a bag of mayo to bowl',
        'knead mayo into tuna',
      ],
    }
    
    const prependGloveStep = mPrependOne(
      'wash hands and put on gloves'
    )
    const addGloveStepToAll = forEach(
      prependGloveStep
    )
    addGloveStepToAll(prepInstructions)
    
    console.log(prepInstructions)
    // is {
    //   bacon: [
    //     wash hands and put on gloves
    //     ensure it is fully thawed
    //     open bag with scissors
    //     place in cambro diagonally
    //     wrap the cambro with saran wrap and lid
    //   ]
    //   tuna: [
    //     wash hands and put on gloves
    //     place dried tuna into bowl
    //     break tuna apart
    //     add half a bag of mayo to bowl
    //     knead mayo into tuna
    //   ]
    // }
    type PrepInstructions = Record<string, string[]>
    const prepInstructions: PrepInstructions = {
      bacon: [
        'ensure it is fully thawed',
        'open bag with scissors',
        'place in cambro diagonally',
        'wrap the cambro with saran wrap and lid',
      ],
      tuna: [
        'place dried tuna into bowl',
        'break tuna apart',
        'add half a bag of mayo to bowl',
        'knead mayo into tuna',
      ],
    }
    
    const prependGloveStep = mPrependOne(
      'wash hands and put on gloves'
    )<string[]>
    const addGloveStepToAll = forEach(
      prependGloveStep
    )<PrepInstructions>
    addGloveStepToAll(prepInstructions)
    
    console.log(prepInstructions)
    // is {
    //   bacon: [
    //     wash hands and put on gloves
    //     ensure it is fully thawed
    //     open bag with scissors
    //     place in cambro diagonally
    //     wrap the cambro with saran wrap and lid
    //   ]
    //   tuna: [
    //     wash hands and put on gloves
    //     place dried tuna into bowl
    //     break tuna apart
    //     add half a bag of mayo to bowl
    //     knead mayo into tuna
    //   ]
    // }