isTruthy
isTruthy
(value: any) => boolean
Test whether a value is truthy
isTruthy('a') // is true isTruthy(1) // is true isTruthy(undefined) // is false isTruthy('') // is false isTruthy(0) // is false
isTruthy('a') // is true isTruthy(1) // is true isTruthy(undefined) // is false isTruthy('') // is false isTruthy(0) // is false
isTruthy('a') // is true isTruthy(1) // is true isTruthy(undefined) // is false isTruthy('') // is false isTruthy(0) // is false
isTruthy('a') // is true isTruthy(1) // is true isTruthy(undefined) // is false isTruthy('') // is false isTruthy(0) // is false
Sometimes we need to handle truthy values. Below, we have applicants compiled from multiple sources. If a field is truthy, that means the data was compiled successfully. Let's clean our data by only keeping the truthy fields.
const applicants = { chris: { yearsExperience: undefined, applied: '2025 02 15', }, liz: { yearsExperience: '2', applied: '', }, } const keepTruthyFields = keepWhen(isTruthy) const cleanApplicants = mapValues(keepTruthyFields) const updatedApplicants = cleanApplicants(applicants) console.log(updatedApplicants) // is { // chris: { // applied: 2025 02 15 // } // liz: { // yearsExperience: 2 // } // }
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 keepTruthyFields = keepWhen(isTruthy)<Applicant> const cleanApplicants = mapValues(keepTruthyFields)<Applicants> const updatedApplicants = cleanApplicants(applicants) console.log(updatedApplicants) // is { // chris: { // applied: 2025 02 15 // } // liz: { // yearsExperience: 2 // } // }
const applicants = { chris: { yearsExperience: undefined, applied: '2025 02 15', }, liz: { yearsExperience: '2', applied: '', }, } const keepTruthyFields = keepWhen(isTruthy) const cleanApplicants = mapValues( keepTruthyFields ) const updatedApplicants = cleanApplicants(applicants) console.log(updatedApplicants) // is { // chris: { // applied: 2025 02 15 // } // liz: { // yearsExperience: 2 // } // }
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 keepTruthyFields = keepWhen( isTruthy )<Applicant> const cleanApplicants = mapValues( keepTruthyFields )<Applicants> const updatedApplicants = cleanApplicants(applicants) console.log(updatedApplicants) // is { // chris: { // applied: 2025 02 15 // } // liz: { // yearsExperience: 2 // } // }