getSize

getSize

  • (collection: Collection | string) => number
  1. Return the size of the collection

    getSize([1, 2, 3]) // is 3
    getSize('123') // is 3
    getSize({ a: 1, b: 2, c: 3 }) // is 3
    getSize([1, 2, 3]) // is 3
    getSize('123') // is 3
    getSize({ a: 1, b: 2, c: 3 }) // is 3
    
    getSize([1, 2, 3]) // is 3
    getSize('123') // is 3
    getSize({ a: 1, b: 2, c: 3 }) // is 3
    getSize([1, 2, 3]) // is 3
    getSize('123') // is 3
    getSize({ a: 1, b: 2, c: 3 }) // is 3
    
  2. Sometimes we need the size of a collection. Below, we're planning a field trip and need the total number of students. Let's get the size of each class and sum them.

    const classrooms = {
      A: ['grace', 'sam', 'jen'],
      B: ['mike', 'luke', 'emma', 'meg'],
    }
    
    const getClassroomSizes = mapValues(getSize)
    const getNumStudents = compose([getClassroomSizes, sumValues])
    const numStudents = getNumStudents(classrooms)
    console.log(numStudents)
    // is 7
    type Classrooms = Record<string, string[]>
    const classrooms = {
      A: ['grace', 'sam', 'jen'],
      B: ['mike', 'luke', 'emma', 'meg'],
    }
    
    const getClassroomSizes = mapValues(getSize<string[]>)<Classrooms>
    const getNumStudents = compose([getClassroomSizes, sumValues])
    const numStudents = getNumStudents(classrooms)
    console.log(numStudents)
    // is 7
    
    const classrooms = {
      A: ['grace', 'sam', 'jen'],
      B: ['mike', 'luke', 'emma', 'meg'],
    }
    
    const getClassroomSizes = mapValues(getSize)
    const getNumStudents = compose([
      getClassroomSizes,
      sumValues,
    ])
    const numStudents = getNumStudents(classrooms)
    console.log(numStudents)
    // is 7
    type Classrooms = Record<string, string[]>
    const classrooms = {
      A: ['grace', 'sam', 'jen'],
      B: ['mike', 'luke', 'emma', 'meg'],
    }
    
    const getClassroomSizes = mapValues(
      getSize<string[]>
    )<Classrooms>
    const getNumStudents = compose([
      getClassroomSizes,
      sumValues,
    ])
    const numStudents = getNumStudents(classrooms)
    console.log(numStudents)
    // is 7