From 8d5bf0f04e5dfd45f13b8b9e4cb5c1314907ce39 Mon Sep 17 00:00:00 2001 From: LiSheep Date: Thu, 30 Oct 2014 22:33:28 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0List=E5=9B=9E=E5=A4=8D?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E8=87=AA=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/list.js | 9 +++++++-- test/list.test.js | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/lib/list.js b/lib/list.js index 308fea60..d2c82312 100644 --- a/lib/list.js +++ b/lib/list.js @@ -1,3 +1,4 @@ +var util = require('util'); /*! * 缓存列表 */ @@ -22,8 +23,11 @@ List.prototype.get = function (key) { * 静态方法,根据items生成List对象,并放置到缓存中 * @param {String} name 列表名字 * @param {Array} items 元素列表 + * @param {String} head 回复开头 + * @param {String} deli 回复分隔符 + * @param {String} foot 回复底部 */ -List.add = function (name, items) { +List.add = function (name, items, head, deli, foot) { var description = []; var list = new List(); list.name = name; @@ -36,7 +40,8 @@ List.add = function (name, items) { }); description.push(replaced); }); - list.description = description.join('\n'), + var lists = description.join( '\n' + (deli||'') + '\n'); + list.description = util.format('%s\n%s\n%s', head||'', lists, (foot||'')) listCache[name] = list; }; diff --git a/test/list.test.js b/test/list.test.js index 2469b623..a0d3430f 100644 --- a/test/list.test.js +++ b/test/list.test.js @@ -2,7 +2,39 @@ var should = require('should'); var List = require('../').List; describe('list.js', function () { - it('should ok', function () { + it.only('should ok', function () { + var common = [ + ['选择{a}查看啥', function () {}], + ['选择{b}查看啥', function () {}], + ['选择{c}查看啥', function () {}] + ]; + var head = '我是头'; + var deli = '-------'; + var foot = '我是小尾巴'; + + List.add('common', common, head, deli, '我是小尾巴'); + var list = List.get('common'); + list.description.should.be.equal(head + '\n选择a查看啥\n' + deli +'\n选择b查看啥\n' + deli+'\n选择c查看啥\n' + foot); + list.get('a').should.be.equal(common[0][1]); + list.get('b').should.be.equal(common[1][1]); + list.get('c').should.be.equal(common[2][1]); + }); + + it('should ok when clear', function () { + var common = [ + ['选择{a}查看啥', function () {}], + ['选择{b}查看啥', function () {}], + ['选择{c}查看啥', function () {}] + ]; + List.add('common', common); + var list = List.get('common'); + should.exist(list); + List.clear(); + list = List.get('common'); + should.not.exist(list); + }); + + it('add body should ok', function () { var common = [ ['选择{a}查看啥', function () {}], ['选择{b}查看啥', function () {}], From 4a82a33ca4e599857cc52dbe059fdb008d6dd600 Mon Sep 17 00:00:00 2001 From: ltc Date: Fri, 31 Oct 2014 15:01:33 +0800 Subject: [PATCH 2/6] Update list.js --- lib/list.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/list.js b/lib/list.js index d2c82312..421e6c52 100644 --- a/lib/list.js +++ b/lib/list.js @@ -24,10 +24,10 @@ List.prototype.get = function (key) { * @param {String} name 列表名字 * @param {Array} items 元素列表 * @param {String} head 回复开头 - * @param {String} deli 回复分隔符 + * @param {String} delimiter 回复分隔符 * @param {String} foot 回复底部 */ -List.add = function (name, items, head, deli, foot) { +List.add = function (name, items, head, delimiter, foot) { var description = []; var list = new List(); list.name = name; @@ -40,8 +40,8 @@ List.add = function (name, items, head, deli, foot) { }); description.push(replaced); }); - var lists = description.join( '\n' + (deli||'') + '\n'); - list.description = util.format('%s\n%s\n%s', head||'', lists, (foot||'')) + var lists = description.join('\n' + (delimiter || '') + '\n'); + list.description = util.format('%s\n%s\n%s', head || '', lists, (foot || '')) listCache[name] = list; }; From 21a1b8020ecf418d85f4481dd4121ea8d03d7811 Mon Sep 17 00:00:00 2001 From: ltc Date: Fri, 31 Oct 2014 20:51:54 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=9A=84List=E5=9B=9E=E5=A4=8D=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/list.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/list.js b/lib/list.js index 421e6c52..69aac91a 100644 --- a/lib/list.js +++ b/lib/list.js @@ -28,6 +28,9 @@ List.prototype.get = function (key) { * @param {String} foot 回复底部 */ List.add = function (name, items, head, delimiter, foot) { + if(arguments.length > 2 && arguments.length != 5){ + throw 'List.add arguments error'; + } var description = []; var list = new List(); list.name = name; @@ -40,8 +43,13 @@ List.add = function (name, items, head, delimiter, foot) { }); description.push(replaced); }); - var lists = description.join('\n' + (delimiter || '') + '\n'); - list.description = util.format('%s\n%s\n%s', head || '', lists, (foot || '')) + + if(delimiter) { + var lists = description.join('\n' + (delimiter || '') + '\n'); + list.description = util.format('%s\n%s\n%s', head || '', lists, (foot || '')); + }else { + list.description = description.join('\n'); + } listCache[name] = list; }; From 34ce3861e4a033d20c34fc5a88fc393fb69f5a6f Mon Sep 17 00:00:00 2001 From: ltc Date: Fri, 31 Oct 2014 20:53:14 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=9A=84List=E5=9B=9E=E5=A4=8D=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/list.test.js | 53 ++++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/test/list.test.js b/test/list.test.js index a0d3430f..83b17300 100644 --- a/test/list.test.js +++ b/test/list.test.js @@ -1,40 +1,9 @@ + var should = require('should'); var List = require('../').List; describe('list.js', function () { - it.only('should ok', function () { - var common = [ - ['选择{a}查看啥', function () {}], - ['选择{b}查看啥', function () {}], - ['选择{c}查看啥', function () {}] - ]; - var head = '我是头'; - var deli = '-------'; - var foot = '我是小尾巴'; - - List.add('common', common, head, deli, '我是小尾巴'); - var list = List.get('common'); - list.description.should.be.equal(head + '\n选择a查看啥\n' + deli +'\n选择b查看啥\n' + deli+'\n选择c查看啥\n' + foot); - list.get('a').should.be.equal(common[0][1]); - list.get('b').should.be.equal(common[1][1]); - list.get('c').should.be.equal(common[2][1]); - }); - - it('should ok when clear', function () { - var common = [ - ['选择{a}查看啥', function () {}], - ['选择{b}查看啥', function () {}], - ['选择{c}查看啥', function () {}] - ]; - List.add('common', common); - var list = List.get('common'); - should.exist(list); - List.clear(); - list = List.get('common'); - should.not.exist(list); - }); - - it('add body should ok', function () { + it('should ok', function () { var common = [ ['选择{a}查看啥', function () {}], ['选择{b}查看啥', function () {}], @@ -76,4 +45,22 @@ describe('list.js', function () { list.get('b').should.be.equal(common[2][1]); list.get('c').should.be.equal(common[3][1]); }); + + it('should ok with body, foot, delimiter', function() { + var common = [ + ['选择{a}查看啥', function () {}], + ['选择{b}查看啥', function () {}], + ['选择{c}查看啥', function () {}] + ]; + var head = '我是头'; + var delimiter = '-------'; + var foot = '我是小尾巴'; + + List.add('common', common, head, delimiter, '我是小尾巴'); + var list = List.get('common'); + list.description.should.be.equal(head + '\n选择a查看啥\n' + delimiter + '\n选择b查看啥\n' + delimiter + '\n选择c查看啥\n' + foot); + list.get('a').should.be.equal(common[0][1]); + list.get('b').should.be.equal(common[1][1]); + list.get('c').should.be.equal(common[2][1]); + }); }); From 2df35e9d284e5a8bb3720a832ba6c587b20093da Mon Sep 17 00:00:00 2001 From: ltc Date: Sat, 1 Nov 2014 00:27:11 +0800 Subject: [PATCH 5/6] Update list.js --- lib/list.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/list.js b/lib/list.js index 69aac91a..ffaaac14 100644 --- a/lib/list.js +++ b/lib/list.js @@ -27,29 +27,29 @@ List.prototype.get = function (key) { * @param {String} delimiter 回复分隔符 * @param {String} foot 回复底部 */ -List.add = function (name, items, head, delimiter, foot) { - if(arguments.length > 2 && arguments.length != 5){ +List.add = function(name, items, head, delimiter, foot) { + if (arguments.length > 2 && arguments.length != 5) { throw 'List.add arguments error'; } var description = []; var list = new List(); list.name = name; - items.forEach(function (item) { + items.forEach(function(item) { var text = item[0]; // 抽取出key,并关联上对应的handle - var replaced = text.replace(/\{(.*)\}/, function (match, key) { + var replaced = text.replace(/\{(.*)\}/, function(match, key) { list.map[key] = item[1]; return key; }); description.push(replaced); }); - - if(delimiter) { + + if (delimiter) { var lists = description.join('\n' + (delimiter || '') + '\n'); list.description = util.format('%s\n%s\n%s', head || '', lists, (foot || '')); - }else { + } else { list.description = description.join('\n'); - } + } listCache[name] = list; }; From 4c53d0ceee40a96a72323d7afae4cb60981d0712 Mon Sep 17 00:00:00 2001 From: ltc Date: Sat, 1 Nov 2014 00:28:33 +0800 Subject: [PATCH 6/6] Update list.js --- lib/list.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/list.js b/lib/list.js index ffaaac14..80539bdd 100644 --- a/lib/list.js +++ b/lib/list.js @@ -28,9 +28,6 @@ List.prototype.get = function (key) { * @param {String} foot 回复底部 */ List.add = function(name, items, head, delimiter, foot) { - if (arguments.length > 2 && arguments.length != 5) { - throw 'List.add arguments error'; - } var description = []; var list = new List(); list.name = name;