From c34e8f194b407b5c9eb8eb91e327f6ef06c37aa4 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Sun, 30 Oct 2016 01:57:02 +0900 Subject: [PATCH] :zap: improvement(named): using default use nmae when value is missing --- src/format.js | 6 ++++-- test/specs/format.js | 39 ++++++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/format.js b/src/format.js index f2b2de5d1..d5c89a072 100644 --- a/src/format.js +++ b/src/format.js @@ -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) { @@ -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 diff --git a/test/specs/format.js b/test/specs/format.js index ae0afe3e9..7ff2b2414 100644 --- a/test/specs/format.js +++ b/test/specs/format.js @@ -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: 'foo@domain.com' - }), 'name: kazupon, email: foo@domain.com') + context('default delimiter', () => { + it('should be replace with object value', () => { + const template = 'name: {name}, email: {email}' + assert.equal(format(template, { + name: 'kazupon', email: 'foo@domain.com' + }), 'name: kazupon, email: foo@domain.com') + }) }) - it('should be replace with object value', () => { - const template = 'name: %{name}, email: %{email}' - assert.equal(format(template, { - name: 'kazupon', email: 'foo@domain.com' - }), 'name: kazupon, email: foo@domain.com') + context('RoR delimiter', () => { + it('should be replace with object value', () => { + const template = 'name: %{name}, email: %{email}' + assert.equal(format(template, { + name: 'kazupon', email: 'foo@domain.com' + }), 'name: kazupon, email: foo@domain.com') + }) + }) + + 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}') + }) }) }) @@ -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}') }) }) })