All files / string readingTime.ts

100% Statements 46/46
94.44% Branches 17/18
100% Functions 7/7
100% Lines 46/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167          1x         1x         1x         1x                                                                                                             5x   5x             5x             5x         5x 5x   5x 5x   5x               1x 1x   1x 1x 1x 1x 1x   1x 1x 1x   1x 1x 1x     1x 1x   1x 1x 1x     1x 1x   1x 1x 1x     1x 1x   1x 1x 1x     1x 1x             1x 1x 1x        
import { isObject } from '../is'
 
/**
 * Match Chinese characters
 */
export const REGEXP_CHINESE = /[\u4E00-\u9FFF]/g
 
/**
 * Match Japanese characters
 */
export const REGEXP_JAPANESE = /[\u3040-\u309F\u30A0-\u30FF]/g
 
/**
 * Match Korean characters
 */
export const REGEXP_KOREAN = /[\uAC00-\uD7AF\u1100-\u11FF]/g
 
/**
 * Match CJK punctuation
 */
export const REGEXP_CJK_PUNCTUATION = /[\u3000-\u303F\uFF00-\uFF60]/g
 
/**
 * Options for readingTime
 */
interface ReadingTimeOptions {
  /**
   * Average words read per minute
   */
  wordsPerMinute?: number | {
    /**
     * Average words read per minute for CJK
     */
    cjk: number
 
    /**
     * Average words read per minute for latin, e.g. English
     */
    latin: number
  }
}
 
/**
 * Result for readingTime
 */
interface ReadingTimeResult {
  /**
   * String literal for statistics
   */
  readonly text: string
 
  /**
   * Number of characters
   */
  readonly characters: number
 
  /**
   * Number of words
   */
  readonly words: number
 
  /**
   * Estimated reading market
   */
  readonly minutes: number
}
 
/**
 * Count the number of characters, words, and reading time of a string
 *
 * @param {string} data - String literal for statistics
 * @param {ReadingTimeOptions} options
 * @returns {ReadingTimeResult} result
 */
export function readingTime(data: string, options?: ReadingTimeOptions): ReadingTimeResult {
  const text = data.trim()
 
  const cjkCharacters
    = (text.match(REGEXP_CHINESE) || []).length
    + (text.match(REGEXP_JAPANESE) || []).length
    + (text.match(REGEXP_KOREAN) || []).length
    + (text.match(REGEXP_CJK_PUNCTUATION) || []).length
 
  const latinCharacters
    = text
      .replace(REGEXP_CHINESE, '')
      .replace(REGEXP_JAPANESE, '')
      .replace(REGEXP_KOREAN, '')
      .replace(REGEXP_CJK_PUNCTUATION, '')
 
  const latinWords
    = latinCharacters
      .split(/\s+/)
      .filter(Boolean)
      .length
 
  const wordsPerMinuteForCjk = isObject(options?.wordsPerMinute) ? options.wordsPerMinute?.cjk : options?.wordsPerMinute ?? 200
  const wordsPerMinuteForLatin = isObject(options?.wordsPerMinute) ? options.wordsPerMinute?.latin : options?.wordsPerMinute ?? 300
 
  const words = cjkCharacters + latinWords
  const minutes = Math.ceil(cjkCharacters / wordsPerMinuteForCjk + latinWords / wordsPerMinuteForLatin)
 
  return {
    text,
    characters: text.length,
    words,
    minutes,
  }
}
 
Eif (import.meta.vitest) {
  const { describe, test, expect } = import.meta.vitest
 
  const english = 'Hello, world!'
  const chinese = '你好,世界!'
  const japanese = 'こんにちは世界!'
  const korean = '안녕하세요,세상아!'
  const content = [english, chinese, japanese, korean].join('\n')
 
  describe('readingTime', () => {
    test('english', () => {
      const { characters, words, minutes } = readingTime(english)
 
      expect(characters).toBe(13)
      expect(words).toBe(2)
      expect(minutes).toBe(1)
    })
 
    test('chinese', () => {
      const { characters, words, minutes } = readingTime(chinese)
 
      expect(characters).toBe(6)
      expect(words).toBe(6)
      expect(minutes).toBe(1)
    })
 
    test('japanese', () => {
      const { characters, words, minutes } = readingTime(japanese)
 
      expect(characters).toBe(8)
      expect(words).toBe(8)
      expect(minutes).toBe(1)
    })
 
    test('korean', () => {
      const { characters, words, minutes } = readingTime(korean)
 
      expect(characters).toBe(10)
      expect(words).toBe(10)
      expect(minutes).toBe(1)
    })
 
    test('wordsPerMinute', () => {
      const { characters, words, minutes } = readingTime(content, {
        wordsPerMinute: {
          cjk: 2,
          latin: 5,
        },
      })
 
      expect(characters).toBe(40)
      expect(words).toBe(26)
      expect(minutes).toBe(13)
    })
  })
}