isFalsey

isFalsey

  • (value: any) => boolean
  1. Test whether a value is falsey

    isFalsey(undefined) // is true
    isFalsey('') // is true
    isFalsey(0) // is true
    
    isFalsey('a') // is false
    isFalsey(1) // is false
    isFalsey(undefined) // is true
    isFalsey('') // is true
    isFalsey(0) // is true
    
    isFalsey('a') // is false
    isFalsey(1) // is false
    
    isFalsey(undefined) // is true
    isFalsey('') // is true
    isFalsey(0) // is true
    
    isFalsey('a') // is false
    isFalsey(1) // is false
    isFalsey(undefined) // is true
    isFalsey('') // is true
    isFalsey(0) // is true
    
    isFalsey('a') // is false
    isFalsey(1) // is false
    
  2. Sometimes falsey values need to be handled. Below, we have applicants compiled from multiple sources. If a field is falsey, that means the data is missing. Let's get the missing fields so we can reach back out to the applicants.

    const applicants = {
      chris: {
        yearsExperience: undefined,
        applied: '2025 02 15',
      },
      liz: {
        yearsExperience: '2',
        applied: '',
      },
    }
    
    const getMissingFields = compose([keepWhen(isFalsey), Object.keys])
    const getAllMissingFields = mapValues(getMissingFields)
    const applicantPrompts = getAllMissingFields(applicants)
    console.log(applicantPrompts)
    // is {
    //   chris: [yearsExperience]
    //   liz: [applied]
    // }
    type Applicant = {
      yearsExperience?: string
      applied?: string
    }
    type Applicants = Record<string, Applicant>
    const applicants: Applicants = {
      chris: {
        yearsExperience: undefined,
        applied: '2025 02 15',
      },
      liz: {
        yearsExperience: '2',
        applied: '',
      },
    }
    
    const getMissingFields = compose([keepWhen(isFalsey)<Applicant>, Object.keys])
    const getAllMissingFields = mapValues(getMissingFields)<Applicants>
    const applicantPrompts = getAllMissingFields(applicants)
    console.log(applicantPrompts)
    // is {
    //   chris: [yearsExperience]
    //   liz: [applied]
    // }
    
    const applicants = {
      chris: {
        yearsExperience: undefined,
        applied: '2025 02 15',
      },
      liz: {
        yearsExperience: '2',
        applied: '',
      },
    }
    
    const getMissingFields = compose([
      keepWhen(isFalsey),
      Object.keys,
    ])
    const getAllMissingFields = mapValues(
      getMissingFields
    )
    const applicantPrompts =
      getAllMissingFields(applicants)
    console.log(applicantPrompts)
    // is {
    //   chris: [yearsExperience]
    //   liz: [applied]
    // }
    type Applicant = {
      yearsExperience?: string
      applied?: string
    }
    type Applicants = Record<string, Applicant>
    const applicants: Applicants = {
      chris: {
        yearsExperience: undefined,
        applied: '2025 02 15',
      },
      liz: {
        yearsExperience: '2',
        applied: '',
      },
    }
    
    const getMissingFields = compose([
      keepWhen(isFalsey)<Applicant>,
      Object.keys,
    ])
    const getAllMissingFields = mapValues(
      getMissingFields
    )<Applicants>
    const applicantPrompts =
      getAllMissingFields(applicants)
    console.log(applicantPrompts)
    // is {
    //   chris: [yearsExperience]
    //   liz: [applied]
    // }