Code coverage report for wechat-enterprise/lib/list.js

Statements: 100% (21 / 21)      Branches: 100% (0 / 0)      Functions: 100% (7 / 7)      Lines: 100% (21 / 21)      Ignored: none     

All files » wechat-enterprise/lib/ » list.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      1         1 5             1 13               1 5 5 5 5 15   15 14 14   15   5               1 14             1 1     1  
/*!
 * 缓存列表
 */
var listCache = {};
 
/**
 * 回复列表类型
 */
var List = function () {
  this.map = {};
};
 
/**
 * 从List对象中根据key取出对应的handler
 * @param {String} key 列表中的关键词
 */
List.prototype.get = function (key) {
  return this.map[key];
};
 
/**
 * 静态方法,根据items生成List对象,并放置到缓存中
 * @param {String} name 列表名字
 * @param {Array} items 元素列表
 */
List.add = function (name, items) {
  var description = [];
  var list = new List();
  list.name = name;
  items.forEach(function (item) {
    var text = item[0];
    // 抽取出key,并关联上对应的handle
    var replaced = text.replace(/\{(.*)\}/, function (match, key) {
      list.map[key] = item[1];
      return key;
    });
    description.push(replaced);
  });
  list.description = description.join('\n'),
  listCache[name] = list;
};
 
/**
 * 静态方法,从缓存中根据名字取出List对象
 * @param {String} name 列表名字
 */
List.get = function (name) {
  return listCache[name];
};
 
/**
 * 静态方法,清空缓存的所有的List对象
 * @param {String} name 列表名字
 */
List.clear = function () {
  listCache = {};
};
 
module.exports = List;