Skip to content

Commit

Permalink
⚡ improvement(named): using default use nmae when value is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Oct 29, 2016
1 parent 2c17636 commit c34e8f1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export default function (Vue) {
function template (string, ...args) {
if (args.length === 1 && typeof args[0] === 'object') {
args = args[0]
} else {
args = {}
}

if (!args || !args.hasOwnProperty) {
Expand All @@ -34,9 +36,9 @@ export default function (Vue) {
&& string[index + match.length] === '}') {
return i
} else {
result = hasOwn(args, i) ? args[i] : null
result = hasOwn(args, i) ? args[i] : match
if (result === null || result === undefined) {
return match
return ''
}

return result
Expand Down
39 changes: 26 additions & 13 deletions test/specs/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,31 @@ const format = Format(Vue)
describe('format', () => {
describe('argument', () => {
context('Object', () => {
it('should be replace with object value', () => {
const template = 'name: {name}, email: {email}'
assert.equal(format(template, {
name: 'kazupon', email: '[email protected]'
}), 'name: kazupon, email: [email protected]')
context('default delimiter', () => {
it('should be replace with object value', () => {
const template = 'name: {name}, email: {email}'
assert.equal(format(template, {
name: 'kazupon', email: '[email protected]'
}), 'name: kazupon, email: [email protected]')
})
})

it('should be replace with object value', () => {
const template = 'name: %{name}, email: %{email}'
assert.equal(format(template, {
name: 'kazupon', email: '[email protected]'
}), 'name: kazupon, email: [email protected]')
context('RoR delimiter', () => {
it('should be replace with object value', () => {
const template = 'name: %{name}, email: %{email}'
assert.equal(format(template, {
name: 'kazupon', email: '[email protected]'
}), 'name: kazupon, email: [email protected]')
})
})

context('missing', () => {
it('should be replace with as is', () => {
const template = 'name: {name}, email: {email}'
assert.equal(format(template, {
name: 'kazupon'
}), 'name: kazupon, email: {email}')
})
})
})

Expand All @@ -36,21 +49,21 @@ describe('format', () => {
context('null', () => {
it('should be replace with empty', () => {
const template = 'name: {0}, email: {1}'
assert.equal(format(template, null), 'name: , email: ')
assert.equal(format(template, null), 'name: {0}, email: {1}')
})
})

context('undefined', () => {
it('should be replace with empty', () => {
const template = 'name: {0}, email: {1}'
assert.equal(format(template, undefined), 'name: , email: ')
assert.equal(format(template, undefined), 'name: {0}, email: {1}')
})
})

context('not specify', () => {
it('should be replace with empty', () => {
const template = 'name: {0}, email: {1}'
assert.equal(format(template), 'name: , email: ')
assert.equal(format(template), 'name: {0}, email: {1}')
})
})
})
Expand Down

0 comments on commit c34e8f1

Please sign in to comment.