fillRangeWith

fillRangeWith

  • (value: any, range: Range) => (anArray: array) => array
  1. Fill an array range with value

    Ranges in Common FP are inclusive

    const zeroOutRange = fillRangeWith(0, { startIdx: 1, endIdx: 2 })
    
    zeroOutRange([1, 2, 3, 4]) // is [1, 0, 0, 4]
    zeroOutRange([1, 2]) // is [1, 0]
    zeroOutRange([]) // is []
    const zeroOutRange = fillRangeWith(0, { startIdx: 1, endIdx: 2 })
    
    zeroOutRange([1, 2, 3, 4]) // is [1, 0, 0, 4]
    zeroOutRange([1, 2]) // is [1, 0]
    zeroOutRange([]) // is []
    
    const zeroOutRange = fillRangeWith(0, {
      startIdx: 1,
      endIdx: 2,
    })
    
    zeroOutRange([1, 2, 3, 4]) // is [1, 0, 0, 4]
    zeroOutRange([1, 2]) // is [1, 0]
    zeroOutRange([]) // is []
    const zeroOutRange = fillRangeWith(0, {
      startIdx: 1,
      endIdx: 2,
    })
    
    zeroOutRange([1, 2, 3, 4]) // is [1, 0, 0, 4]
    zeroOutRange([1, 2]) // is [1, 0]
    zeroOutRange([]) // is []
    
  2. Sometimes we want to set an array range to a value. Below, we have Kim's appointment schedule for this week. Unfortunately an emergency came up, and she'll have to close her business down through Wednesday. Let's update her schedule.

    const schedule = [
      'closed',
      ['10am-12'],
      [],
      ['8am-9', '10:30am-11:30'],
      ['1pm-2:30'],
      ['2pm-3'],
      'closed',
    ]
    
    const monThroughWed = { startIdx: 1, endIdx: 3 }
    const close = fillRangeWith('closed', monThroughWed)
    const updatedSchedule = close(schedule)
    console.log(updatedSchedule)
    // is
    // [
    //   'closed',
    //   'closed',
    //   'closed',
    //   'closed',
    //   ['1pm-2:30'],
    //   ['2pm-3'],
    //   'closed',
    // ]
    const schedule = [
      'closed',
      ['10am-12'],
      [],
      ['8am-9', '10:30am-11:30'],
      ['1pm-2:30'],
      ['2pm-3'],
      'closed',
    ]
    
    const monThroughWed = { startIdx: 1, endIdx: 3 }
    const close = fillRangeWith('closed', monThroughWed)
    const updatedSchedule = close(schedule)
    console.log(updatedSchedule)
    // is
    // [
    //   'closed',
    //   'closed',
    //   'closed',
    //   'closed',
    //   ['1pm-2:30'],
    //   ['2pm-3'],
    //   'closed',
    // ]
    
    const schedule = [
      'closed',
      ['10am-12'],
      [],
      ['8am-9', '10:30am-11:30'],
      ['1pm-2:30'],
      ['2pm-3'],
      'closed',
    ]
    
    const monThroughWed = { startIdx: 1, endIdx: 3 }
    const close = fillRangeWith(
      'closed',
      monThroughWed
    )
    const updatedSchedule = close(schedule)
    console.log(updatedSchedule)
    // is
    // [
    //   'closed',
    //   'closed',
    //   'closed',
    //   'closed',
    //   ['1pm-2:30'],
    //   ['2pm-3'],
    //   'closed',
    // ]
    const schedule = [
      'closed',
      ['10am-12'],
      [],
      ['8am-9', '10:30am-11:30'],
      ['1pm-2:30'],
      ['2pm-3'],
      'closed',
    ]
    
    const monThroughWed = { startIdx: 1, endIdx: 3 }
    const close = fillRangeWith(
      'closed',
      monThroughWed
    )
    const updatedSchedule = close(schedule)
    console.log(updatedSchedule)
    // is
    // [
    //   'closed',
    //   'closed',
    //   'closed',
    //   'closed',
    //   ['1pm-2:30'],
    //   ['2pm-3'],
    //   'closed',
    // ]