Sumsar
Sumsar

Reputation: 11

How to change plurality depending on array length

I need to automatically change the plurality of the Student(s), Supervisor(s) and Co-Supervisor(s) depending on the array length. Note the authors array will have to contain atleast one entry (string) the supervisor and co-supervisor do not. I tried using if statements, while inside the stack, but it keeps giving me syntax error, and i do not understand how else to write it.

grid(
  columns: 2,
  column-gutter: if authors.len() > 0 and authors.len() > 20{
  3em
  }else{
  7em  
  },
  // Students column
  stack(
    dir: ttb,
    spacing: 0.7em,
    if authors.len() > 1{
      align(start, emph("Students:")),
      ..authors.map(author => align(start, author))   
    }else{
      align(start, emph("Student:")),
      align(start, authors.first())
    }),
  // Supervisors column
  stack(
    dir: ttb,
    spacing: 0.7em,
      align(start, emph("Supervisors:")),
      ..supervisors.map(supervisor => align(start, supervisor)),
      // Co-Supervisors
      align(start, emph("Co-Supervisor:")),
      ..co-supervisors.map(co-supervisor => align(start, co-supervisor))
  ),
)

Upvotes: 0

Views: 27

Answers (1)

Campbell He
Campbell He

Reputation: 535

Here is a not-very-elegant solution:

stack(
  dir: ttb,
  spacing: 0.7em,
  ..{
    if authors.len() > 1{
      (align(start, emph("Students:")),) + authors.map(author => align(start, author))
    } else {
      (align(start, emph("Student:")), align(start, authors.first()))
    }
  }
),

We return the array in the if-else block and extract contents using .. outside of the if-else block.

Upvotes: 0

Related Questions