isLaden
isLaden
(collection: Collection | string) => boolean
Test if a collection or string is non-empty
isLaden([1]) // is true isLaden([]) // is false isLaden('a') // is true isLaden('') // is false
isLaden([1]) // is true isLaden([]) // is false isLaden('a') // is true isLaden('') // is false
isLaden([1]) // is true isLaden([]) // is false isLaden('a') // is true isLaden('') // is false
isLaden([1]) // is true isLaden([]) // is false isLaden('a') // is true isLaden('') // is false
Usually we can iterate over collections without worrying whether they contain elements. Sometimes, however, we need to treat non-empty collections differently. Below, we have appointments scheduled for the next week. To avoid a bunch of empty days in our display, let's only show those with appointments.
const appointments = { Monday: [], Tuesday: [ { time: '8am-9:30', client: "Baz's Baby Furniture", }, { time: '11am-12', client: 'University Of Florida', }, ], Wednesday: [], Thursday: [], Friday: [ { time: '9am-10', client: 'RVs R Us', }, ], } const displayOneAppt = ({ time, client }) => { return ` At: ${time}\n` + ` With: ${client}` } const displayDayAppts = (appts, day) => { const dayAppts = appts.map(displayOneAppt).join('\n\n') return `${day}:\n` + dayAppts } const displayAllDayAppts = compose([ keepWhen(isLaden), mapValues(displayDayAppts), joinValues('\n\n'), ]) const apptDisplay = displayAllDayAppts(appointments) console.log(apptDisplay) // prints // Tuesday: // At: 8am-9:30 // With: Baz's Baby Furniture // // At: 11am-12 // With: University Of Florida // // Friday: // At: 9am-10 // With: RVs R Us
type Appointment = { time: string client: string } type Appointments = Record<string, Appointment[]> const appointments: Appointments = { Monday: [], Tuesday: [ { time: '8am-9:30', client: "Baz's Baby Furniture", }, { time: '11am-12', client: 'University Of Florida', }, ], Wednesday: [], Thursday: [], Friday: [ { time: '9am-10', client: 'RVs R Us', }, ], } const displayOneAppt = ({ time, client }: Appointment) => { return ` At: ${time}\n` + ` With: ${client}` } const displayDayAppts = (appts: Appointment[], day: string) => { const dayAppts = appts.map(displayOneAppt).join('\n\n') return `${day}:\n` + dayAppts } const displayAllDayAppts = compose([ keepWhen(isLaden)<Appointments>, mapValues(displayDayAppts), joinValues('\n\n'), ]) const apptDisplay = displayAllDayAppts(appointments) console.log(apptDisplay) // prints // Tuesday: // At: 8am-9:30 // With: Baz's Baby Furniture // // At: 11am-12 // With: University Of Florida // // Friday: // At: 9am-10 // With: RVs R Us
const appointments = { Monday: [], Tuesday: [ { time: '8am-9:30', client: "Baz's Baby Furniture", }, { time: '11am-12', client: 'University Of Florida', }, ], Wednesday: [], Thursday: [], Friday: [ { time: '9am-10', client: 'RVs R Us', }, ], } const displayOneAppt = ({ time, client }) => { return ` At: ${time}\n` + ` With: ${client}` } const displayDayAppts = (appts, day) => { const dayAppts = appts .map(displayOneAppt) .join('\n\n') return `${day}:\n` + dayAppts } const displayAllDayAppts = compose([ keepWhen(isLaden), mapValues(displayDayAppts), joinValues('\n\n'), ]) const apptDisplay = displayAllDayAppts(appointments) console.log(apptDisplay) // prints // Tuesday: // At: 8am-9:30 // With: Baz's Baby Furniture // // At: 11am-12 // With: University Of Florida // // Friday: // At: 9am-10 // With: RVs R Us
type Appointment = { time: string client: string } type Appointments = Record<string, Appointment[]> const appointments: Appointments = { Monday: [], Tuesday: [ { time: '8am-9:30', client: "Baz's Baby Furniture", }, { time: '11am-12', client: 'University Of Florida', }, ], Wednesday: [], Thursday: [], Friday: [ { time: '9am-10', client: 'RVs R Us', }, ], } const displayOneAppt = ({ time, client, }: Appointment) => { return ` At: ${time}\n` + ` With: ${client}` } const displayDayAppts = ( appts: Appointment[], day: string ) => { const dayAppts = appts .map(displayOneAppt) .join('\n\n') return `${day}:\n` + dayAppts } const displayAllDayAppts = compose([ keepWhen(isLaden)<Appointments>, mapValues(displayDayAppts), joinValues('\n\n'), ]) const apptDisplay = displayAllDayAppts(appointments) console.log(apptDisplay) // prints // Tuesday: // At: 8am-9:30 // With: Baz's Baby Furniture // // At: 11am-12 // With: University Of Florida // // Friday: // At: 9am-10 // With: RVs R Us
I understand "laden" is not a common term. It's the best I could find to convey a positive form of "non-empty" with the purpose of avoiding double negatives.