index.js 679 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict'
  2. module.exports = longestStreak
  3. // Get the count of the longest repeating streak of `character` in `value`.
  4. function longestStreak(value, character) {
  5. var count = 0
  6. var maximum = 0
  7. var expected
  8. var index
  9. if (typeof character !== 'string' || character.length !== 1) {
  10. throw new Error('Expected character')
  11. }
  12. value = String(value)
  13. index = value.indexOf(character)
  14. expected = index
  15. while (index !== -1) {
  16. count++
  17. if (index === expected) {
  18. if (count > maximum) {
  19. maximum = count
  20. }
  21. } else {
  22. count = 1
  23. }
  24. expected = index + 1
  25. index = value.indexOf(character, expected)
  26. }
  27. return maximum
  28. }