Skip to content

Commit

Permalink
Merge pull request #19 from Fishfaceeast/fix
Browse files Browse the repository at this point in the history
修改_toTemplateId和_toElementId参数检测
  • Loading branch information
cssmagic committed Oct 15, 2015
2 parents ae3a2a8 + 5f6e9e6 commit 0bdfc34
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/underscore-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ var template = function () {
//`#template-my-tpl-001` -> `my-tpl-001`
// `template-my-tpl-001` -> `my-tpl-001`
// `my-tpl-001` -> `my-tpl-001`
id = _.isString(id) && id ? _trim(id).replace(/^[#!]+/, '') : ''
id = id && _.isString(id) ? _trim(id).replace(/^[#!]+/, '') : ''
return _trim(id).replace(ELEM_ID_PREFIX, '')
}
function _toElementId(id) {
//`template-my-tpl-001` -> `template-my-tpl-001`
// `my-tpl-001` -> `template-my-tpl-001`
id = id ? _trim(id) : ''
id = id && _.isString(id) ? _trim(id) : ''
return _startsWith(id, ELEM_ID_PREFIX) ? id : ELEM_ID_PREFIX + id
}
function _stripCommentTag(str) {
Expand Down
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ void function () {
arg = NaN
expect(_toTemplateId(arg)).to.equal('')
})
it('converts invalid type to empty string', function () {
var arg
arg = 1999
expect(_toTemplateId(arg)).to.equal('')
arg = {name: 'Peter', age: '20'}
expect(_toTemplateId(arg)).to.equal('')
arg = [1,2,3]
expect(_toTemplateId(arg)).to.equal('')
})
})
describe('_toElementId()', function () {
it('adds `' + PREFIX + '` prefix', function () {
Expand All @@ -194,6 +203,15 @@ void function () {
arg = NaN
expect(_toElementId(arg)).to.equal(PREFIX)
})
it('converts invalid type to `' + PREFIX + '`', function () {
var arg
arg = 1999
expect(_toElementId(arg)).to.equal(PREFIX)
arg = {name: 'Peter', age: '20'}
expect(_toElementId(arg)).to.equal(PREFIX)
arg = [1,2,3]
expect(_toElementId(arg)).to.equal(PREFIX)
})
})
describe('_isTemplateCode()', function () {
it('does basic functionality', function () {
Expand Down

0 comments on commit 0bdfc34

Please sign in to comment.