code: function applyToCardState(prev, ts, rating) {
    if (prev.lastReviewed != null && prev.lastReviewed > ts) {
      var p = prev.lastReviewed.toISOString();
      var t = ts.toISOString();
      throw new Error('cannot apply review before current lastReviewed: ' + p + ' > ' + t);
    }
  
    if (prev.mode === 'learning') {
      return applyToLearningCardState(prev, ts, rating);
    } else if (prev.mode === 'reviewing') {
      return applyToReviewingCardState(prev, ts, rating);
    } else if (prev.mode === 'lapsed') {
      return applyToLapsedCardState(prev, ts, rating);
    }
    throw new Error('invalid mode: ' + prev.mode);
  }
docs: -- applyReview(...)