Skip to content

Commit

Permalink
Fixes string v-model support on Edge and Safari
Browse files Browse the repository at this point in the history
Added polyfill of "String.prototype.matchAll" for those browsers (fixes #76)
  • Loading branch information
phoenixwong committed Sep 29, 2019
1 parent cf121b6 commit 90e672a
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/vue-timepicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,10 @@ export default {
const tokensRegxStr = `[(${regxStr})]+`
const othersRegxStr = `[^(${regxStr})]+`
const tokensMatchAll = formatString.matchAll(new RegExp(tokensRegxStr, 'g'))
const othersMatchAll = formatString.matchAll(new RegExp(othersRegxStr, 'g'))
const needsPolyfill = Boolean(!formatString.matchAll || typeof formatString.matchAll !== 'function')
const tokensMatchAll = needsPolyfill ? this.matchAllPolyfill(formatString, tokensRegxStr) : formatString.matchAll(new RegExp(tokensRegxStr, 'g'))
const othersMatchAll = needsPolyfill ? this.matchAllPolyfill(formatString, othersRegxStr) : formatString.matchAll(new RegExp(othersRegxStr, 'g'))
const chunks = []
const tokenChunks = []
Expand Down Expand Up @@ -616,6 +618,35 @@ export default {
}
},
matchAllPolyfill (targetString, regxStr) {
const matchesList = targetString.match(new RegExp(regxStr, 'g'))
const result = []
const indicesReg = []
if (matchesList && matchesList.length) {
matchesList.forEach(matchedItem => {
const existIndex = indicesReg.findIndex(idxItem => idxItem.str === matchedItem)
let index
if (existIndex >= 0) {
if (indicesReg[existIndex] && indicesReg[existIndex].regex) {
index = indicesReg[existIndex].regex.exec(targetString).index
}
} else {
const itemIndicesRegex = new RegExp(matchedItem, 'g')
index = itemIndicesRegex.exec(targetString).index
indicesReg.push({
str: String(matchedItem),
regex: itemIndicesRegex
})
}
result.push({
0: String(matchedItem),
index: index
})
})
}
return result
},
addFallbackValues () {
const timeValue = {}
timeValue[this.hourType] = ''
Expand Down

0 comments on commit 90e672a

Please sign in to comment.