Skip to content

Commit

Permalink
📈 performance(format): use hasOwn function of Vue.util
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Aug 12, 2016
1 parent 81d7359 commit a8a19a0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/extend.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { warn } from './util'
import format from './format'
import Format from './format'
import { getValue } from './path'


Expand All @@ -12,6 +12,7 @@ import { getValue } from './path'

export default function (Vue) {
const { isObject } = Vue.util
const format = Format(Vue)

function parseArgs (...args) {
let lang = Vue.config.lang
Expand Down
58 changes: 32 additions & 26 deletions src/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,42 @@
const RE_NARGS = /(%|)\{([0-9a-zA-Z_]+)\}/g


/**
* template
*
* @param {String} string
* @param {Array} ...args
* @return {String}
*/
export default function (Vue) {
const { hasOwn } = Vue.util

/**
* template
*
* @param {String} string
* @param {Array} ...args
* @return {String}
*/

function template (string, ...args) {
if (args.length === 1 && typeof args[0] === 'object') {
args = args[0]
}

export default function (string, ...args) {
if (args.length === 1 && typeof args[0] === 'object') {
args = args[0]
}
if (!args || !args.hasOwnProperty) {
args = {}
}

if (!args || !args.hasOwnProperty) {
args = {}
}
return string.replace(RE_NARGS, (match, prefix, i, index) => {
let result

return string.replace(RE_NARGS, (match, prefix, i, index) => {
let result
if (string[index - 1] === '{'
&& string[index + match.length] === '}') {
return i
} else {
result = hasOwn(args, i) ? args[i] : null
if (result === null || result === undefined) {
return ''
}

if (string[index - 1] === '{'
&& string[index + match.length] === '}') {
return i
} else {
result = args.hasOwnProperty(i) ? args[i] : null
if (result === null || result === undefined) {
return ''
return result
}
})
}

return result
}
})
return template
}
5 changes: 4 additions & 1 deletion test/specs/format.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Vue from 'vue'
import assert from 'power-assert'
import format from '../../src/format'
import Format from '../../src/format'

const format = Format(Vue)


describe('format', () => {
Expand Down

0 comments on commit a8a19a0

Please sign in to comment.