Blame view

node_modules/word-count/index.js 542 Bytes
f7563de62   Palak Handa   first commit
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
  /**
   * Word Count
   *
   * Word count in respect of CJK characters.
   *
   * Copyright (c) 2015 - 2016 by Hsiaoming Yang.
   */
  
  var pattern = /[a-zA-Z0-9_\u0392-\u03c9\u00c0-\u00ff\u0600-\u06ff]+|[\u4e00-\u9fff\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af]+/g;
  
  module.exports = function (data) {
    var m = data.match(pattern);
    var count = 0;
    if (!m) {
      return 0;
    }
    for (var i = 0; i < m.length; i++) {
      if (m[i].charCodeAt(0) >= 0x4e00) {
        count += m[i].length;
      } else {
        count += 1;
      }
    }
    return count;
  };