isEmpty

isEmpty

  • (collection: Collection | string) => boolean
  1. Test if a collection or string is empty

    isEmpty([]) // is true
    isEmpty([1]) // is false
    
    isEmpty('') // is true
    isEmpty('a') // is false
    isEmpty([]) // is true
    isEmpty([1]) // is false
    
    isEmpty('') // is true
    isEmpty('a') // is false
    
    isEmpty([]) // is true
    isEmpty([1]) // is false
    
    isEmpty('') // is true
    isEmpty('a') // is false
    isEmpty([]) // is true
    isEmpty([1]) // is false
    
    isEmpty('') // is true
    isEmpty('a') // is false
    
  2. Usually we can iterate over collections without worrying whether they contain elements. Sometimes, however, we need to treat empty collections differently. Below, we have appointments scheduled for the next couple of days. Let's display "None" for the days without any appointments.

    const appointments = {
      Monday: [],
      Tuesday: [
        {
          time: '8am-9:30',
          client: "Baz's Baby Furniture",
        },
        {
          time: '11am-12',
          client: 'University Of Florida',
        },
      ],
    }
    
    const displayOneAppt = ({ time, client }) => {
      return `  At: ${time}\n` + `  With: ${client}`
    }
    
    const displayDayAppts = (appts, day) => {
      if (isEmpty(appts)) return day + ': None'
    
      const dayAppts = appts.map(displayOneAppt).join('\n\n')
      return `${day}:\n` + dayAppts
    }
    
    const displayAllDayAppts = compose([
      mapValues(displayDayAppts),
      joinValues('\n\n'),
    ])
    
    const apptDisplay = displayAllDayAppts(appointments)
    console.log(apptDisplay)
    // prints
    // Monday: None
    //
    // Tuesday:
    //   At: 8am-9:30
    //   With: Baz's Baby Furniture
    //
    //   At: 11am-12
    //   With: University Of Florida
    type Appointment = {
      time: string
      client: string
    }
    type Appointments = Record<string, Appointment[]>
    const appointments = {
      Monday: [],
      Tuesday: [
        {
          time: '8am-9:30',
          client: "Baz's Baby Furniture",
        },
        {
          time: '11am-12',
          client: 'University Of Florida',
        },
      ],
    }
    
    const displayOneAppt = ({ time, client }: Appointment) => {
      return `  At: ${time}\n` + `  With: ${client}`
    }
    
    const displayDayAppts = (appts: Appointment[], day: string) => {
      if (isEmpty(appts)) return day + ': None'
    
      const dayAppts = appts.map(displayOneAppt).join('\n\n')
      return `${day}:\n` + dayAppts
    }
    
    const displayAllDayAppts = compose([
      mapValues(displayDayAppts)<Appointments>,
      joinValues('\n\n'),
    ])
    
    const apptDisplay = displayAllDayAppts(appointments)
    console.log(apptDisplay)
    // prints
    // Monday: None
    //
    // Tuesday:
    //   At: 8am-9:30
    //   With: Baz's Baby Furniture
    //
    //   At: 11am-12
    //   With: University Of Florida
    
    const appointments = {
      Monday: [],
      Tuesday: [
        {
          time: '8am-9:30',
          client: "Baz's Baby Furniture",
        },
        {
          time: '11am-12',
          client: 'University Of Florida',
        },
      ],
    }
    
    const displayOneAppt = ({ time, client }) => {
      return `  At: ${time}\n` + `  With: ${client}`
    }
    
    const displayDayAppts = (appts, day) => {
      if (isEmpty(appts)) return day + ': None'
    
      const dayAppts = appts
        .map(displayOneAppt)
        .join('\n\n')
      return `${day}:\n` + dayAppts
    }
    
    const displayAllDayAppts = compose([
      mapValues(displayDayAppts),
      joinValues('\n\n'),
    ])
    
    const apptDisplay =
      displayAllDayAppts(appointments)
    console.log(apptDisplay)
    // prints
    // Monday: None
    //
    // Tuesday:
    //   At: 8am-9:30
    //   With: Baz's Baby Furniture
    //
    //   At: 11am-12
    //   With: University Of Florida
    type Appointment = {
      time: string
      client: string
    }
    type Appointments = Record<string, Appointment[]>
    const appointments = {
      Monday: [],
      Tuesday: [
        {
          time: '8am-9:30',
          client: "Baz's Baby Furniture",
        },
        {
          time: '11am-12',
          client: 'University Of Florida',
        },
      ],
    }
    
    const displayOneAppt = ({
      time,
      client,
    }: Appointment) => {
      return `  At: ${time}\n` + `  With: ${client}`
    }
    
    const displayDayAppts = (
      appts: Appointment[],
      day: string
    ) => {
      if (isEmpty(appts)) return day + ': None'
    
      const dayAppts = appts
        .map(displayOneAppt)
        .join('\n\n')
      return `${day}:\n` + dayAppts
    }
    
    const displayAllDayAppts = compose([
      mapValues(displayDayAppts)<Appointments>,
      joinValues('\n\n'),
    ])
    
    const apptDisplay =
      displayAllDayAppts(appointments)
    console.log(apptDisplay)
    // prints
    // Monday: None
    //
    // Tuesday:
    //   At: 8am-9:30
    //   With: Baz's Baby Furniture
    //
    //   At: 11am-12
    //   With: University Of Florida