Code coverage report for pinyin/index.js

Statements: 87.04% (47 / 54)      Branches: 87.5% (21 / 24)      Functions: 100% (5 / 5)      Lines: 87.04% (47 / 54)      Ignored: none     

All files » pinyin/ » index.js
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    1 1 1 1 1   1   1 65 65                 65 65             1 25 25   15 30 30 30 30             10 20     25           1   195       195 195 195   195   510 510   510     260 30 30     260 235   25       250         195 60 60     195   1 1 1 1 1  
"use strict";
 
const utils = require("./src/utils");
const PINYIN_DICT = require("./data/dict-zi");
const pinyin = require("./src/pinyin");
let jieba;
let PHRASES_DICT;
 
pinyin.setDict(PINYIN_DICT);
 
function segment(hans) {
  try {
    jieba = jieba || require("nodejieba");
  } catch (ex) {
    console.error();
    console.error("    Segment need nodejieba, please run '$ npm install nodejieba'.");
    console.error("    分词需要使用 nodejieba 模块,请运行 '$ npm install nodejieba' 并确保安装完成。");
    console.error();
    throw ex;
  }
  // 词语拼音库。
  PHRASES_DICT = PHRASES_DICT || require("./data/phrases-dict");
  return jieba.cut(hans);
}
 
// 词语注音
// @param {String} phrases, 指定的词组。
// @param {Object} options, 选项。
// @return {Array}
function phrases_pinyin(phrases, options) {
  var py = [];
  if (PHRASES_DICT.hasOwnProperty(phrases)){
    //! copy pinyin result.
    PHRASES_DICT[phrases].forEach(function(item, idx){
      py[idx] = [];
      Eif (options.heteronym){
        item.forEach(function(py_item, py_index){
          py[idx][py_index] = pinyin.toFixed(py_item, options.style);
        });
      } else {
        py[idx][0] = pinyin.toFixed(item[0], options.style);
      }
    });
  } else {
    for(var i = 0, l = phrases.length; i < l; i++){
      py = py.concat(pinyin(phrases[i], options));
    }
  }
  return py;
}
 
// @param {String} hans 要转为拼音的目标字符串(汉字)。
// @param {Object} options, 可选,用于指定拼音风格,是否启用多音字。
// @return {Array} 返回的拼音列表。
module.exports = function(hans, options) {
 
  Iif(typeof hans !== "string"){
    return [];
  }
 
  options = utils.extend(utils.DEFAULT_OPTIONS, options || {});
  var phrases = options && options.segment ? segment(hans) : hans;
  var pys = [];
 
  for(var i = 0, nohans = "", firstCharCode, words, l = phrases.length; i < l; i++){
 
    words = phrases[i];
    firstCharCode = words.charCodeAt(0);
 
    if(PINYIN_DICT[firstCharCode]){
 
      // ends of non-chinese words.
      if(nohans.length > 0){
        pys.push([nohans]);
        nohans = ""; // reset non-chinese words.
      }
 
      if(words.length === 1){
        pys = pys.concat(pinyin(words, options));
      }else{
        pys = pys.concat(phrases_pinyin(words, options));
      }
 
    }else{
      nohans += words;
    }
  }
 
  // 清理最后的非中文字符串。
  if(nohans.length > 0){
    pys.push([nohans]);
    nohans = ""; // reset non-chinese words.
  }
 
  return pys;
};
module.exports.STYLE_NORMAL = utils.PINYIN_STYLE.NORMAL;
module.exports.STYLE_TONE = utils.PINYIN_STYLE.TONE;
module.exports.STYLE_TONE2 = utils.PINYIN_STYLE.TONE2;
module.exports.STYLE_INITIALS = utils.PINYIN_STYLE.INITIALS;
module.exports.STYLE_FIRST_LETTER = utils.PINYIN_STYLE.FIRST_LETTER;